Skip to content

Instantly share code, notes, and snippets.

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/73784a2ebefe63a7822ac7077050018d to your computer and use it in GitHub Desktop.
Save Sohair63/73784a2ebefe63a7822ac7077050018d to your computer and use it in GitHub Desktop.
This gist provides bulk selection; after bulk selection there appear a dropdown which shows count of selected resources and an action button that submit to the form Update/Delete/ etc.
<script type="text/javascript">
// JAVASCRIPT
function bind_bulk_toggle(resource_toggle_id, form_id) {
return $(resource_toggle_id).click(function() {
if ($(resource_toggle_id).is(':checked')) {
$(form_id + ' input:checkbox').prop('checked', true);
return $(form_id + ' input:checkbox').each(function() {
return $(this).val($(this).parents('tr').find('.selected_ids').val());
});
} else {
$(form_id + ' input:checkbox').prop('checked', false);
return $(form_id + ' input:checkbox').each(function() {
return $(this).val(0);
});
}
});
}
function bind_bulk_checkboxes(form_id, count_class) {
return $('body').on('click', form_id + ' input:checkbox', function() {
checked_count = $('input[class='+ count_class + ']:checked').length
if ($(this).is(':checked')) {
toggle_checkbox_selection_text(checked_count, form_id, '.bulk-remove-edit-update-action');
return $(this).val($(this).parents('tr').find('.selected_ids').val());
} else {
toggle_checkbox_selection_text(checked_count, '.bulk-remove-edit-update-action');
if (checked_count == 0) {
toggle_checkbox_deselection_text(form_id, '.bulk-remove-edit-update-action');
};
return $(this).val(0);
}
});
};
function toggle_checkbox_selection_text(count, form_id, klass) {
items = [{
selected_count: count,
selected_resource: $(form_id + ' ' + klass + ' a').text()
}];
bulk_select_dropdown = $('script[data-template="bulk_select_dropdown"]').text().split(/\$\{(.+?)\}/g);
$(form_id + ' ' + klass).html(items.map(function(item) {
return bulk_select_dropdown.map(render(item)).join('');
}));
$(document).foundation();
};
function toggle_checkbox_deselection_text(form_id, klass) {
items = [{
selected_resource: $(form_id + ' ' + klass + ' a').text()
}];
var bulk_deselect_dropdown = $('script[data-template="bulk_deselect_dropdown"]').text().split(/\$\{(.+?)\}/g);
$(form_id + ' ' + klass).html(items.map(function(item) {
return bulk_deselect_dropdown.map(render(item)).join('');
}));
}
// MAIN FUNCTION
function bind_bulk_resource_table_checkbox_toggle() {
bind_bulk_toggle('#bulk_toggle_resource_id', '#bulk_form')
bind_bulk_checkboxes('#form_id', 'resource_checkbox')
};
bind_bulk_resource_table_checkbox_toggle()
</script>
<!-- FORM -->
<%= form_tag(path), {id: 'bulk_form'}) do %>
<%= submit_tag 'Bulk Update' %>
<table>
<thead>
<tr>
<td><%= check_box_tag 'bulk_tag', 'yes', false, id: 'bulk_toggle_resource_id' %></td>
<td colspan="4" class='bulk-remove-edit-update-action'>
<a href="#">Selected Resources</a>
</td>
</tr>
</thead>
<tbody>
<% @resources.each do |resource| %>
<td>
<!-- ONLY SELECTED RESOURCE IDS -->
<%= check_box_tag 'selected_resources_ids[]', '0', class: 'resource_checkbox' %>
<!-- ALL RESOURCE IDS -->
<%= hidden_field_tag 'resources_ids[]', resource.id, class: 'selected_ids' %>
</td>
<% end %>
</tbody>
</table>
<% end %>
<!-- DATA TEMPLATE -->
<script type="text/template" data-template="bulk_select_dropdown">
<ul class="dropdown menu" data-dropdown-menu>
<li>
<a href="#">${selected_resource}</a>
<ul class="menu">
<li class='text-center'><%= t('selected') %>: ${selected_count}</li>
<hr>
<li class='text-center'><%= submit_tag t('remove'), class: 'button alert' %></li>
</ul>
</li>
</ul>
</script>
<script type="text/template" data-template="bulk_deselect_dropdown">
<td colspan="5"><a href="#">${selected_resource}</a></td>
</script>
@Sohair63
Copy link
Author

Sohair63 commented Aug 2, 2017

Before Selection:

screen shot 2017-08-02 at 11 32 23 am

After Selection:

screen shot 2017-08-02 at 11 32 36 am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment