DataTables example Index column

Highly-interactive tables often require a 'counter' column that contains the position in the table for each row. 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.

NamePositionOfficeAgeSalary
NamePositionOfficeAgeSalary
Tiger NixonSystem ArchitectEdinburgh61$320,800
Garrett WintersAccountantTokyo63$170,750
Ashton CoxJunior Technical AuthorSan Francisco66$86,000
Cedric KellySenior Javascript DeveloperEdinburgh22$433,060
Airi SatouAccountantTokyo33$162,700
Brielle WilliamsonIntegration SpecialistNew York61$372,000
Herrod ChandlerSales AssistantSan Francisco59$137,500
Rhona DavidsonIntegration SpecialistTokyo55$327,900
Colleen HurstJavascript DeveloperSan Francisco39$205,500
Sonya FrostSoftware EngineerEdinburgh23$103,600
Jena GainesOffice ManagerLondon30$90,560
Quinn FlynnSupport LeadEdinburgh22$342,000
Charde MarshallRegional DirectorSan Francisco36$470,600
Haley KennedySenior Marketing DesignerLondon43$313,500
Tatyana FitzpatrickRegional DirectorLondon19$385,750
Michael SilvaMarketing DesignerLondon66$198,500
Paul ByrdChief Financial Officer (CFO)New York64$725,000
Gloria LittleSystems AdministratorNew York59$237,500
Bradley GreerSoftware EngineerLondon41$132,000
Dai RiosPersonnel LeadEdinburgh35$217,500
Jenette CaldwellDevelopment LeadNew York30$345,000
Yuri BerryChief Marketing Officer (CMO)New York40$675,000
Caesar VancePre-Sales SupportNew York21$106,450
Doris WilderSales AssistantSidney23$85,600
Angelica RamosChief Executive Officer (CEO)London47$1,200,000
Gavin JoyceDeveloperEdinburgh42$92,575
Jennifer ChangRegional DirectorSingapore28$357,650
Brenden WagnerSoftware EngineerSan Francisco28$206,850
Fiona GreenChief Operating Officer (COO)San Francisco48$850,000
Shou ItouRegional MarketingTokyo20$163,000
Michelle HouseIntegration SpecialistSidney37$95,400
Suki BurksDeveloperLondon53$114,500
Prescott BartlettTechnical AuthorLondon27$145,000
Gavin CortezTeam LeaderSan Francisco22$235,500
Martena MccrayPost-Sales supportEdinburgh46$324,050
Unity ButlerMarketing DesignerSan Francisco47$85,675
Howard HatfieldOffice ManagerSan Francisco51$164,500
Hope FuentesSecretarySan Francisco41$109,850
Vivian HarrellFinancial ControllerSan Francisco62$452,500
Timothy MooneyOffice ManagerLondon37$136,200
Jackson BradshawDirectorNew York65$645,750
Olivia LiangSupport EngineerSingapore64$234,500
Bruno NashSoftware EngineerLondon38$163,500
Sakura YamamotoSupport EngineerTokyo37$139,575
Thor WaltonDeveloperNew York61$98,540
Finn CamachoSupport EngineerSan Francisco47$87,500
Serge BaldwinData CoordinatorSingapore64$138,575
Zenaida FrankSoftware EngineerNew York63$125,250
Zorita SerranoSoftware EngineerSan Francisco56$115,000
Jennifer AcostaJunior Javascript DeveloperEdinburgh43$75,650
Cara StevensSales AssistantNew York46$145,600
Hermione ButlerRegional DirectorLondon47$356,250
Lael GreerSystems AdministratorLondon21$103,500
Jonas AlexanderDeveloperSan Francisco30$86,500
Shad DeckerRegional DirectorEdinburgh51$183,000
Michael BruceJavascript DeveloperSingapore29$183,000
Donna SniderCustomer SupportNew York27$112,000

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.

The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.