DataTables server-side processing with pipelining example

# Preamble

When using server-side processing with DataTables, it can be quite intensive on your server having an Ajax call every time the user performs some kind of interaction - you can effectively DDOS your server with your own application!

This example shows how you might over-come this by modifying the request set to the server to retrieve more information than is actually required for a single page's display. This means that the user can page multiple times (5 times the display size is the default) before a request must be made of the server. Paging is typically the most common interaction performed with a DataTable, so this can be most beneficial to your server's resource usage. Of course the pipeline must be cleared for interactions other than paging (sorting, filtering etc), but that's the trade off that can be made (sending extra information is cheap - while another XHR is expensive).

# Live example

Rendering engine | Browser | Platform(s) | Engine version | CSS grade
--- | --- | --- | --- | ---
Loading data from server
Rendering engine | Browser | Platform(s) | Engine version | CSS grade

# Initialisation code

    var oCache = {
    	iCacheLower: -1
    };

    function fnSetKey( aoData, sKey, mValue )
    {
    	for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
    	{
    		if ( aoData[i].name == sKey )
    		{
    			aoData[i].value = mValue;
    		}
    	}
    }

    function fnGetKey( aoData, sKey )
    {
    	for ( var i=0, iLen=aoData.length ; i<iLen ; i++ )
    	{
    		if ( aoData[i].name == sKey )
    		{
    			return aoData[i].value;
    		}
    	}
    	return null;
    }

    function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
    	var iPipe = 5; /* Ajust the pipe size */

    	var bNeedServer = false;
    	var sEcho = fnGetKey(aoData, "sEcho");
    	var iRequestStart = fnGetKey(aoData, "iDisplayStart");
    	var iRequestLength = fnGetKey(aoData, "iDisplayLength");
    	var iRequestEnd = iRequestStart + iRequestLength;
    	oCache.iDisplayStart = iRequestStart;

    	/* outside pipeline? */
    	if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
    	{
    		bNeedServer = true;
    	}

    	/* sorting etc changed? */
    	if ( oCache.lastRequest && !bNeedServer )
    	{
    		for( var i=0, iLen=aoData.length ; i<iLen ; i++ )
    		{
    			if ( aoData[i].name != "iDisplayStart" && aoData[i].name != "iDisplayLength" && aoData[i].name != "sEcho" )
    			{
    				if ( aoData[i].value != oCache.lastRequest[i].value )
    				{
    					bNeedServer = true;
    					break;
    				}
    			}
    		}
    	}

    	/* Store the request for checking next time around */
    	oCache.lastRequest = aoData.slice();

    	if ( bNeedServer )
    	{
    		if ( iRequestStart < oCache.iCacheLower )
    		{
    			iRequestStart = iRequestStart - (iRequestLength*(iPipe-1));
    			if ( iRequestStart < 0 )
    			{
    				iRequestStart = 0;
    			}
    		}

    		oCache.iCacheLower = iRequestStart;
    		oCache.iCacheUpper = iRequestStart + (iRequestLength * iPipe);
    		oCache.iDisplayLength = fnGetKey( aoData, "iDisplayLength" );
    		fnSetKey( aoData, "iDisplayStart", iRequestStart );
    		fnSetKey( aoData, "iDisplayLength", iRequestLength*iPipe );

    		$.getJSON( sSource, aoData, function (json) {
    			/* Callback processing */
    			oCache.lastJson = jQuery.extend(true, {}, json);

    			if ( oCache.iCacheLower != oCache.iDisplayStart )
    			{
    				json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
    			}
    			json.aaData.splice( oCache.iDisplayLength, json.aaData.length );

    			fnCallback(json)
    		} );
    	}
    	else
    	{
    		json = jQuery.extend(true, {}, oCache.lastJson);
    		json.sEcho = sEcho; /* Update the echo for each response */
    		json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
    		json.aaData.splice( iRequestLength, json.aaData.length );
    		fnCallback(json);
    		return;
    	}
    }

    $(document).ready(function() {
    	$('#example').dataTable( {
    		"bProcessing": true,
    		"bServerSide": true,
    		"sAjaxSource": "scripts/server_processing.php",
    		"fnServerData": fnDataTablesPipeline
    	} );
    } );

# Server response

The code below shows the latest JSON data that has been returned from the server in response to the Ajax request made by DataTables. This will update as further requests are made.

# Other examples

## Basic initialisation

- Zero configuration
- Feature enablement
- Sorting data
- Multi-column sorting
- Multiple tables
- Hidden columns
- Complex headers - grouping with colspan
- DOM positioning
- Flexible table width
- State saving
- Alternative pagination styles
- Scrolling:
  Horizontal / Vertical / Both / Themed / Infinite
- Change language information (internationalisation)
- ThemeRoller themes (Smoothness)

## Advanced initialisation

- Events:
  Live events / Pre-init / Post-init
- Column rendering
- Sorting without HTML tags
- Multiple table controls (sDom)
- Defining length menu options
- Complex headers and hidden columns
- Custom toolbar (element) around table
- Row highlighting with CSS
- Row grouping
- Row callback
- Footer callback
- Control sorting direction of columns
- Change language information from a file (internationalisation)
- Setting defaults
- State saving with localStorage
- Custom events

## API

- Dynamically add a new row
- Individual column filtering (using "input" elements)
- Individual column filtering (using "select" elements)
- Highlight rows and columns
- Show and hide details about a particular record
- User selectable rows (multiple rows)
- User selectable rows (single row) and delete rows
- Editable rows (with jEditable)
- Submit form with elements in table
- Index column (static number column)
- Show and hide columns dynamically
- API function use in initialisation object (callback)
- DataTables scrolling and tabs
- Regular expression filtering

## Data sources

- DOM
- Javascript array
- Ajax source
- Server side processing

## Server-side processing

- Obtain server-side data
- Add extra HTTP variables
- Use HTTP POST
- Automatic addition of IDs and classes to rows
- Reading table data from objects
- Show and hide details about a particular record
- User selectable rows (multiple rows)
- JSONP for a cross domain data source
- jEditable integration with DataTables
- Deferred loading of Ajax data
- Pipelining data (reduce Ajax calls for paging)

## Ajax data source

- Ajax sourced data (array of arrays)
- Ajax sourced data (array of objects)
- Deferred DOM creation for extra speed
- Empty data source columns
- Use a data source other than aaData (the default)
- Read column data from sub-arrays
- Read column data from deeply nested properties

## Plug-ins

- Add custom API functions
- Sorting and automatic type detection
- Sorting without automatic type detection
- Custom pagination controls
- Range filtering / custom filtering
- Live DOM sorting
- Automatic HTML type detection