1

I am using simple_form 3 and bootstrap 3 with rails 4. I am using the following for my form

   <%= f.association :characteristics, collection: @holidays, 
                                       prompt: "Select Holidays", 
                                       label: false,  as: :check_boxes %>

I do not want to use a button as suggested here Rails 3.1 Select All checkbox, I'd prefer a checkbox in the same list as the other checkboxes.

How can I set up this "select all" checkbox?

1

1 Answer 1

1

Try the following:

<%= f.association :characteristics, 
                  collection: [Holiday.new(id: 0, name: 'Select All')] + @holidays, 
                  label: false,  as: :check_boxes,
                  input_html: {data: {behaviour: 'select-all'}} %>

And then in js:

$('[data-behaviour="select-all"][value=0]').on('change', function(){
  var checked = this.checked;
  $('[data-behaviour="select-all"]').each(function(){
    $(this).prop('checked', checked);
  });
})

Is not an elegant solution but I think it should works

1
  • Holiday.new(id: 0, name: 'Select All') Genius! Nov 15, 2017 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.