DataTables example - Index column

A fairly common requirement for highly interactive tables which are displayed on the web is to have a column which with a 'counter' for the row number. This column should not be sortable, and will change dynamically as the ordering and searching applied to the table is altered by the end user.

This example shows how this can be achieved with DataTables, where the first column is the counter column, and is updated when ordering or searching occurs. This is done by listening for the order and search events emitted by the table. When these events are detected the column().nodes() method is used to get the TD/TH nodes for the target column and the each() helper function used to iterate over each, which have their contents updated as needed. Note that the filter and order options are using in the column() method to get the nodes in the current order and with the currently applied filter.

Name Position Office Age Salary
Name Position Office Age Salary
Tiger Nixon System Architect Edinburgh 61 $3,120
Garrett Winters Director Edinburgh 63 $5,300
Ashton Cox Technical Author San Francisco 66 $4,800
Cedric Kelly Javascript Developer Edinburgh 22 $3,600
Jenna Elliott Financial Controller Edinburgh 33 $5,300
Brielle Williamson Integration Specialist New York 61 $4,525
Herrod Chandler Sales Assistant San Francisco 59 $4,080
Rhona Davidson Integration Specialist Edinburgh 55 $6,730
Colleen Hurst Javascript Developer San Francisco 39 $5,000
Sonya Frost Software Engineer Edinburgh 23 $3,600
Jena Gaines System Architect London 30 $5,000
Quinn Flynn Financial Controller Edinburgh 22 $4,200
Charde Marshall Regional Director San Francisco 36 $5,300
Haley Kennedy Senior Marketing Designer London 43 $4,800
Tatyana Fitzpatrick Regional Director London 19 $2,875
Michael Silva Senior Marketing Designer London 66 $3,750
Paul Byrd Javascript Developer New York 64 $5,000
Gloria Little Systems Administrator New York 59 $3,120
Bradley Greer Software Engineer London 41 $3,120
Dai Rios System Architect Edinburgh 35 $4,200
Jenette Caldwell Financial Controller New York 30 $4,965
Yuri Berry System Architect New York 40 $3,600
Caesar Vance Technical Author New York 21 $4,965
Doris Wilder Sales Assistant Edinburgh 23 $4,965
Angelica Ramos System Architect London 36 $2,875
Gavin Joyce Developer Edinburgh 42 $4,525
Jennifer Chang Regional Director London 28 $4,080
Brenden Wagner Software Engineer San Francisco 18 $3,750
Ebony Grimes Software Engineer San Francisco 48 $2,875
Russell Chavez Director Edinburgh 20 $3,600
Michelle House Integration Specialist Edinburgh 37 $3,750
Suki Burks Developer London 53 $2,875
Prescott Bartlett Technical Author London 27 $6,730
Gavin Cortez Technical Author San Francisco 22 $6,730
Martena Mccray Integration Specialist Edinburgh 46 $4,080
Unity Butler Senior Marketing Designer San Francisco 47 $3,750
Howard Hatfield Financial Controller San Francisco 51 $4,080
Hope Fuentes Financial Controller San Francisco 41 $4,200
Vivian Harrell System Architect San Francisco 62 $4,965
Timothy Mooney Financial Controller London 37 $4,200
Jackson Bradshaw Director New York 65 $5,000
Miriam Weiss Support Engineer Edinburgh 64 $4,965
Bruno Nash Software Engineer London 38 $4,200
Odessa Jackson Support Engineer Edinburgh 37 $3,600
Thor Walton Developer New York 61 $3,600
Finn Camacho Support Engineer San Francisco 47 $4,800
Elton Baldwin Data Coordinator Edinburgh 64 $6,730
Zenaida Frank Software Engineer New York 63 $4,800
Zorita Serrano Software Engineer San Francisco 56 $5,300
Jennifer Acosta Javascript Developer Edinburgh 43 $2,875
Cara Stevens Sales Assistant New York 46 $4,800
Hermione Butler Director London 47 $4,080
Lael Greer Systems Administrator London 21 $3,120
Jonas Alexander Developer San Francisco 30 $5,300
Shad Decker Regional Director Edinburgh 51 $5,300
Michael Bruce Javascript Developer Edinburgh 29 $4,080
Donna Snider System Architect New York 27 $3,120

The Javascript shown below is used to initialise the table shown in this example:

$(document).ready(function() { var t = $('#example').DataTable( { "columnDefs": [ { "searchable": false, "orderable": false, "targets": 0 } ], "order": [[ 1, 'asc' ]] } ); t.on( 'order.dt search.dt', function () { t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) { cell.innerHTML = i+1; } ); } ).draw(); } );

In addition to the above code, the following Javascript library files are loaded for use in this example:

The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:

This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:

The following CSS library files are loaded for use in this example to provide the styling of the table:

This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.