bootstrap-table-group-by.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('core-js/modules/es.array.concat.js'), require('core-js/modules/es.array.filter.js'), require('core-js/modules/es.array.find.js'), require('core-js/modules/es.array.join.js'), require('core-js/modules/es.array.slice.js'), require('core-js/modules/es.array.sort.js'), require('core-js/modules/es.function.name.js'), require('core-js/modules/es.object.assign.js'), require('core-js/modules/es.object.to-string.js'), require('core-js/modules/web.dom-collections.for-each.js'), require('jquery')) :
  3. typeof define === 'function' && define.amd ? define(['core-js/modules/es.array.concat.js', 'core-js/modules/es.array.filter.js', 'core-js/modules/es.array.find.js', 'core-js/modules/es.array.join.js', 'core-js/modules/es.array.slice.js', 'core-js/modules/es.array.sort.js', 'core-js/modules/es.function.name.js', 'core-js/modules/es.object.assign.js', 'core-js/modules/es.object.to-string.js', 'core-js/modules/web.dom-collections.for-each.js', 'jquery'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(null, null, null, null, null, null, null, null, null, null, global.jQuery));
  5. })(this, (function (es_array_concat_js, es_array_filter_js, es_array_find_js, es_array_join_js, es_array_slice_js, es_array_sort_js, es_function_name_js, es_object_assign_js, es_object_toString_js, web_domCollections_forEach_js, $) { 'use strict';
  6. function _assertThisInitialized(e) {
  7. if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  8. return e;
  9. }
  10. function _callSuper(t, o, e) {
  11. return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
  12. }
  13. function _classCallCheck(a, n) {
  14. if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
  15. }
  16. function _defineProperties(e, r) {
  17. for (var t = 0; t < r.length; t++) {
  18. var o = r[t];
  19. o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
  20. }
  21. }
  22. function _createClass(e, r, t) {
  23. return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
  24. writable: !1
  25. }), e;
  26. }
  27. function _get() {
  28. return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
  29. var p = _superPropBase(e, t);
  30. if (p) {
  31. var n = Object.getOwnPropertyDescriptor(p, t);
  32. return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
  33. }
  34. }, _get.apply(null, arguments);
  35. }
  36. function _getPrototypeOf(t) {
  37. return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
  38. return t.__proto__ || Object.getPrototypeOf(t);
  39. }, _getPrototypeOf(t);
  40. }
  41. function _inherits(t, e) {
  42. if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
  43. t.prototype = Object.create(e && e.prototype, {
  44. constructor: {
  45. value: t,
  46. writable: !0,
  47. configurable: !0
  48. }
  49. }), Object.defineProperty(t, "prototype", {
  50. writable: !1
  51. }), e && _setPrototypeOf(t, e);
  52. }
  53. function _isNativeReflectConstruct() {
  54. try {
  55. var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  56. } catch (t) {}
  57. return (_isNativeReflectConstruct = function () {
  58. return !!t;
  59. })();
  60. }
  61. function _possibleConstructorReturn(t, e) {
  62. if (e && ("object" == typeof e || "function" == typeof e)) return e;
  63. if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
  64. return _assertThisInitialized(t);
  65. }
  66. function _setPrototypeOf(t, e) {
  67. return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
  68. return t.__proto__ = e, t;
  69. }, _setPrototypeOf(t, e);
  70. }
  71. function _superPropBase(t, o) {
  72. for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
  73. return t;
  74. }
  75. function _toPrimitive(t, r) {
  76. if ("object" != typeof t || !t) return t;
  77. var e = t[Symbol.toPrimitive];
  78. if (void 0 !== e) {
  79. var i = e.call(t, r );
  80. if ("object" != typeof i) return i;
  81. throw new TypeError("@@toPrimitive must return a primitive value.");
  82. }
  83. return (String )(t);
  84. }
  85. function _toPropertyKey(t) {
  86. var i = _toPrimitive(t, "string");
  87. return "symbol" == typeof i ? i : i + "";
  88. }
  89. function _typeof(o) {
  90. "@babel/helpers - typeof";
  91. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
  92. return typeof o;
  93. } : function (o) {
  94. return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
  95. }, _typeof(o);
  96. }
  97. /**
  98. * @author: Yura Knoxville
  99. * @version: v1.1.0
  100. */
  101. var Utils = $.fn.bootstrapTable.utils;
  102. var initBodyCaller;
  103. var groupBy = function groupBy(array, f) {
  104. var tmpGroups = {};
  105. array.forEach(function (o) {
  106. var groups = f(o);
  107. tmpGroups[groups] = tmpGroups[groups] || [];
  108. tmpGroups[groups].push(o);
  109. });
  110. return tmpGroups;
  111. };
  112. Object.assign($.fn.bootstrapTable.defaults.icons, {
  113. collapseGroup: {
  114. bootstrap3: 'glyphicon-chevron-up',
  115. bootstrap5: 'bi-chevron-up',
  116. materialize: 'arrow_drop_down'
  117. }[$.fn.bootstrapTable.theme] || 'fa-angle-up',
  118. expandGroup: {
  119. bootstrap3: 'glyphicon-chevron-down',
  120. bootstrap5: 'bi-chevron-down',
  121. materialize: 'arrow_drop_up'
  122. }[$.fn.bootstrapTable.theme] || 'fa-angle-down'
  123. });
  124. Object.assign($.fn.bootstrapTable.defaults, {
  125. groupBy: false,
  126. groupByField: '',
  127. groupByFormatter: undefined,
  128. groupByToggle: false,
  129. groupByShowToggleIcon: false,
  130. groupByCollapsedGroups: []
  131. });
  132. var BootstrapTable = $.fn.bootstrapTable.Constructor;
  133. var _initSort = BootstrapTable.prototype.initSort;
  134. var _initBody = BootstrapTable.prototype.initBody;
  135. var _updateSelected = BootstrapTable.prototype.updateSelected;
  136. BootstrapTable.prototype.initSort = function () {
  137. var _this = this;
  138. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  139. args[_key] = arguments[_key];
  140. }
  141. _initSort.apply(this, Array.prototype.slice.apply(args));
  142. var that = this;
  143. this.tableGroups = [];
  144. if (this.options.groupBy && this.options.groupByField !== '') {
  145. if (this.options.sortName !== this.options.groupByField) {
  146. if (this.options.customSort) {
  147. Utils.calculateObjectValue(this.options, this.options.customSort, [this.options.sortName, this.options.sortOrder, this.data]);
  148. } else {
  149. this.options.data.sort(function (a, b) {
  150. var groupByFields = _this.getGroupByFields();
  151. var fieldValuesA = [];
  152. var fieldValuesB = [];
  153. $.each(groupByFields, function (i, field) {
  154. fieldValuesA.push(a[field]);
  155. fieldValuesB.push(b[field]);
  156. });
  157. a = fieldValuesA.join();
  158. b = fieldValuesB.join();
  159. return a.localeCompare(b, undefined, {
  160. numeric: true
  161. });
  162. });
  163. }
  164. }
  165. var groups = groupBy(that.data, function (item) {
  166. var groupByFields = _this.getGroupByFields();
  167. var groupValues = [];
  168. $.each(groupByFields, function (i, field) {
  169. var value_ = Utils.getItemField(item, field, that.options.escape, item.escape);
  170. groupValues.push(value_);
  171. });
  172. return groupValues.join(', ');
  173. });
  174. var index = 0;
  175. $.each(groups, function (key, value) {
  176. _this.tableGroups.push({
  177. id: index,
  178. name: key,
  179. data: value
  180. });
  181. value.forEach(function (item) {
  182. if (!item._data) {
  183. item._data = {};
  184. }
  185. if (_this.isCollapsed(key, value)) {
  186. item._class += ' hidden';
  187. }
  188. item._data['parent-index'] = index;
  189. });
  190. index++;
  191. });
  192. }
  193. };
  194. BootstrapTable.prototype.initBody = function () {
  195. var _this2 = this;
  196. initBodyCaller = true;
  197. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  198. args[_key2] = arguments[_key2];
  199. }
  200. _initBody.apply(this, Array.prototype.slice.apply(args));
  201. if (this.options.groupBy && this.options.groupByField !== '') {
  202. var that = this;
  203. var checkBox = false;
  204. var visibleColumns = 0;
  205. this.columns.forEach(function (column) {
  206. if (column.checkbox && !that.options.singleSelect) {
  207. checkBox = true;
  208. } else if (column.visible) {
  209. visibleColumns += 1;
  210. }
  211. });
  212. if (this.options.detailView && !this.options.cardView) {
  213. visibleColumns += 1;
  214. }
  215. this.tableGroups.forEach(function (item) {
  216. var html = [];
  217. html.push(Utils.sprintf('<tr class="info group-by %s" data-group-index="%s">', _this2.options.groupByToggle ? 'expanded' : '', item.id));
  218. if (that.options.detailView && !that.options.cardView) {
  219. html.push('<td class="detail"></td>');
  220. }
  221. if (checkBox) {
  222. html.push('<td class="bs-checkbox">', '<input name="btSelectGroup" type="checkbox" />', '</td>');
  223. }
  224. var formattedValue = item.name;
  225. if (that.options.groupByFormatter !== undefined) {
  226. formattedValue = Utils.calculateObjectValue(that.options, that.options.groupByFormatter, [item.name, item.id, item.data]);
  227. }
  228. html.push('<td', Utils.sprintf(' colspan="%s"', visibleColumns), '>', formattedValue);
  229. var icon = _this2.options.icons.collapseGroup;
  230. if (_this2.isCollapsed(item.name, item.data)) {
  231. icon = _this2.options.icons.expandGroup;
  232. }
  233. if (_this2.options.groupByToggle && _this2.options.groupByShowToggleIcon) {
  234. html.push("<span class=\"float-right ".concat(_this2.options.iconsPrefix, " ").concat(icon, "\"></span>"));
  235. }
  236. html.push('</td></tr>');
  237. that.$body.find("tr[data-parent-index=".concat(item.id, "]:first")).before($(html.join('')));
  238. });
  239. this.$selectGroup = [];
  240. this.$body.find('[name="btSelectGroup"]').each(function () {
  241. var self = $(this);
  242. that.$selectGroup.push({
  243. group: self,
  244. item: that.$selectItem.filter(function () {
  245. return $(this).closest('tr').data('parent-index') === self.closest('tr').data('group-index');
  246. })
  247. });
  248. });
  249. if (this.options.groupByToggle) {
  250. this.$container.off('click', '.group-by').on('click', '.group-by', function () {
  251. var $this = $(this);
  252. var groupIndex = $this.closest('tr').data('group-index');
  253. var $groupRows = that.$body.find("tr[data-parent-index=".concat(groupIndex, "]"));
  254. $this.toggleClass('expanded collapsed');
  255. $this.find('span').toggleClass("".concat(that.options.icons.collapseGroup, " ").concat(that.options.icons.expandGroup));
  256. $groupRows.toggleClass('hidden');
  257. $groupRows.each(function (i, element) {
  258. return that.collapseRow($(element).data('index'));
  259. });
  260. });
  261. }
  262. this.$container.off('click', '[name="btSelectGroup"]').on('click', '[name="btSelectGroup"]', function (event) {
  263. event.stopImmediatePropagation();
  264. var self = $(this);
  265. var checked = self.prop('checked');
  266. that[checked ? 'checkGroup' : 'uncheckGroup']($(this).closest('tr').data('group-index'));
  267. });
  268. }
  269. initBodyCaller = false;
  270. this.updateSelected();
  271. };
  272. BootstrapTable.prototype.updateSelected = function () {
  273. if (!initBodyCaller) {
  274. for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  275. args[_key3] = arguments[_key3];
  276. }
  277. _updateSelected.apply(this, Array.prototype.slice.apply(args));
  278. if (this.options.groupBy && this.options.groupByField !== '') {
  279. this.$selectGroup.forEach(function (item) {
  280. var checkGroup = item.item.filter(':enabled').length === item.item.filter(':enabled').filter(':checked').length;
  281. item.group.prop('checked', checkGroup);
  282. });
  283. }
  284. }
  285. };
  286. BootstrapTable.prototype.checkGroup = function (index) {
  287. this.checkGroup_(index, true);
  288. };
  289. BootstrapTable.prototype.uncheckGroup = function (index) {
  290. this.checkGroup_(index, false);
  291. };
  292. BootstrapTable.prototype.isCollapsed = function (groupKey, items) {
  293. if (this.options.groupByCollapsedGroups) {
  294. var collapsedGroups = Utils.calculateObjectValue(this, this.options.groupByCollapsedGroups, [groupKey, items], true);
  295. if ($.inArray(groupKey, collapsedGroups) > -1) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. };
  301. BootstrapTable.prototype.checkGroup_ = function (index, checked) {
  302. var rowsBefore = this.getSelections();
  303. var filter = function filter() {
  304. return $(this).closest('tr').data('parent-index') === index;
  305. };
  306. this.$selectItem.filter(filter).prop('checked', checked);
  307. this.updateRows();
  308. this.updateSelected();
  309. var rowsAfter = this.getSelections();
  310. if (checked) {
  311. this.trigger('check-all', rowsAfter, rowsBefore);
  312. return;
  313. }
  314. this.trigger('uncheck-all', rowsAfter, rowsBefore);
  315. };
  316. BootstrapTable.prototype.getGroupByFields = function () {
  317. var groupByFields = this.options.groupByField;
  318. if (!$.isArray(this.options.groupByField)) {
  319. groupByFields = [this.options.groupByField];
  320. }
  321. return groupByFields;
  322. };
  323. $.BootstrapTable = /*#__PURE__*/function (_$$BootstrapTable) {
  324. function _class() {
  325. _classCallCheck(this, _class);
  326. return _callSuper(this, _class, arguments);
  327. }
  328. _inherits(_class, _$$BootstrapTable);
  329. return _createClass(_class, [{
  330. key: "scrollTo",
  331. value: function scrollTo(params) {
  332. if (this.options.groupBy) {
  333. var options = {
  334. unit: 'px',
  335. value: 0
  336. };
  337. if (_typeof(params) === 'object') {
  338. options = Object.assign(options, params);
  339. }
  340. if (options.unit === 'rows') {
  341. var scrollTo = 0;
  342. this.$body.find("> tr:not(.group-by):lt(".concat(options.value, ")")).each(function (i, el) {
  343. scrollTo += $(el).outerHeight(true);
  344. });
  345. var $targetColumn = this.$body.find("> tr:not(.group-by):eq(".concat(options.value, ")"));
  346. $targetColumn.prevAll('.group-by').each(function (i, el) {
  347. scrollTo += $(el).outerHeight(true);
  348. });
  349. this.$tableBody.scrollTop(scrollTo);
  350. return;
  351. }
  352. }
  353. _get(_getPrototypeOf(_class.prototype), "scrollTo", this).call(this, params);
  354. }
  355. }]);
  356. }($.BootstrapTable);
  357. }));