You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.5 KiB

4 years ago
  1. /*! DataTables Bootstrap 3 integration
  2. * ©2011-2014 SpryMedia Ltd - datatables.net/license
  3. */
  4. /**
  5. * DataTables integration for Bootstrap 3. This requires Bootstrap 3 and
  6. * DataTables 1.10 or newer.
  7. *
  8. * This file sets the defaults and adds options to DataTables to style its
  9. * controls using Bootstrap. See http://datatables.net/manual/styling/bootstrap
  10. * for further information.
  11. */
  12. (function (window, document, undefined) {
  13. var factory = function ($, DataTable) {
  14. "use strict";
  15. /* Set the defaults for DataTables initialisation */
  16. $.extend(true, DataTable.defaults, {
  17. dom:
  18. "<'row'<'col-sm-6'l><'col-sm-6'f>>" +
  19. "<'row'<'col-sm-12'tr>>" +
  20. "<'row'<'col-sm-6'i><'col-sm-6'p>>",
  21. renderer: 'bootstrap'
  22. });
  23. /* Default class modification */
  24. $.extend(DataTable.ext.classes, {
  25. sWrapper: "dataTables_wrapper form-inline dt-bootstrap",
  26. sFilterInput: "form-control input-sm",
  27. sLengthSelect: "form-control input-sm"
  28. });
  29. /* Bootstrap paging button renderer */
  30. DataTable.ext.renderer.pageButton.bootstrap = function (settings, host, idx, buttons, page, pages) {
  31. var api = new DataTable.Api(settings);
  32. var classes = settings.oClasses;
  33. var lang = settings.oLanguage.oPaginate;
  34. var btnDisplay, btnClass;
  35. var attach = function (container, buttons) {
  36. var i, ien, node, button;
  37. var clickHandler = function (e) {
  38. e.preventDefault();
  39. if (!$(e.currentTarget).hasClass('disabled')) {
  40. api.page(e.data.action).draw(false);
  41. }
  42. };
  43. for (i = 0, ien = buttons.length; i < ien; i++) {
  44. button = buttons[i];
  45. if ($.isArray(button)) {
  46. attach(container, button);
  47. }
  48. else {
  49. btnDisplay = '';
  50. btnClass = '';
  51. switch (button) {
  52. case 'ellipsis':
  53. btnDisplay = '&hellip;';
  54. btnClass = 'disabled';
  55. break;
  56. case 'first':
  57. btnDisplay = lang.sFirst;
  58. btnClass = button + (page > 0 ?
  59. '' : ' disabled');
  60. break;
  61. case 'previous':
  62. btnDisplay = lang.sPrevious;
  63. btnClass = button + (page > 0 ?
  64. '' : ' disabled');
  65. break;
  66. case 'next':
  67. btnDisplay = lang.sNext;
  68. btnClass = button + (page < pages - 1 ?
  69. '' : ' disabled');
  70. break;
  71. case 'last':
  72. btnDisplay = lang.sLast;
  73. btnClass = button + (page < pages - 1 ?
  74. '' : ' disabled');
  75. break;
  76. default:
  77. btnDisplay = button + 1;
  78. btnClass = page === button ?
  79. 'active' : '';
  80. break;
  81. }
  82. if (btnDisplay) {
  83. node = $('<li>', {
  84. 'class': classes.sPageButton + ' ' + btnClass,
  85. 'aria-controls': settings.sTableId,
  86. 'tabindex': settings.iTabIndex,
  87. 'id': idx === 0 && typeof button === 'string' ?
  88. settings.sTableId + '_' + button :
  89. null
  90. })
  91. .append($('<a>', {
  92. 'href': '#'
  93. })
  94. .html(btnDisplay)
  95. )
  96. .appendTo(container);
  97. settings.oApi._fnBindAction(
  98. node, { action: button }, clickHandler
  99. );
  100. }
  101. }
  102. }
  103. };
  104. attach(
  105. $(host).empty().html('<ul class="pagination"/>').children('ul'),
  106. buttons
  107. );
  108. };
  109. /*
  110. * TableTools Bootstrap compatibility
  111. * Required TableTools 2.1+
  112. */
  113. if (DataTable.TableTools) {
  114. // Set the classes that TableTools uses to something suitable for Bootstrap
  115. $.extend(true, DataTable.TableTools.classes, {
  116. "container": "DTTT btn-group",
  117. "buttons": {
  118. "normal": "btn btn-default",
  119. "disabled": "disabled"
  120. },
  121. "collection": {
  122. "container": "DTTT_dropdown dropdown-menu",
  123. "buttons": {
  124. "normal": "",
  125. "disabled": "disabled"
  126. }
  127. },
  128. "print": {
  129. "info": "DTTT_print_info"
  130. },
  131. "select": {
  132. "row": "active"
  133. }
  134. });
  135. // Have the collection use a bootstrap compatible drop down
  136. $.extend(true, DataTable.TableTools.DEFAULTS.oTags, {
  137. "collection": {
  138. "container": "ul",
  139. "button": "li",
  140. "liner": "a"
  141. }
  142. });
  143. }
  144. }; // /factory
  145. // Define as an AMD module if possible
  146. if (typeof define === 'function' && define.amd) {
  147. define(['jquery', 'datatables'], factory);
  148. }
  149. else if (typeof exports === 'object') {
  150. // Node/CommonJS
  151. factory(require('jquery'), require('datatables'));
  152. }
  153. else if (jQuery) {
  154. // Otherwise simply initialise as normal, stopping multiple evaluation
  155. factory(jQuery, jQuery.fn.dataTable);
  156. }
  157. })(window, document);