bootstrap-table-multiple-sort.js 15 KB

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