datatables_advanced.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Advanced datatables
  4. *
  5. * Demo JS code for datatable_advanced.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var DatatableAdvanced = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Basic Datatable examples
  15. var _componentDatatableAdvanced = function() {
  16. if (!$().DataTable) {
  17. console.warn('Warning - datatables.min.js is not loaded.');
  18. return;
  19. }
  20. // Setting datatable defaults
  21. $.extend( $.fn.dataTable.defaults, {
  22. autoWidth: false,
  23. columnDefs: [{
  24. orderable: false,
  25. width: 100,
  26. targets: [ 5 ]
  27. }],
  28. dom: '<"datatable-header"fl><"datatable-scroll"t><"datatable-footer"ip>',
  29. language: {
  30. search: '<span>Filter:</span> _INPUT_',
  31. searchPlaceholder: 'Type to filter...',
  32. lengthMenu: '<span>Show:</span> _MENU_',
  33. paginate: { 'first': 'First', 'last': 'Last', 'next': $('html').attr('dir') == 'rtl' ? '&larr;' : '&rarr;', 'previous': $('html').attr('dir') == 'rtl' ? '&rarr;' : '&larr;' }
  34. }
  35. });
  36. // Datatable 'length' options
  37. $('.datatable-show-all').DataTable({
  38. lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]]
  39. });
  40. // DOM positioning
  41. $('.datatable-dom-position').DataTable({
  42. dom: '<"datatable-header length-left"lp><"datatable-scroll"t><"datatable-footer info-right"fi>',
  43. });
  44. // Highlighting rows and columns on mouseover
  45. var lastIdx = null;
  46. var table = $('.datatable-highlight').DataTable();
  47. $('.datatable-highlight tbody').on('mouseover', 'td', function() {
  48. var colIdx = table.cell(this).index().column;
  49. if (colIdx !== lastIdx) {
  50. $(table.cells().nodes()).removeClass('active');
  51. $(table.column(colIdx).nodes()).addClass('active');
  52. }
  53. }).on('mouseleave', function() {
  54. $(table.cells().nodes()).removeClass('active');
  55. });
  56. // Columns rendering
  57. $('.datatable-columns').dataTable({
  58. columnDefs: [
  59. {
  60. // The `data` parameter refers to the data for the cell (defined by the
  61. // `data` option, which defaults to the column being worked with, in
  62. // this case `data: 0`.
  63. render: function (data, type, row) {
  64. return data +' ('+ row[3]+')';
  65. },
  66. targets: 0
  67. },
  68. { visible: false, targets: [ 3 ] }
  69. ]
  70. });
  71. };
  72. // Select2 for length menu styling
  73. var _componentSelect2 = function() {
  74. if (!$().select2) {
  75. console.warn('Warning - select2.min.js is not loaded.');
  76. return;
  77. }
  78. // Initialize
  79. $('.dataTables_length select').select2({
  80. minimumResultsForSearch: Infinity,
  81. dropdownAutoWidth: true,
  82. width: 'auto'
  83. });
  84. };
  85. //
  86. // Return objects assigned to module
  87. //
  88. return {
  89. init: function() {
  90. _componentDatatableAdvanced();
  91. _componentSelect2();
  92. }
  93. }
  94. }();
  95. // Initialize module
  96. // ------------------------------
  97. document.addEventListener('DOMContentLoaded', function() {
  98. DatatableAdvanced.init();
  99. });