Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submitting all form data after filtering

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?

like image 214
leora Avatar asked Jul 13 '16 20:07

leora


People also ask

How can I send form data in POST request?

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.

How do you get information from a form that is submitted using the GET method?

The Correct Answer is " Request.

What is form method post action?

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" ).


1 Answers

SOLUTION

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)
         );
      } 
   });      
});

DEMO

See this example for code and demonstration.

LINKS

See jQuery DataTables: How to submit all pages form data for more details and examples.

like image 186
Gyrocode.com Avatar answered Oct 23 '22 04:10

Gyrocode.com