table-advanced.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var TableAdvanced = function () {
  2. var initTable1 = function() {
  3. /* Formating function for row details */
  4. function fnFormatDetails ( oTable, nTr )
  5. {
  6. var aData = oTable.fnGetData( nTr );
  7. var sOut = '<table>';
  8. sOut += '<tr><td>Platform(s):</td><td>'+aData[2]+'</td></tr>';
  9. sOut += '<tr><td>Engine version:</td><td>'+aData[3]+'</td></tr>';
  10. sOut += '<tr><td>CSS grade:</td><td>'+aData[4]+'</td></tr>';
  11. sOut += '<tr><td>Others:</td><td>Could provide a link here</td></tr>';
  12. sOut += '</table>';
  13. return sOut;
  14. }
  15. /*
  16. * Insert a 'details' column to the table
  17. */
  18. var nCloneTh = document.createElement( 'th' );
  19. var nCloneTd = document.createElement( 'td' );
  20. nCloneTd.innerHTML = '<span class="row-details row-details-close"></span>';
  21. $('#sample_1 thead tr').each( function () {
  22. this.insertBefore( nCloneTh, this.childNodes[0] );
  23. } );
  24. $('#sample_1 tbody tr').each( function () {
  25. this.insertBefore( nCloneTd.cloneNode( true ), this.childNodes[0] );
  26. } );
  27. /*
  28. * Initialse DataTables, with no sorting on the 'details' column
  29. */
  30. var oTable = $('#sample_1').dataTable( {
  31. "aoColumnDefs": [
  32. {"bSortable": false, "aTargets": [ 0 ] }
  33. ],
  34. "aaSorting": [[1, 'asc']],
  35. "aLengthMenu": [
  36. [5, 15, 20, -1],
  37. [5, 15, 20, "All"] // change per page values here
  38. ],
  39. // set the initial value
  40. "iDisplayLength": 10,
  41. });
  42. jQuery('#sample_1_wrapper .dataTables_filter input').addClass("m-wrap small"); // modify table search input
  43. jQuery('#sample_1_wrapper .dataTables_length select').addClass("m-wrap small"); // modify table per page dropdown
  44. jQuery('#sample_1_wrapper .dataTables_length select').select2(); // initialzie select2 dropdown
  45. /* Add event listener for opening and closing details
  46. * Note that the indicator for showing which row is open is not controlled by DataTables,
  47. * rather it is done here
  48. */
  49. $('#sample_1').on('click', ' tbody td .row-details', function () {
  50. var nTr = $(this).parents('tr')[0];
  51. if ( oTable.fnIsOpen(nTr) )
  52. {
  53. /* This row is already open - close it */
  54. $(this).addClass("row-details-close").removeClass("row-details-open");
  55. oTable.fnClose( nTr );
  56. }
  57. else
  58. {
  59. /* Open this row */
  60. $(this).addClass("row-details-open").removeClass("row-details-close");
  61. oTable.fnOpen( nTr, fnFormatDetails(oTable, nTr), 'details' );
  62. }
  63. });
  64. }
  65. var initTable2 = function() {
  66. var oTable = $('#sample_2').dataTable( {
  67. "aoColumnDefs": [
  68. { "aTargets": [ 0 ] }
  69. ],
  70. "aaSorting": [[1, 'asc']],
  71. "aLengthMenu": [
  72. [5, 15, 20, -1],
  73. [5, 15, 20, "All"] // change per page values here
  74. ],
  75. // set the initial value
  76. "iDisplayLength": 10,
  77. });
  78. jQuery('#sample_2_wrapper .dataTables_filter input').addClass("m-wrap small"); // modify table search input
  79. jQuery('#sample_2_wrapper .dataTables_length select').addClass("m-wrap small"); // modify table per page dropdown
  80. jQuery('#sample_2_wrapper .dataTables_length select').select2(); // initialzie select2 dropdown
  81. $('#sample_2_column_toggler input[type="checkbox"]').change(function(){
  82. /* Get the DataTables object again - this is not a recreation, just a get of the object */
  83. var iCol = parseInt($(this).attr("data-column"));
  84. var bVis = oTable.fnSettings().aoColumns[iCol].bVisible;
  85. oTable.fnSetColumnVis(iCol, (bVis ? false : true));
  86. });
  87. }
  88. return {
  89. //main function to initiate the module
  90. init: function () {
  91. if (!jQuery().dataTable) {
  92. return;
  93. }
  94. initTable1();
  95. initTable2();
  96. }
  97. };
  98. }();