How to detect when all rows in a table are selected?

How to detect when all rows in a table are selected?

lpacelpace Posts: 10Questions: 5Answers: 0

I'd like to make a button and modal appear when all rows of my table are selected to prompt the user to submit the data to my server. Can you provide an example of detecting when all rows are selected?
my code is below, if(rmaTable.rows({"selected":true})) seems to work if ANY row is selected, not ALL.

var rmaTable = $('#rmas').DataTable({
  "ajax":
  {"url": "http://aomppapp.gmaom.us:9090/get_RMA?NAME_ID="+customerno,                        
  },
   "columns": [null,
     { "data": "Record.RECID" },
     { "data": "Record.QSHP" },
     { "data": "Record.PART_NO" },
     { "data": "Record.DESCRIPTION" },
     null,
   ],
  "columnDefs": [ {
            "orderable": false,
            "className": 'select-checkbox',
            "targets":   0,
            "data": null,
            "defaultContent":''
            
        },
        {
                "targets": -1,
                "data": null,
                "defaultContent": "<button class='button'type='button'>SELECT</button>",
        },],
        "select": {
            "style":    'multi',
            //"selector": 'td:first-child'
        },
  "paging":false
  
  
   
});
       
/************************************************************************************************************************
  Listen for keypresses from the RMA pickup input control.
  upon receiving key code for "Enter", compare the value to those in the dataTable #rmas.
  Mark the row in the table as "picked up" (check box) and add the barcode value to a JSON object.
*/   

$("#rma-pickups").change(function(){
var scanned = document.getElementById("rma-pickups").value;
//alert(scanned);
rmaTable.rows(':contains("'+scanned+'")').select();
document.getElementById("rma-pickups").value = '';
if(rmaTable.rows({"selected":true}))
{
 alert("all rows selected!");
}


}); //end .change callback function

Answers

  • kthorngrenkthorngren Posts: 20,307Questions: 26Answers: 4,769

    Maybe compare the length of rows() to rows() with the selector-modifier of {selected: true}. You can use the count() API to get the number of rows. Something like this:

    if( rmaTable.rows({"selected":true}).count() === rmaTable.rows().count()  )
    {
     alert("all rows selected!");
    }
    

    Kevin

Sign In or Register to comment.