J.C
J.C

Reputation: 752

Jquery datatable when checkbox is checked, all the rows should be transfered to new jquery datatable

I have two datatable the first datatable when checkbox is checked the rows are transferred to a new jquery datatable. I also I have a chk all checkbox when the checkbox is checked I want all the table rows to be transfered to the other datatable.

Here are the two tables

var primaryTable = $('#tblProveedorToTransfert').DataTable({
            //code that generates datatable 
     });

var secondTable = $('#tblProveedorTransferd').DataTable({ 
     //code that generates datatable 
});

This is the code that should transfer all the rows to the other table. However only the rows that are visually visible are transferred and not all the rows on all the pages.

        $('#chkAll').click(function (e) {
        $(this).closest('table').find('td input:checkbox').prop('checked', this.checked);
            $('#tblProveedorToTransfert tbody').find('input[type="checkbox"]').each(function () {
                var alereadyAdded = false;

            if ($(this).is(":checked")) {
                for (var i = rowsIDs.length - 1; i >= 0; i--) {
                    if (rowsIDs[i] === $(this).attr("data-id"))
                    {
                        alereadyAdded = true;
                    }
                }
                if (alereadyAdded === false)
                {
                    rowsIDs.push($(this).attr("data-id"));
                }
            }
            else {
                for (var i = rowsIDs.length - 1; i >= 0; i--) {
                    if (rowsIDs[i] === $(this).attr("data-id")) {
                        rowsIDs.splice(i, 1);
                    }
                }
            }
            secondTable.ajax.reload();
            });

    });

What must I modify in my code to transfer all the rows and not just the visible ones. Thank you for your help.

Upvotes: 0

Views: 85

Answers (1)

VinhNT
VinhNT

Reputation: 1101

  1. Make another column in data source for your table, to storing the checkbox status (for eg: requestTransfer)
  2. attach event onclicked with every checkbox, whenever the checkbox status changed, update the new added column/field from the step 1.
  3. When rendering the checkbox, load the value of that field, and render the checkbox status
  4. When do copying, check the new added column to see if the row is needed to copy or not.

Upvotes: 1

Related Questions