I found many examples but not "as low-level" as I'd wish.
I can print all the rows with this:
var r = table.rows( { selected: true } )
for(i=0; i<r.count(); i) {
console.log('data:' table.row(r[i]).data())
}
This would be ALMOST perfect, but it prints all the cells separated by a comma. Why is it wrong? Because it doesn't escape the commas in the cells in any way, so if there is a comma, I have no way to know if it's a cell separator or an innocent comma.
I tried to get the first column with
var r = table.rows( { selected: true } )
for(i=0; i<r.count(); i) {
console.log('data2:' table.row( r[i] ).cells(0).data())
console.log('data3:' table.row( r[i] ).cells(0))
}
And while there is no error, they give back an Object that I cannot find the properties of.
I wish there could be a method like
console.log('table.row( r[i] ).cells(0).toString())
but I couldn't find any. Even better, I wish I could have a table.cells[4,3] :) :) :)
CodePudding user response:
you can find data in selected row with below block of code -
Example 1 :
var table = $('#example').DataTable();
$('#example tbody').on( 'click', 'tr', function () {
//Below Line of Code will give full line of selected on clicked row in datatable
console.log( table.row( this ).data() );
//Below Line of Code will give column data [0] mean first column
console.log( table.row( this ).data()[0] );
});
Another Code Block with printing of every column value or extracting it from row.
you can find data in selected row with below block of code -
Example 2 :
var table = $('#example').DataTable();
$('#example tbody').on( 'click', 'tr', function () {
//Get Columns Count
var count=$("#example thead tr th").length;
for(i=0;i<count;i )
{
//Below line of Code will print data of every column inside selected row.
// you can also use and array to collect column data.
console.log( table.row( this ).data()[i] );
}
});
Example 3 : This below code block will give you single / multiple selected rows with loop and another loop to get its columsn data its not using (this)
//Set Reference to Table Tag
var table = $('#example').DataTable();
//Get Selected Rows of Datatable one or multiple
var r=table.rows( { selected: true } );
//Get Columns Count
var count=$("#example thead tr").length;
for(i=0;i<count;i )
{
//Below line of Code will print data of every row
console.log(r.data()[i]);
//To Get Each Cell Data Inside Row use below code block
var cell_count=$("#example thead tr th").length;
//Loop on cells inside row
for(cell=0; cell < cell_count; cell )
{
//Get Cell Inside Row
var _cell=r.data()[i];
$.each( _cell, function( key, value ) {
console.log( "Cell " key " : " value );
});
}
}
Selected Rows in Jquery Datatable :