examples.datatables.row.with.details.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. (function( $ ) {
  2. 'use strict';
  3. var datatableInit = function() {
  4. var $table = $('#datatable-details');
  5. // format function for row details
  6. var fnFormatDetails = function( datatable, tr ) {
  7. var data = datatable.fnGetData( tr );
  8. return [
  9. '<table class="table mb-none">',
  10. '<tr class="b-top-none">',
  11. '<td><label class="mb-none">Rendering engine:</label></td>',
  12. '<td>' + data[1]+ ' ' + data[4] + '</td>',
  13. '</tr>',
  14. '<tr>',
  15. '<td><label class="mb-none">Link to source:</label></td>',
  16. '<td>Could provide a link here</td>',
  17. '</tr>',
  18. '<tr>',
  19. '<td><label class="mb-none">Extra info:</label></td>',
  20. '<td>And any further details here (images etc)</td>',
  21. '</tr>',
  22. '</div>'
  23. ].join('');
  24. };
  25. // insert the expand/collapse column
  26. var th = document.createElement( 'th' );
  27. var td = document.createElement( 'td' );
  28. td.innerHTML = '<i data-toggle class="fa fa-plus-square-o text-primary h5 m-none" style="cursor: pointer;"></i>';
  29. td.className = "text-center";
  30. $table
  31. .find( 'thead tr' ).each(function() {
  32. this.insertBefore( th, this.childNodes[0] );
  33. });
  34. $table
  35. .find( 'tbody tr' ).each(function() {
  36. this.insertBefore( td.cloneNode( true ), this.childNodes[0] );
  37. });
  38. // initialize
  39. var datatable = $table.dataTable({
  40. aoColumnDefs: [{
  41. bSortable: false,
  42. aTargets: [ 0 ]
  43. }],
  44. aaSorting: [
  45. [1, 'asc']
  46. ]
  47. });
  48. // add a listener
  49. $table.on('click', 'i[data-toggle]', function() {
  50. var $this = $(this),
  51. tr = $(this).closest( 'tr' ).get(0);
  52. if ( datatable.fnIsOpen(tr) ) {
  53. $this.removeClass( 'fa-minus-square-o' ).addClass( 'fa-plus-square-o' );
  54. datatable.fnClose( tr );
  55. } else {
  56. $this.removeClass( 'fa-plus-square-o' ).addClass( 'fa-minus-square-o' );
  57. datatable.fnOpen( tr, fnFormatDetails( datatable, tr), 'details' );
  58. }
  59. });
  60. };
  61. $(function() {
  62. datatableInit();
  63. });
  64. }).apply( this, [ jQuery ]);