Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MauricioMoraes/3cdbc02f08ee99bfb7d5 to your computer and use it in GitHub Desktop.
Save MauricioMoraes/3cdbc02f08ee99bfb7d5 to your computer and use it in GitHub Desktop.
Jquery DataTables - Workaround to have a selectable list of options with dataTables and Pagination. When the form is submitted, the script inserts hidden inputs with the values of the checkboxes that are not visible, due to pagination.
function setupDatatableCheckboxesWithPagination(tableSelector, attributeName) {
var dataTableObject = $(tableSelector).dataTable();
form = $(tableSelector).closest("form");
$(form).submit(function(){
insertHiddenInputsWithCheckboxesValues(tableSelector, attributeName);
dataTableObject.fnFilter('');
});
};
function insertHiddenInputsWithCheckboxesValues(tableSelector, attributeName) {
var dataTableObject = $(tableSelector).dataTable();
$("input:checked", dataTableObject.fnGetNodes()).map(function(i, checkbox) {
var hiddenInput = $('<input>').attr({
type: 'hidden',
name: attributeName,
value: checkbox.value
});
$(tableSelector).append(hiddenInput);
});
};
@MauricioMoraes
Copy link
Author

This can be adapted and used with any type of input fields. The list with checkboxes is the most common use.

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