trestle icon indicating copy to clipboard operation
trestle copied to clipboard

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

Open palutova opened this issue 3 years ago • 1 comments

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

palutova avatar Aug 26 '22 02:08 palutova

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');
    }
  });
});

spohlenz avatar Aug 29 '22 06:08 spohlenz