bootstrap-table-group-by.js 7.7 KB

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