bootstrap-table-multiple-sort.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**
  2. * @author Nadim Basalamah <dimbslmh@gmail.com>
  3. * @version: v1.1.0
  4. * https://github.com/dimbslmh/bootstrap-table/tree/master/src/extensions/multiple-sort/bootstrap-table-multiple-sort.js
  5. * Modification: ErwannNevou <https://github.com/ErwannNevou>
  6. */
  7. (function($) {
  8. 'use strict';
  9. var isSingleSort = false;
  10. var showSortModal = function(that) {
  11. var _selector = that.$sortModal.selector,
  12. _id = _selector.substr(1);
  13. if (!$(_id).hasClass("modal")) {
  14. var sModal = ' <div class="modal fade" id="' + _id + '" tabindex="-1" role="dialog" aria-labelledby="' + _id + 'Label" aria-hidden="true">';
  15. sModal += ' <div class="modal-dialog">';
  16. sModal += ' <div class="modal-content">';
  17. sModal += ' <div class="modal-header">';
  18. sModal += ' <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
  19. sModal += ' <h4 class="modal-title" id="' + _id + 'Label">' + that.options.formatMultipleSort() + '</h4>';
  20. sModal += ' </div>';
  21. sModal += ' <div class="modal-body">';
  22. sModal += ' <div class="bootstrap-table">';
  23. sModal += ' <div class="fixed-table-toolbar">';
  24. sModal += ' <div class="bars">';
  25. sModal += ' <div id="toolbar">';
  26. sModal += ' <button id="add" type="button" class="btn btn-default"><i class="' + that.options.iconsPrefix + ' ' + that.options.icons.plus + '"></i> ' + that.options.formatAddLevel() + '</button>';
  27. sModal += ' <button id="delete" type="button" class="btn btn-default" disabled><i class="' + that.options.iconsPrefix + ' ' + that.options.icons.minus + '"></i> ' + that.options.formatDeleteLevel() + '</button>';
  28. sModal += ' </div>';
  29. sModal += ' </div>';
  30. sModal += ' </div>';
  31. sModal += ' <div class="fixed-table-container">';
  32. sModal += ' <table id="multi-sort" class="table">';
  33. sModal += ' <thead>';
  34. sModal += ' <tr>';
  35. sModal += ' <th></th>';
  36. sModal += ' <th><div class="th-inner">' + that.options.formatColumn() + '</div></th>';
  37. sModal += ' <th><div class="th-inner">' + that.options.formatOrder() + '</div></th>';
  38. sModal += ' </tr>';
  39. sModal += ' </thead>';
  40. sModal += ' <tbody></tbody>';
  41. sModal += ' </table>';
  42. sModal += ' </div>';
  43. sModal += ' </div>';
  44. sModal += ' </div>';
  45. sModal += ' <div class="modal-footer">';
  46. sModal += ' <button type="button" class="btn btn-default" data-dismiss="modal">' + that.options.formatCancel() + '</button>';
  47. sModal += ' <button type="button" class="btn btn-primary">' + that.options.formatSort() + '</button>';
  48. sModal += ' </div>';
  49. sModal += ' </div>';
  50. sModal += ' </div>';
  51. sModal += ' </div>';
  52. $('body').append($(sModal));
  53. that.$sortModal = $(_selector);
  54. var $rows = that.$sortModal.find('tbody > tr');
  55. that.$sortModal.off('click', '#add').on('click', '#add', function() {
  56. var total = that.$sortModal.find('.multi-sort-name:first option').length,
  57. current = that.$sortModal.find('tbody tr').length;
  58. if (current < total) {
  59. current++;
  60. that.addLevel();
  61. that.setButtonStates();
  62. }
  63. });
  64. that.$sortModal.off('click', '#delete').on('click', '#delete', function() {
  65. var total = that.$sortModal.find('.multi-sort-name:first option').length,
  66. current = that.$sortModal.find('tbody tr').length;
  67. if (current > 1 && current <= total) {
  68. current--;
  69. that.$sortModal.find('tbody tr:last').remove();
  70. that.setButtonStates();
  71. }
  72. });
  73. that.$sortModal.off('click', '.btn-primary').on('click', '.btn-primary', function() {
  74. var $rows = that.$sortModal.find('tbody > tr'),
  75. $alert = that.$sortModal.find('div.alert'),
  76. fields = [],
  77. results = [];
  78. that.options.sortPriority = $.map($rows, function(row) {
  79. var $row = $(row),
  80. name = $row.find('.multi-sort-name').val(),
  81. order = $row.find('.multi-sort-order').val();
  82. fields.push(name);
  83. return {
  84. sortName: name,
  85. sortOrder: order
  86. };
  87. });
  88. var sorted_fields = fields.sort();
  89. for (var i = 0; i < fields.length - 1; i++) {
  90. if (sorted_fields[i + 1] == sorted_fields[i]) {
  91. results.push(sorted_fields[i]);
  92. }
  93. }
  94. if (results.length > 0) {
  95. if ($alert.length === 0) {
  96. $alert = '<div class="alert alert-danger" role="alert"><strong>' + that.options.formatDuplicateAlertTitle() + '</strong> ' + that.options.formatDuplicateAlertDescription() + '</div>';
  97. $($alert).insertBefore(that.$sortModal.find('.bars'));
  98. }
  99. } else {
  100. if ($alert.length === 1) {
  101. $($alert).remove();
  102. }
  103. that.$sortModal.modal('hide');
  104. that.options.sortName = '';
  105. if (that.options.sidePagination === 'server') {
  106. that.options.queryParams = function(params) {
  107. params.multiSort = that.options.sortPriority;
  108. return params;
  109. };
  110. that.initServer(that.options.silentSort);
  111. return;
  112. }
  113. that.onMultipleSort();
  114. }
  115. });
  116. if (that.options.sortPriority === null || that.options.sortPriority.length === 0) {
  117. if (that.options.sortName) {
  118. that.options.sortPriority = [{
  119. sortName: that.options.sortName,
  120. sortOrder: that.options.sortOrder
  121. }];
  122. }
  123. }
  124. if (that.options.sortPriority !== null && that.options.sortPriority.length > 0) {
  125. if ($rows.length < that.options.sortPriority.length && typeof that.options.sortPriority === 'object') {
  126. for (var i = 0; i < that.options.sortPriority.length; i++) {
  127. that.addLevel(i, that.options.sortPriority[i]);
  128. }
  129. }
  130. } else {
  131. that.addLevel(0);
  132. }
  133. that.setButtonStates();
  134. }
  135. };
  136. $.extend($.fn.bootstrapTable.defaults, {
  137. showMultiSort: false,
  138. sortPriority: null,
  139. onMultipleSort: function() {
  140. return false;
  141. }
  142. });
  143. $.extend($.fn.bootstrapTable.defaults.icons, {
  144. sort: 'glyphicon-sort',
  145. plus: 'glyphicon-plus',
  146. minus: 'glyphicon-minus'
  147. });
  148. $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
  149. 'multiple-sort.bs.table': 'onMultipleSort'
  150. });
  151. $.extend($.fn.bootstrapTable.locales, {
  152. formatMultipleSort: function() {
  153. return 'Multiple Sort';
  154. },
  155. formatAddLevel: function() {
  156. return 'Add Level';
  157. },
  158. formatDeleteLevel: function() {
  159. return 'Delete Level';
  160. },
  161. formatColumn: function() {
  162. return 'Column';
  163. },
  164. formatOrder: function() {
  165. return 'Order';
  166. },
  167. formatSortBy: function() {
  168. return 'Sort by';
  169. },
  170. formatThenBy: function() {
  171. return 'Then by';
  172. },
  173. formatSort: function() {
  174. return 'Sort';
  175. },
  176. formatCancel: function() {
  177. return 'Cancel';
  178. },
  179. formatDuplicateAlertTitle: function() {
  180. return 'Duplicate(s) detected!';
  181. },
  182. formatDuplicateAlertDescription: function() {
  183. return 'Please remove or change any duplicate column.';
  184. },
  185. formatSortOrders: function() {
  186. return {
  187. asc: 'Ascending',
  188. desc: 'Descending'
  189. };
  190. }
  191. });
  192. $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
  193. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  194. _initToolbar = BootstrapTable.prototype.initToolbar;
  195. BootstrapTable.prototype.initToolbar = function() {
  196. this.showToolbar = true;
  197. var that = this,
  198. sortModalId = '#sortModal_' + this.$el.attr('id');
  199. this.$sortModal = $(sortModalId);
  200. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  201. if (this.options.showMultiSort) {
  202. var $btnGroup = this.$toolbar.find('>.btn-group').first(),
  203. $multiSortBtn = this.$toolbar.find('div.multi-sort');
  204. if (!$multiSortBtn.length) {
  205. $multiSortBtn = ' <button class="multi-sort btn btn-default' + (this.options.iconSize === undefined ? '' : ' btn-' + this.options.iconSize) + '" type="button" data-toggle="modal" data-target="' + sortModalId + '" title="' + this.options.formatMultipleSort() + '">';
  206. $multiSortBtn += ' <i class="' + this.options.iconsPrefix + ' ' + this.options.icons.sort + '"></i>';
  207. $multiSortBtn += '</button>';
  208. $btnGroup.append($multiSortBtn);
  209. showSortModal(that);
  210. }
  211. this.$el.on('sort.bs.table', function() {
  212. isSingleSort = true;
  213. });
  214. this.$el.on('multiple-sort.bs.table', function() {
  215. isSingleSort = false;
  216. });
  217. this.$el.on('load-success.bs.table', function() {
  218. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object' && that.options.sidePagination !== 'server') {
  219. that.onMultipleSort();
  220. }
  221. });
  222. this.$el.on('column-switch.bs.table', function(field, checked) {
  223. for (var i = 0; i < that.options.sortPriority.length; i++) {
  224. if (that.options.sortPriority[i].sortName === checked) {
  225. that.options.sortPriority.splice(i, 1);
  226. }
  227. }
  228. that.assignSortableArrows();
  229. that.$sortModal.remove();
  230. showSortModal(that);
  231. });
  232. this.$el.on('reset-view.bs.table', function() {
  233. if (!isSingleSort && that.options.sortPriority !== null && typeof that.options.sortPriority === 'object') {
  234. that.assignSortableArrows();
  235. }
  236. });
  237. }
  238. };
  239. BootstrapTable.prototype.onMultipleSort = function() {
  240. var that = this;
  241. var cmp = function(x, y) {
  242. return x > y ? 1 : x < y ? -1 : 0;
  243. };
  244. var arrayCmp = function(a, b) {
  245. var arr1 = [],
  246. arr2 = [];
  247. for (var i = 0; i < that.options.sortPriority.length; i++) {
  248. var order = that.options.sortPriority[i].sortOrder === 'desc' ? -1 : 1,
  249. aa = a[that.options.sortPriority[i].sortName],
  250. bb = b[that.options.sortPriority[i].sortName];
  251. if (aa === undefined || aa === null) {
  252. aa = '';
  253. }
  254. if (bb === undefined || bb === null) {
  255. bb = '';
  256. }
  257. if ($.isNumeric(aa) && $.isNumeric(bb)) {
  258. aa = parseFloat(aa);
  259. bb = parseFloat(bb);
  260. }
  261. if (typeof aa !== 'string') {
  262. aa = aa.toString();
  263. }
  264. arr1.push(
  265. order * cmp(aa, bb));
  266. arr2.push(
  267. order * cmp(bb, aa));
  268. }
  269. return cmp(arr1, arr2);
  270. };
  271. this.data.sort(function(a, b) {
  272. return arrayCmp(a, b);
  273. });
  274. this.initBody();
  275. this.assignSortableArrows();
  276. this.trigger('multiple-sort');
  277. };
  278. BootstrapTable.prototype.addLevel = function(index, sortPriority) {
  279. var text = index === 0 ? this.options.formatSortBy() : this.options.formatThenBy();
  280. this.$sortModal.find('tbody')
  281. .append($('<tr>')
  282. .append($('<td>').text(text))
  283. .append($('<td>').append($('<select class="form-control multi-sort-name">')))
  284. .append($('<td>').append($('<select class="form-control multi-sort-order">')))
  285. );
  286. var $multiSortName = this.$sortModal.find('.multi-sort-name').last(),
  287. $multiSortOrder = this.$sortModal.find('.multi-sort-order').last();
  288. $.each(this.columns, function(i, column) {
  289. if (column.sortable === false || column.visible === false) {
  290. return true;
  291. }
  292. $multiSortName.append('<option value="' + column.field + '">' + column.title + '</option>');
  293. });
  294. $.each(this.options.formatSortOrders(), function(value, order) {
  295. $multiSortOrder.append('<option value="' + value + '">' + order + '</option>');
  296. });
  297. if (sortPriority !== undefined) {
  298. $multiSortName.find('option[value="' + sortPriority.sortName + '"]').attr("selected", true);
  299. $multiSortOrder.find('option[value="' + sortPriority.sortOrder + '"]').attr("selected", true);
  300. }
  301. };
  302. BootstrapTable.prototype.assignSortableArrows = function() {
  303. var that = this,
  304. headers = that.$header.find('th');
  305. for (var i = 0; i < headers.length; i++) {
  306. for (var c = 0; c < that.options.sortPriority.length; c++) {
  307. if ($(headers[i]).data('field') === that.options.sortPriority[c].sortName) {
  308. $(headers[i]).find('.sortable').removeClass('desc asc').addClass(that.options.sortPriority[c].sortOrder);
  309. }
  310. }
  311. }
  312. };
  313. BootstrapTable.prototype.setButtonStates = function() {
  314. var total = this.$sortModal.find('.multi-sort-name:first option').length,
  315. current = this.$sortModal.find('tbody tr').length;
  316. if (current == total) {
  317. this.$sortModal.find('#add').attr('disabled', 'disabled');
  318. }
  319. if (current > 1) {
  320. this.$sortModal.find('#delete').removeAttr('disabled');
  321. }
  322. if (current < total) {
  323. this.$sortModal.find('#add').removeAttr('disabled');
  324. }
  325. if (current == 1) {
  326. this.$sortModal.find('#delete').attr('disabled', 'disabled');
  327. }
  328. };
  329. })(jQuery);