2

I have a table working with jQuery DataTables and pagination

I have over 300 registrys which 50 are the max on each page. Every time I change from page to page the other 250 registrys are removed from DOM so when I submit and do the POST to my controller I only get a list of 50 models which are the ones on my current page.

How do I access the other 250, or tell the table to send them all? I have searched and I have read that this is the normal behavior of pagination. But how do you guys work around it?

3
  • OK...first a question. Why do you want the other 250? If you want all 300 just change your page size to 300. The idea of pagination is to reduce load time for the requested page. Commented Jul 14, 2015 at 22:01
  • Because i have a checkbox in each registry, depending on which ones are checked i do certain stuff. I did pagination so they didnt had to scroll that much. Thank you sir. Commented Jul 14, 2015 at 22:05
  • Post the model, view, and the controller. It will be much easier to offer a solution if we can see the code. Commented Jul 15, 2015 at 1:16

1 Answer 1

6

CAUSE

When using DataTables plug-in with pagination only current page rows exist in the DOM for performance and compatibility. Therefore when you submit the form, only current page form controls values are being submitted.

SOLUTION 1: Submit form directly

The trick is to turn form elements from pages other than current into <input type="hidden"> before submitting the form.

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 jQuery DataTables: How to submit all pages form data - Submit directly for more details and examples.

SOLUTION 2: Submit form via Ajax

Another solution is to submit the form via Ajax.

var table = $('#example').DataTable();

// Handle form submission event
$('#frm-example').on('submit', function(e){
   // Prevent actual form submission
   e.preventDefault();

   // Serialize form data
   var data = table.$('input,select,textarea').serialize();

   // Submit form data via Ajax
   $.ajax({
      url: '/echo/jsonp/',
      data: data,
      success: function(data){
         console.log('Server response', data);
      }
   });
});

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

NOTES

Please note that both solutions will work in client-side processing mode only.

LINKS

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Gyrocode, thanks for your help, now i get all the elements, altho i have a problem, this list of elements is an attribute of a View Model, how do i post all the other attributes and change a certain attribute with the list i am getting from the datatable?
Solution 1 worked like charm. I just did one minor change to select only checked checkboxes -> table.$("input[type='checkbox']").serializeArray(); So it works in Rails 5, exactly as I wanted it, with strong parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.