datatables_api.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Datatables API
  4. *
  5. * Demo JS code for datatable_api.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var DatatableAPI = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Basic Datatable examples
  15. var _componentDatatableAPI = 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. // Single row selection
  37. var singleSelect = $('.datatable-selection-single').DataTable();
  38. $('.datatable-selection-single tbody').on('click', 'tr', function() {
  39. if ($(this).hasClass('table-success')) {
  40. $(this).removeClass('table-success');
  41. }
  42. else {
  43. singleSelect.$('tr.table-success').removeClass('table-success');
  44. $(this).addClass('table-success');
  45. }
  46. });
  47. // Multiple rows selection
  48. $('.datatable-selection-multiple').DataTable();
  49. $('.datatable-selection-multiple tbody').on('click', 'tr', function() {
  50. $(this).toggleClass('table-success');
  51. });
  52. // Individual column searching with text inputs
  53. $('.datatable-column-search-inputs tfoot td').not(':last-child').each(function () {
  54. var title = $('.datatable-column-search-inputs thead th').eq($(this).index()).text();
  55. $(this).html('<input type="text" class="form-control input-sm" placeholder="Search '+title+'" />');
  56. });
  57. var table = $('.datatable-column-search-inputs').DataTable();
  58. table.columns().every( function () {
  59. var that = this;
  60. $('input', this.footer()).on('keyup change', function () {
  61. that.search(this.value).draw();
  62. });
  63. });
  64. // Individual column searching with selects
  65. $('.datatable-column-search-selects').DataTable({
  66. initComplete: function () {
  67. this.api().columns().every(function() {
  68. var column = this;
  69. var select = $('<select class="form-control filter-select" data-placeholder="Filter"><option value=""></option></select>')
  70. .appendTo($(column.footer()).not(':last-child').empty())
  71. .on('change', function() {
  72. var val = $.fn.dataTable.util.escapeRegex(
  73. $(this).val()
  74. );
  75. column
  76. .search( val ? '^'+val+'$' : '', true, false )
  77. .draw();
  78. });
  79. column.data().unique().sort().each(function (d, j) {
  80. select.append('<option value="'+d.replace(/<(?:.|\n)*?>/gm, '')+'">'+d.replace(/<(?:.|\n)*?>/gm, '')+'</option>')
  81. });
  82. });
  83. }
  84. });
  85. };
  86. // Select2 for length menu styling
  87. var _componentSelect2 = function() {
  88. if (!$().select2) {
  89. console.warn('Warning - select2.min.js is not loaded.');
  90. return;
  91. }
  92. // Initialize
  93. $('.dataTables_length select').select2({
  94. minimumResultsForSearch: Infinity,
  95. dropdownAutoWidth: true,
  96. width: 'auto'
  97. });
  98. // Enable Select2 select for individual column searching
  99. $('.filter-select').select2();
  100. };
  101. //
  102. // Return objects assigned to module
  103. //
  104. return {
  105. init: function() {
  106. _componentDatatableAPI();
  107. _componentSelect2();
  108. }
  109. }
  110. }();
  111. // Initialize module
  112. // ------------------------------
  113. document.addEventListener('DOMContentLoaded', function() {
  114. DatatableAPI.init();
  115. });