Checkboxes only work on first page - Datatables, rails

后端 未结 3 714
别那么骄傲
别那么骄傲 2021-01-12 10:55

Senario: So basically I\'m using DataTables and have checkboxes on its first column. My DataTables have multiple pages (pagination).

The Pro

3条回答
  •  温柔的废话
    2021-01-12 11:29

    after trying lots of way finally i found this sweet and simple way of solving this problem!!!! here #contect_form is form id ...you must put your data table inside a form coz on page load you can initialize datatable rows

         var table;
                    $(document).ready(function () {
                        table = $('#table_id').DataTable({
                            scrollY: "412px",
                            scrollX: true,
                            AutoWidth: false,
                            scrollCollapse: false,
                            paging: true,
    
                        });
                        $('#contact_form').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(
                                       $('')
                                          .attr('type', 'hidden')
                                          .attr('name', this.name)
                                          .val(this.value)
                                    );
                                }
                            });
                        });
    
    
                    });

提交回复
热议问题