Get Index column to keep value (Rank column)
Get Index column to keep value (Rank column)
I am using datatables v1.10 which has improved a lot over time. However i am trying to add an index number.
So far i have managed to add the index number but i can't make a record keep it's original index number after search.
Let's say i have this table:
POSITION NAME
0 Tennis
1 Football
2 Basketball
if i would search on Basketball i should get POSITION 2 back like this:
POSITION NAME
2 Basketball
But rather than giving position 2, datatables start counting all over again at 0 (or 1 in my case because i made it start at 1)
I would like the index number to retain its correct value after search.
Last time i used:
"fnDrawCallback": function ( oSettings ) {
if ( oSettings.bSorted || oSettings.bFiltered ) {
for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ){
$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
}
}
},
I tried to convert this to their new api but i can not find a correct workaround.
This is the current server-side code i am using:
require('ssp.class.php');
$result=SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns );
$start=$_REQUEST['start'] + 1;
foreach($result['data'] as &$res){
$res["id"]=(string)$start;
$start++;
}
echo json_encode($result);
and client-side:
$(document).ready(function() {
var t = $('#table-data').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "index.php",
"type": "GET"
},
"columns": [
{"data": "position"},
{"data": "name"},
]
});
});
My issue is similar to: http://stackoverflow.com/questions/12117145/make-jquery-datatables-keep-the-row-number-assigned-when-searching-sorting
I tried the code suggested there but it doesn't seem to work in datatables v1.10 (i changed the correct api namings)
codepen:
http://codepen.io/anon/pen/Jsilb
original topic can be found on stackoverflow: http://stackoverflow.com/questions/25441615/index-number-on-search-should-keep-its-original-value-not-restart