DataTables row select example

Preamble

It can be quite useful at times to provide the user with the option to select rows in a DataTable. This can be done using the API functions that DataTables provides. The example below uses the fnRowCallback() function to add a 'click' listener to each row, which will highlight the required row when selected. The indexes of the selected rows are then provided through the custom function fnGetSelected() for later processing.

Live example

Rendering engine Browser Platform(s) Engine version CSS grade
Trident Internet Explorer 4.0 Win 95+ 4 X
Trident Internet Explorer 5.0 Win 95+ 5 C
Trident Internet Explorer 5.5 Win 95+ 5.5 A
Trident Internet Explorer 6 Win 98+ 6 A
Trident Internet Explorer 7 Win XP SP2+ 7 A
Trident AOL browser (AOL desktop) Win XP 6 A
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
Gecko Mozilla 1.1 Win 95+ / OSX.1+ 1.1 A
Gecko Mozilla 1.2 Win 95+ / OSX.1+ 1.2 A
Gecko Mozilla 1.3 Win 95+ / OSX.1+ 1.3 A
Gecko Mozilla 1.4 Win 95+ / OSX.1+ 1.4 A
Gecko Mozilla 1.5 Win 95+ / OSX.1+ 1.5 A
Gecko Mozilla 1.6 Win 95+ / OSX.1+ 1.6 A
Gecko Mozilla 1.7 Win 98+ / OSX.1+ 1.7 A
Gecko Mozilla 1.8 Win 98+ / OSX.1+ 1.8 A
Gecko Seamonkey 1.1 Win 98+ / OSX.2+ 1.8 A
Gecko Epiphany 2.20 Gnome 1.8 A
Webkit Safari 1.2 OSX.3 125.5 A
Webkit Safari 1.3 OSX.3 312.8 A
Webkit Safari 2.0 OSX.4+ 419.3 A
Webkit Safari 3.0 OSX.4+ 522.1 A
Webkit OmniWeb 5.5 OSX.4+ 420 A
Webkit iPod Touch / iPhone iPod 420.1 A
Webkit S60 S60 413 A
Presto Opera 7.0 Win 95+ / OSX.1+ - A
Presto Opera 7.5 Win 95+ / OSX.2+ - A
Presto Opera 8.0 Win 95+ / OSX.2+ - A
Presto Opera 8.5 Win 95+ / OSX.2+ - A
Presto Opera 9.0 Win 95+ / OSX.3+ - A
Presto Opera 9.2 Win 88+ / OSX.3+ - A
Presto Opera 9.5 Win 88+ / OSX.3+ - A
Presto Opera for Wii Wii - A
Presto Nokia N800 N800 - A
Presto Nintendo DS browser Nintendo DS 8.5 C/A1
KHTML Konqureror 3.1 KDE 3.1 3.1 C
KHTML Konqureror 3.3 KDE 3.3 3.3 A
KHTML Konqureror 3.5 KDE 3.5 3.5 A
Tasman Internet Explorer 4.5 Mac OS 8-9 - X
Tasman Internet Explorer 5.1 Mac OS 7.6-9 1 C
Tasman Internet Explorer 5.2 Mac OS 8-X 1 C
Misc NetFront 3.1 Embedded devices - C
Misc NetFront 3.4 Embedded devices - A
Misc Dillo 0.8 Embedded devices - X
Misc Links Text only - X
Misc Lynx Text only - X
Misc IE Mobile Windows Mobile 6 - C
Misc PSP browser PSP - C
Other browsers All others - - U
Rendering engine Browser Platform(s) Engine version CSS grade

Initialisation code

var oTable;

$(document).ready(function() {
	/*
	 * Append an indicator element to each row - this will be changed dynamically depending
	 * on whether or not the row is selected
	 */
	var nCloneTd = document.createElement( 'td' );
	nCloneTd.appendChild( document.createTextNode( '0' ) );
	
	var nCloneTh = document.createElement( 'th' );
	nCloneTh.appendChild( document.createTextNode( '0' ) );
	
	$('#example tbody tr').each( function () {
		this.appendChild( nCloneTd.cloneNode( true ) );
	} );
	
	$('#example thead tr').each( function () {
		this.appendChild( nCloneTh.cloneNode( true ) );
	} );
	
	/*
	 * Build the table
	 */
	oTable = $('#example').dataTable( {
		"aoColumns": [ 
			null,
			null,
			null,
			null,
			null,
			{
				"bVisible": false,
				"bSearchable": false,
				"bSortable": false
			}
		],
		"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
			/* Deal with a click on each row */
			$(nRow).click( function() {
				/* Check if we are adding or removing a row */
				if ( aData[5] == 1 )
				{
					aData[5] = 0;
				}
				else
				{
					aData[5] = 1;
				}
				
				/* Change the class based on selection */
				this.className = (aData[5] == 1) ?
					this.className+'_selected' :
				 	this.className.replace( /_selected/, "" );
			} );
			
			/* For each draw - set the class based on the stored data */
			nRow.className = (aData[5] == 1) ?
				nRow.className+'_selected' :
			 	nRow.className.replace( /_selected/, "" );
			
			return nRow;
		}
	} );
} );

function fnGetSelected( oTableLocal )
{
	var aSelected = new Array();
	var aaData = oTableLocal.fnSettings().aaDataMaster;
	for ( var i=0 ; i<aaData.length ; i++ )
	{
		if ( aaData[i][5] == 1 )
		{
			aSelected.push( i );
		}
	}
	
	return aSelected;
}

Other examples

Basic initialisation

Advanced initialisation

API

Please refer to the DataTables documentation for full information about it's API properties and methods.