Answer the question
In order to leave comments, you need to log in
How to make line numbering in datatables jquery server-side?
Good afternoon! It is necessary that the lines are numbered from the first and further. I'm using datatables 1.10. Here is a snippet from the server:
$columns = array(
array( 'db' => 'id', 'dt' => 'id' ),
array( 'db' => 'card_number', 'dt' => 'card_number' ),
array(
'db' => 'time_add',
'dt' => 'date_add',
'formatter' => function( $d, $row ) {
return date( 'd.m.Y', $d);
}
),
array('db' => 'username', 'dt' => 'expert'),
array(
'db' => 'id',
'dt' => 'actions',
'formatter' => function( $d, $row ) {
return "<a href=\"$d\">Удалить</a>";
}
)
);
$(document).ready(function() {
$('#example').dataTable( {
"aaSorting": [],
"fnDrawCallback": function ( oSettings ) {
/* Need to redo the counters if filtered or sorted */
if ( oSettings.bSorted || oSettings.bFiltered ) {
for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ) {
this.fnUpdate( i+1, oSettings.aiDisplay[i], 0, false, false );
}
}
},
"aoColumnDefs": [
{ "bSortable": false, "sClass": "index", "aTargets": [ 0 ] }
],
"processing": true,
"serverSide": true,
"ajax": "scripts/objects.php",
"columns": [
{ "data": "id" },
{ "data": "card_number" },
{ "data": "date_add" },
{ "data": "expert" },
{ "data": "actions" }
]
} );
} );
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
var index = (iDisplayIndexFull + 1);
$("td:first", nRow).html(index);
return nRow;
},
Answer the question
In order to leave comments, you need to log in
Do not give numbering from the server. Do it only on the client. DataTables seems to be able to do this.
For numbering lines in one of the projects, they used something like this code:
"fnRowCallback": function(oSettings) {
var table = $('#example').dataTable(), // получаем таблицу
rows = table.fnGetNodes(); // получаем все строки, а не только на текущей страниц
$(rows).each(function () {
$(this).find('td:first').text(this._DT_RowIndex + 1);
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question