TrestleAdmin / trestle

A modern, responsive admin framework for Ruby on Rails
https://trestle.io
GNU Lesser General Public License v3.0
1.96k stars 177 forks source link

how show unchecked checkbox in a column(show table)? #420

Open palutova opened 2 years ago

palutova commented 2 years ago

how to show an unchecked checkbox in a column? so that the user can change this parameter without going into edit mode (form) image

spohlenz commented 2 years ago

The following is a little awkward but is the best approach I can find currently. The Hotwire/Stimulus updates I am currently working on should enable a better approach in future, so stay tuned for that.

In your table:

column :enabled, align: :center do |instance|
  form_for instance, url: admin.instance_path(instance), remote: true, data: { behavior: "toggle-form", type: "json" } do |f|
    content_tag(:div, class: "custom-control custom-switch") do
      f.check_box(:enabled, class: "custom-control-input") + f.label(:enabled, class: "custom-control-label") { "" }
    end
  end
end

(replace custom-switch with custom-checkbox if you prefer a regular checkbox style)

In app/assets/javascripts/trestle/custom.js:

Trestle.init(function (root) {
  $(root).find('[data-behavior="toggle-form"]').on('click', function (e) {
    e.stopPropagation();

    if (e.target.matches('input')) {
      $.rails.fire(this, 'submit');
    }
  });
});