row_details.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">
  6. <meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">
  7. <title>DataTables example - Row details</title>
  8. <link rel="stylesheet" type="text/css" href="../../media/css/jquery.dataTables.css">
  9. <link rel="stylesheet" type="text/css" href="../resources/syntax/shCore.css">
  10. <link rel="stylesheet" type="text/css" href="../resources/demo.css">
  11. <style type="text/css" class="init">
  12. td.details-control {
  13. background: url('../resources/details_open.png') no-repeat center center;
  14. cursor: pointer;
  15. }
  16. tr.details td.details-control {
  17. background: url('../resources/details_close.png') no-repeat center center;
  18. }
  19. </style>
  20. <script type="text/javascript" language="javascript" src="../../media/js/jquery.js"></script>
  21. <script type="text/javascript" language="javascript" src="../../media/js/jquery.dataTables.js"></script>
  22. <script type="text/javascript" language="javascript" src="../resources/syntax/shCore.js"></script>
  23. <script type="text/javascript" language="javascript" src="../resources/demo.js"></script>
  24. <script type="text/javascript" language="javascript" class="init">
  25. function format ( d ) {
  26. return 'Full name: '+d.first_name+' '+d.last_name+'<br>'+
  27. 'Salary: '+d.salary+'<br>'+
  28. 'The child row can contain any data you wish, including links, images, inner tables etc.';
  29. }
  30. $(document).ready(function() {
  31. var dt = $('#example').DataTable( {
  32. "processing": true,
  33. "serverSide": true,
  34. "ajax": "scripts/ids-objects.php",
  35. "columns": [
  36. {
  37. "class": "details-control",
  38. "orderable": false,
  39. "data": null,
  40. "defaultContent": ""
  41. },
  42. { "data": "first_name" },
  43. { "data": "last_name" },
  44. { "data": "position" },
  45. { "data": "office" }
  46. ],
  47. "order": [[1, 'asc']]
  48. } );
  49. // Array to track the ids of the details displayed rows
  50. var detailRows = [];
  51. $('#example tbody').on( 'click', 'tr td:first-child', function () {
  52. var tr = $(this).closest('tr');
  53. var row = dt.row( tr );
  54. var idx = $.inArray( tr.attr('id'), detailRows );
  55. if ( row.child.isShown() ) {
  56. tr.removeClass( 'details' );
  57. row.child.hide();
  58. // Remove from the 'open' array
  59. detailRows.splice( idx, 1 );
  60. }
  61. else {
  62. tr.addClass( 'details' );
  63. row.child( format( row.data() ) ).show();
  64. // Add to the 'open' array
  65. if ( idx === -1 ) {
  66. detailRows.push( tr.attr('id') );
  67. }
  68. }
  69. } );
  70. // On each draw, loop over the `detailRows` array and show any child rows
  71. dt.on( 'draw', function () {
  72. $.each( detailRows, function ( i, id ) {
  73. $('#'+id+' td:first-child').trigger( 'click' );
  74. } );
  75. } );
  76. } );
  77. </script>
  78. </head>
  79. <body class="dt-example">
  80. <div class="container">
  81. <section>
  82. <h1>DataTables example <span>Row details</span></h1>
  83. <div class="info">
  84. <p>This example shows the use of DataTables' ability to show and hide child rows which are attached to
  85. a parent row in the host table. This is often used to show additional information about a row,
  86. particularly when you wish to convey more information about a row than there is space for in the host
  87. table.</p>
  88. <p>The example below shows server-side processing being used with the first column having an event
  89. listener attached to it which will toggle the child row's display. This is set up using <a href=
  90. "//datatables.net/reference/option/columns.data"><code class="option" title=
  91. "DataTables initialisation option">columns.data<span>DT</span></code></a> and <a href=
  92. "//datatables.net/reference/option/columns.defaultContent"><code class="option" title=
  93. "DataTables initialisation option">columns.defaultContent<span>DT</span></code></a>, in combination
  94. with CSS to show an empty cell with a background image which can be clicked upon.</p>
  95. <p>The event handler makes use of the <a href="//datatables.net/reference/api/row().child"><code class=
  96. "api" title="DataTables API method">row().child<span>DT</span></code></a> methods to firstly check if a
  97. row is already displayed, and if so hide it, if not show it. The content of the child row is, in this
  98. example, defined by the <code>formatDetails()</code> function, but you would replace that with whatever
  99. you wanted to show the content required, possibly including, for example, an Ajax call to the server to
  100. obtain the extra information to show. Note that the format details function has access to the full data
  101. source object for the row, including information that is not actually shown in the table (the salary
  102. parameter for example).</p>
  103. <p>Furthermore, this example shows a small difference from the <a href=
  104. "../api/row_details.html">client-side row details example</a> in that to have rows automatically reopen
  105. when the table is redrawn, we need to track a unique identifier for each row - in this case the row
  106. <code>id</code>. This is required because in server-side processing mode rows are automatically
  107. destroyed and recreated on each draw.</p>
  108. </div>
  109. <table id="example" class="display" cellspacing="0" width="100%">
  110. <thead>
  111. <tr>
  112. <th></th>
  113. <th>First name</th>
  114. <th>Last name</th>
  115. <th>Position</th>
  116. <th>Office</th>
  117. </tr>
  118. </thead>
  119. <tfoot>
  120. <tr>
  121. <th></th>
  122. <th>First name</th>
  123. <th>Last name</th>
  124. <th>Position</th>
  125. <th>Office</th>
  126. </tr>
  127. </tfoot>
  128. </table>
  129. <ul class="tabs">
  130. <li class="active">Javascript</li>
  131. <li>HTML</li>
  132. <li>CSS</li>
  133. <li>Ajax</li>
  134. <li>Server-side script</li>
  135. </ul>
  136. <div class="tabs">
  137. <div class="js">
  138. <p>The Javascript shown below is used to initialise the table shown in this
  139. example:</p><code class="multiline brush: js;">function format ( d ) {
  140. return 'Full name: '+d.first_name+' '+d.last_name+'&lt;br&gt;'+
  141. 'Salary: '+d.salary+'&lt;br&gt;'+
  142. 'The child row can contain any data you wish, including links, images, inner tables etc.';
  143. }
  144. $(document).ready(function() {
  145. var dt = $('#example').DataTable( {
  146. &quot;processing&quot;: true,
  147. &quot;serverSide&quot;: true,
  148. &quot;ajax&quot;: &quot;scripts/ids-objects.php&quot;,
  149. &quot;columns&quot;: [
  150. {
  151. &quot;class&quot;: &quot;details-control&quot;,
  152. &quot;orderable&quot;: false,
  153. &quot;data&quot;: null,
  154. &quot;defaultContent&quot;: &quot;&quot;
  155. },
  156. { &quot;data&quot;: &quot;first_name&quot; },
  157. { &quot;data&quot;: &quot;last_name&quot; },
  158. { &quot;data&quot;: &quot;position&quot; },
  159. { &quot;data&quot;: &quot;office&quot; }
  160. ],
  161. &quot;order&quot;: [[1, 'asc']]
  162. } );
  163. // Array to track the ids of the details displayed rows
  164. var detailRows = [];
  165. $('#example tbody').on( 'click', 'tr td:first-child', function () {
  166. var tr = $(this).closest('tr');
  167. var row = dt.row( tr );
  168. var idx = $.inArray( tr.attr('id'), detailRows );
  169. if ( row.child.isShown() ) {
  170. tr.removeClass( 'details' );
  171. row.child.hide();
  172. // Remove from the 'open' array
  173. detailRows.splice( idx, 1 );
  174. }
  175. else {
  176. tr.addClass( 'details' );
  177. row.child( format( row.data() ) ).show();
  178. // Add to the 'open' array
  179. if ( idx === -1 ) {
  180. detailRows.push( tr.attr('id') );
  181. }
  182. }
  183. } );
  184. // On each draw, loop over the `detailRows` array and show any child rows
  185. dt.on( 'draw', function () {
  186. $.each( detailRows, function ( i, id ) {
  187. $('#'+id+' td:first-child').trigger( 'click' );
  188. } );
  189. } );
  190. } );</code>
  191. <p>In addition to the above code, the following Javascript library files are loaded for use in this
  192. example:</p>
  193. <ul>
  194. <li><a href="../../media/js/jquery.js">../../media/js/jquery.js</a></li>
  195. <li><a href="../../media/js/jquery.dataTables.js">../../media/js/jquery.dataTables.js</a></li>
  196. </ul>
  197. </div>
  198. <div class="table">
  199. <p>The HTML shown below is the raw HTML table element, before it has been enhanced by
  200. DataTables:</p>
  201. </div>
  202. <div class="css">
  203. <div>
  204. <p>This example uses a little bit of additional CSS beyond what is loaded from the library
  205. files (below), in order to correctly display the table. The additional CSS used is shown
  206. below:</p><code class="multiline brush: js;">td.details-control {
  207. background: url('../resources/details_open.png') no-repeat center center;
  208. cursor: pointer;
  209. }
  210. tr.details td.details-control {
  211. background: url('../resources/details_close.png') no-repeat center center;
  212. }</code>
  213. </div>
  214. <p>The following CSS library files are loaded for use in this example to provide the styling of the
  215. table:</p>
  216. <ul>
  217. <li><a href=
  218. "../../media/css/jquery.dataTables.css">../../media/css/jquery.dataTables.css</a></li>
  219. </ul>
  220. </div>
  221. <div class="ajax">
  222. <p>This table loads data by Ajax. The latest data that has been loaded is shown below. This data
  223. will update automatically as any additional data is loaded.</p>
  224. </div>
  225. <div class="php">
  226. <p>The script used to perform the server-side processing for this table is shown below. Please note
  227. that this is just an example script using PHP. Server-side processing scripts can be written in any
  228. language, using <a href="//datatables.net/manual/server-side">the protocol described in the
  229. DataTables documentation</a>.</p>
  230. </div>
  231. </div>
  232. </section>
  233. </div>
  234. <section>
  235. <div class="footer">
  236. <div class="gradient"></div>
  237. <div class="liner">
  238. <h2>Other examples</h2>
  239. <div class="toc">
  240. <div class="toc-group">
  241. <h3><a href="../basic_init/index.html">Basic initialisation</a></h3>
  242. <ul class="toc">
  243. <li><a href="../basic_init/zero_configuration.html">Zero configuration</a></li>
  244. <li><a href="../basic_init/filter_only.html">Feature enable / disable</a></li>
  245. <li><a href="../basic_init/table_sorting.html">Default ordering (sorting)</a></li>
  246. <li><a href="../basic_init/multi_col_sort.html">Multi-column ordering</a></li>
  247. <li><a href="../basic_init/multiple_tables.html">Multiple tables</a></li>
  248. <li><a href="../basic_init/hidden_columns.html">Hidden columns</a></li>
  249. <li><a href="../basic_init/complex_header.html">Complex headers (rowspan and
  250. colspan)</a></li>
  251. <li><a href="../basic_init/dom.html">DOM positioning</a></li>
  252. <li><a href="../basic_init/flexible_width.html">Flexible table width</a></li>
  253. <li><a href="../basic_init/state_save.html">State saving</a></li>
  254. <li><a href="../basic_init/alt_pagination.html">Alternative pagination</a></li>
  255. <li><a href="../basic_init/scroll_y.html">Scroll - vertical</a></li>
  256. <li><a href="../basic_init/scroll_x.html">Scroll - horizontal</a></li>
  257. <li><a href="../basic_init/scroll_xy.html">Scroll - horizontal and vertical</a></li>
  258. <li><a href="../basic_init/scroll_y_theme.html">Scroll - vertical with jQuery UI
  259. ThemeRoller</a></li>
  260. <li><a href="../basic_init/comma-decimal.html">Language - Comma decimal place</a></li>
  261. <li><a href="../basic_init/language.html">Language options</a></li>
  262. </ul>
  263. </div>
  264. <div class="toc-group">
  265. <h3><a href="../advanced_init/index.html">Advanced initialisation</a></h3>
  266. <ul class="toc">
  267. <li><a href="../advanced_init/events_live.html">DOM / jQuery events</a></li>
  268. <li><a href="../advanced_init/dt_events.html">DataTables events</a></li>
  269. <li><a href="../advanced_init/column_render.html">Column rendering</a></li>
  270. <li><a href="../advanced_init/length_menu.html">Page length options</a></li>
  271. <li><a href="../advanced_init/dom_multiple_elements.html">Multiple table control
  272. elements</a></li>
  273. <li><a href="../advanced_init/complex_header.html">Complex headers (rowspan /
  274. colspan)</a></li>
  275. <li><a href="../advanced_init/html5-data-attributes.html">HTML5 data-* attributes</a></li>
  276. <li><a href="../advanced_init/language_file.html">Language file</a></li>
  277. <li><a href="../advanced_init/defaults.html">Setting defaults</a></li>
  278. <li><a href="../advanced_init/row_callback.html">Row created callback</a></li>
  279. <li><a href="../advanced_init/row_grouping.html">Row grouping</a></li>
  280. <li><a href="../advanced_init/footer_callback.html">Footer callback</a></li>
  281. <li><a href="../advanced_init/dom_toolbar.html">Custom toolbar elements</a></li>
  282. <li><a href="../advanced_init/sort_direction_control.html">Order direction sequence
  283. control</a></li>
  284. </ul>
  285. </div>
  286. <div class="toc-group">
  287. <h3><a href="../styling/index.html">Styling</a></h3>
  288. <ul class="toc">
  289. <li><a href="../styling/display.html">Base style</a></li>
  290. <li><a href="../styling/no-classes.html">Base style - no styling classes</a></li>
  291. <li><a href="../styling/cell-border.html">Base style - cell borders</a></li>
  292. <li><a href="../styling/compact.html">Base style - compact</a></li>
  293. <li><a href="../styling/hover.html">Base style - hover</a></li>
  294. <li><a href="../styling/order-column.html">Base style - order-column</a></li>
  295. <li><a href="../styling/row-border.html">Base style - row borders</a></li>
  296. <li><a href="../styling/stripe.html">Base style - stripe</a></li>
  297. <li><a href="../styling/bootstrap.html">Bootstrap</a></li>
  298. <li><a href="../styling/foundation.html">Foundation</a></li>
  299. <li><a href="../styling/jqueryUI.html">jQuery UI ThemeRoller</a></li>
  300. </ul>
  301. </div>
  302. <div class="toc-group">
  303. <h3><a href="../data_sources/index.html">Data sources</a></h3>
  304. <ul class="toc">
  305. <li><a href="../data_sources/dom.html">HTML (DOM) sourced data</a></li>
  306. <li><a href="../data_sources/ajax.html">Ajax sourced data</a></li>
  307. <li><a href="../data_sources/js_array.html">Javascript sourced data</a></li>
  308. <li><a href="../data_sources/server_side.html">Server-side processing</a></li>
  309. </ul>
  310. </div>
  311. <div class="toc-group">
  312. <h3><a href="../api/index.html">API</a></h3>
  313. <ul class="toc">
  314. <li><a href="../api/add_row.html">Add rows</a></li>
  315. <li><a href="../api/multi_filter.html">Individual column searching (text inputs)</a></li>
  316. <li><a href="../api/multi_filter_select.html">Individual column searching (select
  317. inputs)</a></li>
  318. <li><a href="../api/highlight.html">Highlighting rows and columns</a></li>
  319. <li><a href="../api/row_details.html">Child rows (show extra / detailed
  320. information)</a></li>
  321. <li><a href="../api/select_row.html">Row selection (multiple rows)</a></li>
  322. <li><a href="../api/select_single_row.html">Row selection and deletion (single
  323. row)</a></li>
  324. <li><a href="../api/form.html">Form inputs</a></li>
  325. <li><a href="../api/counter_columns.html">Index column</a></li>
  326. <li><a href="../api/show_hide.html">Show / hide columns dynamically</a></li>
  327. <li><a href="../api/api_in_init.html">Using API in callbacks</a></li>
  328. <li><a href="../api/tabs_and_scrolling.html">Scrolling and jQuery UI tabs</a></li>
  329. <li><a href="../api/regex.html">Search API (regular expressions)</a></li>
  330. </ul>
  331. </div>
  332. <div class="toc-group">
  333. <h3><a href="../ajax/index.html">Ajax</a></h3>
  334. <ul class="toc">
  335. <li><a href="../ajax/simple.html">Ajax data source (arrays)</a></li>
  336. <li><a href="../ajax/objects.html">Ajax data source (objects)</a></li>
  337. <li><a href="../ajax/deep.html">Nested object data (objects)</a></li>
  338. <li><a href="../ajax/objects_subarrays.html">Nested object data (arrays)</a></li>
  339. <li><a href="../ajax/orthogonal-data.html">Orthogonal data</a></li>
  340. <li><a href="../ajax/null_data_source.html">Generated content for a column</a></li>
  341. <li><a href="../ajax/custom_data_property.html">Custom data source property</a></li>
  342. <li><a href="../ajax/custom_data_flat.html">Flat array data source</a></li>
  343. <li><a href="../ajax/defer_render.html">Deferred rendering for speed</a></li>
  344. </ul>
  345. </div>
  346. <div class="toc-group">
  347. <h3><a href="./index.html">Server-side</a></h3>
  348. <ul class="toc active">
  349. <li><a href="./simple.html">Server-side processing</a></li>
  350. <li><a href="./custom_vars.html">Custom HTTP variables</a></li>
  351. <li><a href="./post.html">POST data</a></li>
  352. <li><a href="./ids.html">Automatic addition of row ID attributes</a></li>
  353. <li><a href="./object_data.html">Object data source</a></li>
  354. <li class="active"><a href="./row_details.html">Row details</a></li>
  355. <li><a href="./select_rows.html">Row selection</a></li>
  356. <li><a href="./jsonp.html">JSONP data source for remote domains</a></li>
  357. <li><a href="./defer_loading.html">Deferred loading of data</a></li>
  358. <li><a href="./pipeline.html">Pipelining data to reduce Ajax calls for paging</a></li>
  359. </ul>
  360. </div>
  361. <div class="toc-group">
  362. <h3><a href="../plug-ins/index.html">Plug-ins</a></h3>
  363. <ul class="toc">
  364. <li><a href="../plug-ins/api.html">API plug-in methods</a></li>
  365. <li><a href="../plug-ins/sorting_auto.html">Ordering plug-ins (with type
  366. detection)</a></li>
  367. <li><a href="../plug-ins/sorting_manual.html">Ordering plug-ins (no type
  368. detection)</a></li>
  369. <li><a href="../plug-ins/range_filtering.html">Custom filtering - range search</a></li>
  370. <li><a href="../plug-ins/dom_sort.html">Live DOM ordering</a></li>
  371. </ul>
  372. </div>
  373. </div>
  374. <div class="epilogue">
  375. <p>Please refer to the <a href="http://www.datatables.net">DataTables documentation</a> for full
  376. information about its API properties and methods.<br>
  377. Additionally, there are a wide range of <a href="http://www.datatables.net/extras">extras</a> and
  378. <a href="http://www.datatables.net/plug-ins">plug-ins</a> which extend the capabilities of
  379. DataTables.</p>
  380. <p class="copyright">DataTables designed and created by <a href=
  381. "http://www.sprymedia.co.uk">SpryMedia Ltd</a> &#169; 2007-2014<br>
  382. DataTables is licensed under the <a href="http://www.datatables.net/mit">MIT license</a>.</p>
  383. </div>
  384. </div>
  385. </div>
  386. </section>
  387. </body>
  388. </html>