Y
Y
Yuri Denisov2014-05-13 09:03:47
PHP
Yuri Denisov, 2014-05-13 09:03:47

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>";
    }
  )
  
);

here is a snippet of the output
$(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" }
      
      
    ]
  } );
} );

If you display a table that is not ready using PHP, then everything is ok. It is numbered, and after I poke into the pagination, the numbering continues. If I make an output using the server, then on the first page of the table the numbering is normal, as soon as I press the second - I see in the id column - not the number on the page, but the id from the database. Maybe somehow prepare the line number in php and send it? Tell me please.
PS I use the standard library from datatable to connect to the database.
PS If you do this
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                    var index = (iDisplayIndexFull + 1);
                    $("td:first", nRow).html(index);
                    return nRow;
    },

then the numbering is obtained on each page from one to 10

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Obrezkov, 2014-05-13
@ghaiklor

Do not give numbering from the server. Do it only on the client. DataTables seems to be able to do this.

D
Dmitry Tiunov, 2014-05-13
@Dizast

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 question

Ask a Question

731 491 924 answers to any question