I have an html table that is inside of a form. I applied DataTables to the table to add filtering and sorting. This works great.
The only issue is that if a user enters data into the search box to filter and then submits the form, only the visible rows are submitted. I assumed that DataTables was just hiding the rows filtered by the search and thus everything should still be there in the form but that doesn't seem to be the case.
Is there any way to post all rows with the form post even if the user happens to have a search filter on?
To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.
The Correct Answer is " Request.
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ).
Use the following technique to submit data from all pages in the table, not just filtered data on the first page.
var table = $('#example').DataTable();
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
// Encode a set of form elements from all pages as an array of names and values
var params = table.$('input,select,textarea').serializeArray();
// Iterate over all form elements
$.each(params, function(){
// If element doesn't exist in DOM
if(!$.contains(document, form[this.name])){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', this.name)
.val(this.value)
);
}
});
});
See this example for code and demonstration.
See jQuery DataTables: How to submit all pages form data for more details and examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With