<?xml version="1.0" encoding="UTF-8" ?>
<dt-example table-type="ssp-details" order="6">

<css lib="datatables"><![CDATA[
td.details-control {
	background: url('../resources/details_open.png') no-repeat center center;
	cursor: pointer;
}
tr.details td.details-control {
	background: url('../resources/details_close.png') no-repeat center center;
}
]]></css>
<js lib="jquery datatables" />

<title>Row details</title>
<js><![CDATA[

function format ( d ) {
	return 'Full name: '+d.first_name+' '+d.last_name+'<br>'+
	    'Salary: '+d.salary+'<br>'+
		'The child row can contain any data you wish, including links, images, inner tables etc.';
}

$(document).ready(function() {
	var dt = $('#example').DataTable( {
		"processing": true,
		"serverSide": true,
		"ajax": "scripts/ids-objects.php",
		"columns": [ 
			{
				"class":          "details-control",
				"orderable":      false,
				"data":           null,
				"defaultContent": ""
			},
			{ "data": "first_name" },
			{ "data": "last_name" },
			{ "data": "position" },
			{ "data": "office" }
		],
		"order": [[1, 'asc']]
	} );

	// Array to track the ids of the details displayed rows
	var detailRows = [];

	$('#example tbody').on( 'click', 'tr td.details-control', function () {
		var tr = $(this).closest('tr');
		var row = dt.row( tr );
		var idx = $.inArray( tr.attr('id'), detailRows );

		if ( row.child.isShown() ) {
			tr.removeClass( 'details' );
			row.child.hide();

			// Remove from the 'open' array
			detailRows.splice( idx, 1 );
		}
		else {
			tr.addClass( 'details' );
			row.child( format( row.data() ) ).show();

			// Add to the 'open' array
			if ( idx === -1 ) {
				detailRows.push( tr.attr('id') );
			}
		}
	} );

	// On each draw, loop over the `detailRows` array and show any child rows
	dt.on( 'draw', function () {
		$.each( detailRows, function ( i, id ) {
			$('#'+id+' td.details-control').trigger( 'click' );
		} );
	} );
} );
]]></js>

<info><![CDATA[

This example shows the use of DataTables' ability to show and hide child rows which are attached to a parent row in the host table. This is often used to show additional information about a row, particularly when you wish to convey more information about a row than there is space for in the host table.

The example below shows server-side processing being used with the first column having an event listener attached to it which will toggle the child row's display. This is set up using `dt-init columns.data` and `dt-init columns.defaultContent`, in combination with CSS to show an empty cell with a background image which can be clicked upon.

The event handler makes use of the `dt-api row().child` methods to firstly check if a row is already displayed, and if so hide it, if not show it. The content of the child row is, in this example, defined by the `format()` function, but you would replace that with whatever you wanted to show the content required, possibly including, for example, an Ajax call to the server to obtain the extra information to show. Note that the format details function has access to the full data source object for the row, including information that is not actually shown in the table (the salary parameter for example).

Furthermore, this example shows a small difference from the [client-side row details example](../api/row_details.html) in that to have rows automatically reopen when the table is redrawn, we need to track a unique identifier for each row - in this case the row `id`. This is required because in server-side processing mode rows are automatically destroyed and recreated on each draw.

]]></info>

</dt-example>
