Winsanity
Winsanity

Reputation: 61

How to get selected values of checkboxes in DataTable of different pagination pages PHP and javascript?

Basically I have a dataTable that displays all the records from the database, and a checkbox for each row within the table. My table has multiple pages. The problem was that I can only get the checkbox values of the current page selected because the system can only see checkboxes that are visible. I want to get specific checked values or all checked values from different pages and push them to the array. How can I achieve this? I can only get the checked value of current selected page in datatable atm. I have searched and tried similar problem, but it's still not working.

Below is my code

PHP code to display checkboxes

foreach($stmt as $row){echo "<tr><td>"."<input type='checkbox' name='check_list' id='checklist' value='$row['id']' style='width:20px; height: 20px'/>"}   

Javascript code

function getCheckboxVals(){

    var check = $('input[name="check_list"]:checked').length;
    var mycheckboxes = new Array();

    //IF NO CHECKBOX CHECKS
    if(check == 0){
        eModal.alert('PLEASE SELECT ISSUES BEFORE YOU EXPORT CONTENTS!!!');
        return false;
    }

    //IF ONE OR MORE CHECKBOXES CHECK
    if(check == 1){
        $('input[name="check_list"]:checked', table.fnGetNodes()).serialize().each(function(){

        mycheckboxes.push(this.value);

    });
}else{
    $('input[name="check_list"]:checked').each(function(){

        mycheckboxes.push(this.value);

    });
}

}

Upvotes: 3

Views: 3146

Answers (1)

user6688183
user6688183

Reputation:

The issue with only being able to get the values of the check boxes on the current page comes from the use of DOM methods or querying without using the DataTables' API directly. For efficiency, the DataTable only stores the values on the current page. In order to access other values you must use access the table using:

dataTable.rows().nodes().to$().find('input[type="checkbox"]').each(function(){}

The datatable in this instance being your defined datatable.

Additionally, here is a working version of checkbox values being stored and sent to the server

For more info see the DataTable FAQs and topics on DOM.

Upvotes: 1

Related Questions