DataTables row numbers example

Preamble

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 change dynamically as the sorting and filtering 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 sorting or filtering occurs. Also the first column is marked as un-sortable and initial sorting is applied only on the second column.

Live example

IndexRendering engineBrowserPlatform(s)Engine versionCSS grade
IndexRendering engineBrowserPlatform(s)Engine versionCSS grade
1 Gecko Firefox 1.0 Win 98+ / OSX.2+ 1.7 A
2 Gecko Firefox 1.5 Win 98+ / OSX.2+ 1.8 A
3 Gecko Firefox 2.0 Win 98+ / OSX.2+ 1.8 A
4 Gecko Firefox 3.0 Win 2k+ / OSX.3+ 1.9 A
5 Gecko Camino 1.0 OSX.2+ 1.8 A
6 Gecko Camino 1.5 OSX.3+ 1.8 A
7 Gecko Netscape 7.2 Win 95+ / Mac OS 8.6-9.2 1.7 A
8 Gecko Netscape Browser 8 Win 98SE+ 1.7 A
9 Gecko Netscape Navigator 9 Win 98+ / OSX.2+ 1.8 A
10 Gecko Mozilla 1.0 Win 95+ / OSX.1+ 1 A
Showing 1 to 10 of 57 entries

Initialisation code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$(document).ready(function() {
    $('#example').dataTable( {
        "fnDrawCallback": function ( oSettings ) {
            /* Need to redo the counters if filtered or sorted */
            if ( oSettings.bSorted || oSettings.bFiltered )
            {
                this.$('td:first-child', {"filter":"applied"}).each( function (i) {
                    that.fnUpdate( i+1, this.parentNode, 0, false, false );
                } );
            }
        },
        "aoColumnDefs": [
            { "bSortable": false, "aTargets": [ 0 ] }
        ],
        "aaSorting": [[ 1, 'asc' ]]
    } );
} );

Other examples