bootstrap-table-group-by.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define([], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory();
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory();
  11. global.bootstrapTableGroupBy = mod.exports;
  12. }
  13. })(this, function () {
  14. 'use strict';
  15. /**
  16. * @author: Dennis Hernández
  17. * @webSite: http://djhvscf.github.io/Blog
  18. * @version: v1.1.0
  19. */
  20. !function ($) {
  21. 'use strict';
  22. var originalRowAttr,
  23. dataTTId = 'data-tt-id',
  24. dataTTParentId = 'data-tt-parent-id',
  25. obj = {},
  26. parentId = undefined;
  27. var getParentRowId = function getParentRowId(that, id) {
  28. var parentRows = that.$body.find('tr').not('[' + 'data-tt-parent-id]');
  29. for (var i = 0; i < parentRows.length; i++) {
  30. if (i === id) {
  31. return $(parentRows[i]).attr('data-tt-id');
  32. }
  33. }
  34. return undefined;
  35. };
  36. var sumData = function sumData(that, data) {
  37. var sumRow = {};
  38. $.each(data, function (i, row) {
  39. if (!row.IsParent) {
  40. for (var prop in row) {
  41. if (!isNaN(parseFloat(row[prop]))) {
  42. if (that.columns[that.fieldsColumnsIndex[prop]].groupBySumGroup) {
  43. if (sumRow[prop] === undefined) {
  44. sumRow[prop] = 0;
  45. }
  46. sumRow[prop] += +row[prop];
  47. }
  48. }
  49. }
  50. }
  51. });
  52. return sumRow;
  53. };
  54. var rowAttr = function rowAttr(row, index) {
  55. //Call the User Defined Function
  56. originalRowAttr.apply([row, index]);
  57. obj[dataTTId.toString()] = index;
  58. if (!row.IsParent) {
  59. obj[dataTTParentId.toString()] = parentId === undefined ? index : parentId;
  60. } else {
  61. parentId = index;
  62. delete obj[dataTTParentId.toString()];
  63. }
  64. return obj;
  65. };
  66. var setObjectKeys = function setObjectKeys() {
  67. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  68. Object.keys = function (o) {
  69. if (o !== Object(o)) {
  70. throw new TypeError('Object.keys called on a non-object');
  71. }
  72. var k = [],
  73. p;
  74. for (p in o) {
  75. if (Object.prototype.hasOwnProperty.call(o, p)) {
  76. k.push(p);
  77. }
  78. }
  79. return k;
  80. };
  81. };
  82. var getDataArrayFromItem = function getDataArrayFromItem(that, item) {
  83. var itemDataArray = [];
  84. for (var i = 0; i < that.options.groupByField.length; i++) {
  85. itemDataArray.push(item[that.options.groupByField[i]]);
  86. }
  87. return itemDataArray;
  88. };
  89. var getNewRow = function getNewRow(that, result, index) {
  90. var newRow = {};
  91. for (var i = 0; i < that.options.groupByField.length; i++) {
  92. newRow[that.options.groupByField[i].toString()] = result[index][0][that.options.groupByField[i]];
  93. }
  94. newRow.IsParent = true;
  95. return newRow;
  96. };
  97. var groupBy = function groupBy(array, f) {
  98. var groups = {};
  99. $.each(array, function (i, o) {
  100. var group = JSON.stringify(f(o));
  101. groups[group] = groups[group] || [];
  102. groups[group].push(o);
  103. });
  104. return Object.keys(groups).map(function (group) {
  105. return groups[group];
  106. });
  107. };
  108. var makeGrouped = function makeGrouped(that, data) {
  109. var newData = [],
  110. sumRow = {};
  111. var result = groupBy(data, function (item) {
  112. return getDataArrayFromItem(that, item);
  113. });
  114. for (var i = 0; i < result.length; i++) {
  115. result[i].unshift(getNewRow(that, result, i));
  116. if (that.options.groupBySumGroup) {
  117. sumRow = sumData(that, result[i]);
  118. if (!$.isEmptyObject(sumRow)) {
  119. result[i].push(sumRow);
  120. }
  121. }
  122. }
  123. newData = newData.concat.apply(newData, result);
  124. if (!that.options.loaded && newData.length > 0) {
  125. that.options.loaded = true;
  126. that.options.originalData = that.options.data;
  127. that.options.data = newData;
  128. }
  129. return newData;
  130. };
  131. $.extend($.fn.bootstrapTable.defaults, {
  132. groupBy: false,
  133. groupByField: [],
  134. groupBySumGroup: false,
  135. groupByInitExpanded: undefined, //node, 'all'
  136. //internal variables
  137. loaded: false,
  138. originalData: undefined
  139. });
  140. $.fn.bootstrapTable.methods.push('collapseAll', 'expandAll', 'refreshGroupByField');
  141. $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
  142. groupBySumGroup: false
  143. });
  144. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  145. _init = BootstrapTable.prototype.init,
  146. _initData = BootstrapTable.prototype.initData;
  147. BootstrapTable.prototype.init = function () {
  148. //Temporal validation
  149. if (!this.options.sortName) {
  150. if (this.options.groupBy && this.options.groupByField.length > 0) {
  151. var that = this;
  152. // Compatibility: IE < 9 and old browsers
  153. if (!Object.keys) {
  154. $.fn.bootstrapTable.utils.objectKeys();
  155. }
  156. //Make sure that the internal variables are set correctly
  157. this.options.loaded = false;
  158. this.options.originalData = undefined;
  159. originalRowAttr = this.options.rowAttributes;
  160. this.options.rowAttributes = rowAttr;
  161. this.$el.off('post-body.bs.table').on('post-body.bs.table', function () {
  162. that.$el.treetable({
  163. expandable: true,
  164. onNodeExpand: function onNodeExpand() {
  165. if (that.options.height) {
  166. that.resetHeader();
  167. }
  168. },
  169. onNodeCollapse: function onNodeCollapse() {
  170. if (that.options.height) {
  171. that.resetHeader();
  172. }
  173. }
  174. }, true);
  175. if (that.options.groupByInitExpanded !== undefined) {
  176. if (typeof that.options.groupByInitExpanded === 'number') {
  177. that.expandNode(that.options.groupByInitExpanded);
  178. } else if (that.options.groupByInitExpanded.toLowerCase() === 'all') {
  179. that.expandAll();
  180. }
  181. }
  182. });
  183. }
  184. }
  185. _init.apply(this, Array.prototype.slice.apply(arguments));
  186. };
  187. BootstrapTable.prototype.initData = function (data, type) {
  188. //Temporal validation
  189. if (!this.options.sortName) {
  190. if (this.options.groupBy && this.options.groupByField.length > 0) {
  191. this.options.groupByField = typeof this.options.groupByField === 'string' ? this.options.groupByField.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') : this.options.groupByField;
  192. data = makeGrouped(this, data ? data : this.options.data);
  193. }
  194. }
  195. _initData.apply(this, [data, type]);
  196. };
  197. BootstrapTable.prototype.expandAll = function () {
  198. this.$el.treetable('expandAll');
  199. };
  200. BootstrapTable.prototype.collapseAll = function () {
  201. this.$el.treetable('collapseAll');
  202. };
  203. BootstrapTable.prototype.expandNode = function (id) {
  204. id = getParentRowId(this, id);
  205. if (id !== undefined) {
  206. this.$el.treetable('expandNode', id);
  207. }
  208. };
  209. BootstrapTable.prototype.refreshGroupByField = function (groupByFields) {
  210. if (!$.fn.bootstrapTable.utils.compareObjects(this.options.groupByField, groupByFields)) {
  211. this.options.groupByField = groupByFields;
  212. this.load(this.options.originalData);
  213. }
  214. };
  215. }(jQuery);
  216. });