Skip to content

Instantly share code, notes, and snippets.

@Sohair63
Created May 18, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sohair63/5cb525d9612938289a83908424e1cfae to your computer and use it in GitHub Desktop.
Save Sohair63/5cb525d9612938289a83908424e1cfae to your computer and use it in GitHub Desktop.
this gist includes javascript/coffee with ruby on rails form to bulk / individual checkbox selection
<script type="text/javascript">
// JAVASCRIPT
window.bind_bulk_resources_checkbox_toggle = function() {
return $('#bulk_toggle_checkbox').click(function() {
if ($('#bulk_toggle_checkbox').is(':checked')) {
$('#bulk_form input:checkbox').prop('checked', true);
return $('#bulk_form input:checkbox').each(function() {
return $(this).val($(this).parents('tr').find('.selected_ids').val());
});
} else {
$('#bulk_form input:checkbox').prop('checked', false);
return $('#bulk_form input:checkbox').each(function() {
return $(this).val(0);
});
}
});
};
window.bind_resource_checkboxes = function() {
return $('body').on('click', '#bulk_form input:checkbox', function() {
if ($(this).is(':checked')) {
return $(this).val($(this).parents('tr').find('.selected_ids').val());
} else {
return $(this).val(0);
}
});
};
// OR ALTERNATIVELY COFFEESCRIPT
window.bind_bulk_resources_checkbox_toggle = ->
$('#bulk_toggle_checkbox').click ->
if $('#bulk_toggle_checkbox').is(':checked')
$('#bulk_form input:checkbox').prop 'checked', true
$('#bulk_form input:checkbox').each ->
$(this).val $(this).parents('tr').find('.selected_ids').val()
else
$('#bulk_form input:checkbox').prop 'checked', false
$('#bulk_form input:checkbox').each ->
$(this).val 0
window.bind_resource_checkboxes = ->
$('body').on 'click', '#bulk_form input:checkbox', ->
if $(this).is(':checked')
$(this).val $(this).parents('tr').find('.selected_ids').val()
else
$(this).val 0
</script>
<!-- RAILS FORM -->
<%= form_tag(path), {id: 'bulk_form'}) do %>
<%= submit_tag 'Bulk Update' %>
<table>
<thead>
<tr>
<th><%= check_box_tag 'bulk_tag', 'yes', false, id: 'bulk_toggle_checkbox' %></th>
</tr>
</thead>
<tbody>
<% @resources.each do |resource| %>
<td>
<!-- ONLY SELECTED RESOURCE IDS -->
<%= check_box_tag 'selected_resources_ids[]', '0' %>
<!-- ALL RESOURCE IDS -->
<%= hidden_field_tag 'resources_ids[]', resource.id, class: 'selected_ids' %>
</td>
<% end %>
</tbody>
</table>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment