Reputation: 10823
this is my view:
@bulk_objects.each do |bulk_warehouse|
bulk_error = @wh_errors[:"bulk_warehouse#{@count}"] if @wh_errors
-%>
<tr class="<%= cycle("odd", "even") %>">
<%= hidden_field_tag("bulk_warehouse_id#{@count}",bulk_warehouse.id) %>
<td><%= text_field_tag("bulk_warehouse_asset#{@count}", bulk_warehouse.asset, :disabled => true)%></td>
<td><%= text_field_tag("bulk_warehouse_serial#{@count}", bulk_warehouse.serial, :disabled => true) %></td>
<td><%= check_box_tag "enable_record#{@count}",1,false,{:onclick => "bulk_warehouse_asset#{@count}.disabled =
bulk_warehouse_serial#{@count}.disabled =
!this.checked;"}%></td>
<td class="last">
<%= link_to "#{t("web-app-theme.delete", :default => "Delete")}", bulk_warehouse_path(bulk_warehouse), :method => :delete, :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}" %>
</td>
</tr>
</div>
<% @count = @count +1 %>
<% end -%>
</table>
<div class="actions-bar wat-cf">
<div class="actions">
</div>
..
and this is my controller:
@bulk_objects = BulkWarehouse.all
@count= @bulk_objects.count
Now I would add in my view a "Select all" checkbox that when you click on it enables all other "enable_record#{@count}" checkbox. I know that this thing should be done using Ajax and Jquery but I don't know how. Anyone can help me? Thank you ll
Upvotes: 2
Views: 3042
Reputation: 161
Just to expand on @Aaron's answer, this would solve the OP's problem without altering other checkboxes on the page:
$('#check-all').click(function(e) {
var checkboxes = $(e.target).closest('table').find('input:checkbox');
var checked = $(e.target).attr('checked') === undefined;
checkboxes.attr('checked', checked);
checkboxes.not('#check-all').click(function() {
$(e.target).attr('checked', false);
});
});
It also has the benefit of unchecking the select all box if one of the boxes gets unchecked.
Upvotes: 0
Reputation: 11723
You can use button_to_function to create a button that will check/uncheck all the boxes on the form.
Place this code in your view, where you want your check/uncheck button to go. When the button is clicked, it will run the toggleChecked javascript function.
<%= button_to_function "Check / Uncheck All", "toggleChecked()" >
Place your javascript code at the bottom of your view.
<script type='text/javascript'>
var toggleChecked = function() { $('input[type="checkbox"]').click(); });
</script>
The function also can be put into the respective .js file in assets/javascripts.
That should get you up and running.
Button_to_function is now deprecated. I recommend handling this using an HTML5 button and jQuery.
View code for your button:
<button type="button" id="check_all">
Check / Uncheck All
</button>
Javascript:
<script type='text/javascript'>
$('#check_all').on("click", function(){ $('input[type="checkbox"]').click(); });
</script>
Upvotes: 9