Last active
August 29, 2015 14:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}; |
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
It inserts many hidden inputs and not a single array of ids (or checkboxes values) so the server doesn't have to know whats happening at the view.
If you have an array in Javascript and submit it, you will receive the parameter on your server as a string with comma separated values.