The DataTables API has a number of methods for attaching child rows to a parent row in the DataTable. This can be used to show additional information about a row, useful for cases where you wish to convey more information about a row than there is space for in the host table.
The example below makes use of the row().child
methods to first check if a row is already displayed, and if so hide it (row().child.hide()
), otherwise show it (row().child.show()
). The content of the child row in this example is defined by the format()
function, but you
would replace that with whatever content you wanted to show, possibly including, for example, an Ajax call to
the server to obtain any extra information.
The Javascript shown below is used to initialise the table shown in this example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | /* Formatting function for row details - modify as you need */ function format ( d ) { // `d` is the original data object for the row return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' + '<tr>' + '<td>Full name:</td>' + '<td>' +d.name+ '</td>' + '</tr>' + '<tr>' + '<td>Extension number:</td>' + '<td>' +d.extn+ '</td>' + '</tr>' + '<tr>' + '<td>Extra info:</td>' + '<td>And any further details here (images etc)...</td>' + '</tr>' + '</table>' ; } $(document).ready( function () { var table = $( '#example' ).DataTable( { "ajax" : "../ajax/data/objects.txt" , "columns" : [ { "className" : 'details-control' , "orderable" : false , "data" : null , "defaultContent" : '' }, { "data" : "name" }, { "data" : "position" }, { "data" : "office" }, { "data" : "salary" } ], "order" : [[1, 'asc' ]] } ); // Add event listener for opening and closing details $( '#example tbody' ).on( 'click' , 'td.details-control' , function () { var tr = $( this ).closest( 'tr' ); var row = table.row( tr ); if ( row.child.isShown() ) { // This row is already open - close it row.child.hide(); tr.removeClass( 'shown' ); } else { // Open this row row.child( format(row.data()) ).show(); tr.addClass( 'shown' ); } } ); } ); |
In addition to the above code, the following Javascript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</tfoot>
</table><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div><div class="line number8 index7 alt1">8</div><div class="line number9 index8 alt2">9</div><div class="line number10 index9 alt1">10</div><div class="line number11 index10 alt2">11</div><div class="line number12 index11 alt1">12</div><div class="line number13 index12 alt2">13</div><div class="line number14 index13 alt1">14</div><div class="line number15 index14 alt2">15</div><div class="line number16 index15 alt1">16</div><div class="line number17 index16 alt2">17</div><div class="line number18 index17 alt1">18</div><div class="line number19 index18 alt2">19</div><div class="line number20 index19 alt1">20</div><div class="line number21 index20 alt2">21</div><div class="line number22 index21 alt1">22</div><div class="line number23 index22 alt2">23</div><div class="line number24 index23 alt1">24</div><div class="line number25 index24 alt2">25</div><div class="line number26 index25 alt1">26</div><div class="line number27 index26 alt2">27</div><div class="line number28 index27 alt1">28</div><div class="line number29 index28 alt2">29</div><div class="line number30 index29 alt1">30</div><div class="line number31 index30 alt2">31</div><div class="line number32 index31 alt1">32</div><div class="line number33 index32 alt2">33</div><div class="line number34 index33 alt1">34</div><div class="line number35 index34 alt2">35</div><div class="line number36 index35 alt1">36</div><div class="line number37 index36 alt2">37</div><div class="line number38 index37 alt1">38</div><div class="line number39 index38 alt2">39</div><div class="line number40 index39 alt1">40</div><div class="line number41 index40 alt2">41</div><div class="line number42 index41 alt1">42</div><div class="line number43 index42 alt2">43</div><div class="line number44 index43 alt1">44</div><div class="line number45 index44 alt2">45</div><div class="line number46 index45 alt1">46</div><div class="line number47 index46 alt2">47</div><div class="line number48 index47 alt1">48</div><div class="line number49 index48 alt2">49</div><div class="line number50 index49 alt1">50</div><div class="line number51 index50 alt2">51</div><div class="line number52 index51 alt1">52</div><div class="line number53 index52 alt2">53</div><div class="line number54 index53 alt1">54</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="js comments">/* Formatting function for row details - modify as you need */</code></div><div class="line number2 index1 alt1"><code class="js keyword">function</code> <code class="js plain">format ( d ) {</code></div><div class="line number3 index2 alt2"><code class="js spaces"> </code><code class="js comments">// `d` is the original data object for the row</code></div><div class="line number4 index3 alt1"><code class="js spaces"> </code><code class="js keyword">return</code> <code class="js string">'<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'</code><code class="js plain">+</code></div><div class="line number5 index4 alt2"><code class="js spaces"> </code><code class="js string">'<tr>'</code><code class="js plain">+</code></div><div class="line number6 index5 alt1"><code class="js spaces"> </code><code class="js string">'<td>Full name:</td>'</code><code class="js plain">+</code></div><div class="line number7 index6 alt2"><code class="js spaces"> </code><code class="js string">'<td>'</code><code class="js plain">+d.name+</code><code class="js string">'</td>'</code><code class="js plain">+</code></div><div class="line number8 index7 alt1"><code class="js spaces"> </code><code class="js string">'</tr>'</code><code class="js plain">+</code></div><div class="line number9 index8 alt2"><code class="js spaces"> </code><code class="js string">'<tr>'</code><code class="js plain">+</code></div><div class="line number10 index9 alt1"><code class="js spaces"> </code><code class="js string">'<td>Extension number:</td>'</code><code class="js plain">+</code></div><div class="line number11 index10 alt2"><code class="js spaces"> </code><code class="js string">'<td>'</code><code class="js plain">+d.extn+</code><code class="js string">'</td>'</code><code class="js plain">+</code></div><div class="line number12 index11 alt1"><code class="js spaces"> </code><code class="js string">'</tr>'</code><code class="js plain">+</code></div><div class="line number13 index12 alt2"><code class="js spaces"> </code><code class="js string">'<tr>'</code><code class="js plain">+</code></div><div class="line number14 index13 alt1"><code class="js spaces"> </code><code class="js string">'<td>Extra info:</td>'</code><code class="js plain">+</code></div><div class="line number15 index14 alt2"><code class="js spaces"> </code><code class="js string">'<td>And any further details here (images etc)...</td>'</code><code class="js plain">+</code></div><div class="line number16 index15 alt1"><code class="js spaces"> </code><code class="js string">'</tr>'</code><code class="js plain">+</code></div><div class="line number17 index16 alt2"><code class="js spaces"> </code><code class="js string">'</table>'</code><code class="js plain">;</code></div><div class="line number18 index17 alt1"><code class="js plain">}</code></div><div class="line number19 index18 alt2"> </div><div class="line number20 index19 alt1"><code class="js plain">$(document).ready(</code><code class="js keyword">function</code><code class="js plain">() {</code></div><div class="line number21 index20 alt2"><code class="js spaces"> </code><code class="js keyword">var</code> <code class="js plain">table = $(</code><code class="js string">'#example'</code><code class="js plain">).DataTable( {</code></div><div class="line number22 index21 alt1"><code class="js spaces"> </code><code class="js string">"ajax"</code><code class="js plain">: </code><code class="js string">"../ajax/data/objects.txt"</code><code class="js plain">,</code></div><div class="line number23 index22 alt2"><code class="js spaces"> </code><code class="js string">"columns"</code><code class="js plain">: [</code></div><div class="line number24 index23 alt1"><code class="js spaces"> </code><code class="js plain">{</code></div><div class="line number25 index24 alt2"><code class="js spaces"> </code><code class="js string">"className"</code><code class="js plain">: </code><code class="js string">'details-control'</code><code class="js plain">,</code></div><div class="line number26 index25 alt1"><code class="js spaces"> </code><code class="js string">"orderable"</code><code class="js plain">: </code><code class="js keyword">false</code><code class="js plain">,</code></div><div class="line number27 index26 alt2"><code class="js spaces"> </code><code class="js string">"data"</code><code class="js plain">: </code><code class="js keyword">null</code><code class="js plain">,</code></div><div class="line number28 index27 alt1"><code class="js spaces"> </code><code class="js string">"defaultContent"</code><code class="js plain">: </code><code class="js string">''</code></div><div class="line number29 index28 alt2"><code class="js spaces"> </code><code class="js plain">},</code></div><div class="line number30 index29 alt1"><code class="js spaces"> </code><code class="js plain">{ </code><code class="js string">"data"</code><code class="js plain">: </code><code class="js string">"name"</code> <code class="js plain">},</code></div><div class="line number31 index30 alt2"><code class="js spaces"> </code><code class="js plain">{ </code><code class="js string">"data"</code><code class="js plain">: </code><code class="js string">"position"</code> <code class="js plain">},</code></div><div class="line number32 index31 alt1"><code class="js spaces"> </code><code class="js plain">{ </code><code class="js string">"data"</code><code class="js plain">: </code><code class="js string">"office"</code> <code class="js plain">},</code></div><div class="line number33 index32 alt2"><code class="js spaces"> </code><code class="js plain">{ </code><code class="js string">"data"</code><code class="js plain">: </code><code class="js string">"salary"</code> <code class="js plain">}</code></div><div class="line number34 index33 alt1"><code class="js spaces"> </code><code class="js plain">],</code></div><div class="line number35 index34 alt2"><code class="js spaces"> </code><code class="js string">"order"</code><code class="js plain">: [[1, </code><code class="js string">'asc'</code><code class="js plain">]]</code></div><div class="line number36 index35 alt1"><code class="js spaces"> </code><code class="js plain">} );</code></div><div class="line number37 index36 alt2"><code class="js spaces"> </code> </div><div class="line number38 index37 alt1"><code class="js spaces"> </code><code class="js comments">// Add event listener for opening and closing details</code></div><div class="line number39 index38 alt2"><code class="js spaces"> </code><code class="js plain">$(</code><code class="js string">'#example tbody'</code><code class="js plain">).on(</code><code class="js string">'click'</code><code class="js plain">, </code><code class="js string">'td.details-control'</code><code class="js plain">, </code><code class="js keyword">function</code> <code class="js plain">() {</code></div><div class="line number40 index39 alt1"><code class="js spaces"> </code><code class="js keyword">var</code> <code class="js plain">tr = $(</code><code class="js keyword">this</code><code class="js plain">).closest(</code><code class="js string">'tr'</code><code class="js plain">);</code></div><div class="line number41 index40 alt2"><code class="js spaces"> </code><code class="js keyword">var</code> <code class="js plain">row = table.row( tr );</code></div><div class="line number42 index41 alt1"> </div><div class="line number43 index42 alt2"><code class="js spaces"> </code><code class="js keyword">if</code> <code class="js plain">( row.child.isShown() ) {</code></div><div class="line number44 index43 alt1"><code class="js spaces"> </code><code class="js comments">// This row is already open - close it</code></div><div class="line number45 index44 alt2"><code class="js spaces"> </code><code class="js plain">row.child.hide();</code></div><div class="line number46 index45 alt1"><code class="js spaces"> </code><code class="js plain">tr.removeClass(</code><code class="js string">'shown'</code><code class="js plain">);</code></div><div class="line number47 index46 alt2"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number48 index47 alt1"><code class="js spaces"> </code><code class="js keyword">else</code> <code class="js plain">{</code></div><div class="line number49 index48 alt2"><code class="js spaces"> </code><code class="js comments">// Open this row</code></div><div class="line number50 index49 alt1"><code class="js spaces"> </code><code class="js plain">row.child( format(row.data()) ).show();</code></div><div class="line number51 index50 alt2"><code class="js spaces"> </code><code class="js plain">tr.addClass(</code><code class="js string">'shown'</code><code class="js plain">);</code></div><div class="line number52 index51 alt1"><code class="js spaces"> </code><code class="js plain">}</code></div><div class="line number53 index52 alt2"><code class="js spaces"> </code><code class="js plain">} );</code></div><div class="line number54 index53 alt1"><code class="js plain">} );</code></div></div></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter"><div class="line number1 index0 alt2">1</div><div class="line number2 index1 alt1">2</div><div class="line number3 index2 alt2">3</div><div class="line number4 index3 alt1">4</div><div class="line number5 index4 alt2">5</div><div class="line number6 index5 alt1">6</div><div class="line number7 index6 alt2">7</div></td><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="css plain">td.details-control {</code></div><div class="line number2 index1 alt1"><code class="css spaces"> </code><code class="css keyword">background</code><code class="css plain">: </code><code class="css value">url</code><code class="css plain">(</code><code class="css string">'../resources/details_open.png'</code><code class="css plain">) </code><code class="css value">no-repeat</code> <code class="css value">center</code> <code class="css value">center</code><code class="css plain">;</code></div><div class="line number3 index2 alt2"><code class="css spaces"> </code><code class="css keyword">cursor</code><code class="css plain">: </code><code class="css value">pointer</code><code class="css plain">;</code></div><div class="line number4 index3 alt1"><code class="css plain">}</code></div><div class="line number5 index4 alt2"><code class="css plain">tr.shown td.details-control {</code></div><div class="line number6 index5 alt1"><code class="css spaces"> </code><code class="css keyword">background</code><code class="css plain">: </code><code class="css value">url</code><code class="css plain">(</code><code class="css string">'../resources/details_close.png'</code><code class="css plain">) </code><code class="css value">no-repeat</code> <code class="css value">center</code> <code class="css value">center</code><code class="css plain">;</code></div><div class="line number7 index6 alt2"><code class="css plain">}</code></div></div></td></tr></tbody></table>
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
1 2 3 4 5 6 7 | td.details-control { background : url ( '../resources/details_open.png' ) no-repeat center center ; cursor : pointer ; } tr.shown td.details-control { background : url ( '../resources/details_close.png' ) no-repeat center center ; } |
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.
Please refer to the DataTables documentation for full information about its API properties and methods.
Additionally, there are a wide range of extensions and plug-ins which extend the capabilities of DataTables.
DataTables designed and created by SpryMedia Ltd © 2007-2018
DataTables is licensed under the MIT license.