DataTables - live events example

Preamble

Events assigned to the table can be exceptionally useful for user interaction, however you must be aware that DataTables will add and remove rows from the DOM as they are needed (i.e. when paging only the visible elements are actually available in the DOM). As such, this can lead to the odd hiccup when working with events. One of the best ways of dealing with this is through the use of live events, as shown in this example.

Live example

Rendering engineBrowserPlatform(s)Engine versionCSS grade
Rendering engineBrowserPlatform(s)Engine versionCSS grade
Gecko Firefox 1.0 Win 98+ / OSX.2+ 1.7 A
Gecko Firefox 1.5 Win 98+ / OSX.2+ 1.8 A
Gecko Firefox 2.0 Win 98+ / OSX.2+ 1.8 A
Gecko Firefox 3.0 Win 2k+ / OSX.3+ 1.9 A
Gecko Camino 1.0 OSX.2+ 1.8 A
Gecko Camino 1.5 OSX.3+ 1.8 A
Gecko Netscape 7.2 Win 95+ / Mac OS 8.6-9.2 1.7 A
Gecko Netscape Browser 8 Win 98SE+ 1.7 A
Gecko Netscape Navigator 9 Win 98+ / OSX.2+ 1.8 A
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
18
19
20
21
22
23
$(document).ready(function() {
    /* Init DataTables */
    $('#example').dataTable();
     
    /* Add events */
    $('#example tbody tr').live('click', function () {
        var sTitle;
        var nTds = $('td', this);
        var sBrowser = $(nTds[1]).text();
        var sGrade = $(nTds[4]).text();
         
        if ( sGrade == "A" )
            sTitle =  sBrowser+' will provide a first class (A) level of CSS support.';
        else if ( sGrade == "C" )
            sTitle = sBrowser+' will provide a core (C) level of CSS support.';
        else if ( sGrade == "X" )
            sTitle = sBrowser+' does not provide CSS support or has a broken implementation. Block CSS.';
        else
            sTitle = sBrowser+' will provide an undefined level of CSS support.';
         
        alert( sTitle )
    } );
} );

Other examples