Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
palutova opened this issue Aug 26, 2022 · 1 comment
Open

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

palutova opened this issue Aug 26, 2022 · 1 comment

Comments

@palutova
Copy link

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants