datatables_responsive.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Responsive extension for Datatables
  4. *
  5. * Demo JS code for datatable_responsive.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var DatatableResponsive = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Basic Datatable examples
  15. var _componentDatatableResponsive = 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. responsive: true,
  24. columnDefs: [{
  25. orderable: false,
  26. width: 100,
  27. targets: [ 5 ]
  28. }],
  29. dom: '<"datatable-header"fl><"datatable-scroll-wrap"t><"datatable-footer"ip>',
  30. language: {
  31. search: '<span>Filter:</span> _INPUT_',
  32. searchPlaceholder: 'Type to filter...',
  33. lengthMenu: '<span>Show:</span> _MENU_',
  34. paginate: { 'first': 'First', 'last': 'Last', 'next': $('html').attr('dir') == 'rtl' ? '&larr;' : '&rarr;', 'previous': $('html').attr('dir') == 'rtl' ? '&rarr;' : '&larr;' }
  35. }
  36. });
  37. // Basic responsive configuration
  38. $('.datatable-responsive').DataTable();
  39. // Column controlled child rows
  40. $('.datatable-responsive-column-controlled').DataTable({
  41. responsive: {
  42. details: {
  43. type: 'column'
  44. }
  45. },
  46. columnDefs: [
  47. {
  48. className: 'control',
  49. orderable: false,
  50. targets: 0
  51. },
  52. {
  53. width: "100px",
  54. targets: [6]
  55. },
  56. {
  57. orderable: false,
  58. targets: [6]
  59. }
  60. ],
  61. order: [1, 'asc']
  62. });
  63. // Control position
  64. $('.datatable-responsive-control-right').DataTable({
  65. responsive: {
  66. details: {
  67. type: 'column',
  68. target: -1
  69. }
  70. },
  71. columnDefs: [
  72. {
  73. className: 'control',
  74. orderable: false,
  75. targets: -1
  76. },
  77. {
  78. width: "100px",
  79. targets: [5]
  80. },
  81. {
  82. orderable: false,
  83. targets: [5]
  84. }
  85. ]
  86. });
  87. // Whole row as a control
  88. $('.datatable-responsive-row-control').DataTable({
  89. responsive: {
  90. details: {
  91. type: 'column',
  92. target: 'tr'
  93. }
  94. },
  95. columnDefs: [
  96. {
  97. className: 'control',
  98. orderable: false,
  99. targets: 0
  100. },
  101. {
  102. width: "100px",
  103. targets: [6]
  104. },
  105. {
  106. orderable: false,
  107. targets: [6]
  108. }
  109. ],
  110. order: [1, 'asc']
  111. });
  112. };
  113. // Select2 for length menu styling
  114. var _componentSelect2 = function() {
  115. if (!$().select2) {
  116. console.warn('Warning - select2.min.js is not loaded.');
  117. return;
  118. }
  119. // Initialize
  120. $('.dataTables_length select').select2({
  121. minimumResultsForSearch: Infinity,
  122. dropdownAutoWidth: true,
  123. width: 'auto'
  124. });
  125. };
  126. //
  127. // Return objects assigned to module
  128. //
  129. return {
  130. init: function() {
  131. _componentDatatableResponsive();
  132. _componentSelect2();
  133. }
  134. }
  135. }();
  136. // Initialize module
  137. // ------------------------------
  138. document.addEventListener('DOMContentLoaded', function() {
  139. DatatableResponsive.init();
  140. });