bootstrap-table-group-by.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @author: Dennis Hernández
  3. * @webSite: http://djhvscf.github.io/Blog
  4. * @version: v1.0.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 rowAttr = function (row, index) {
  14. //Call the User Defined Function
  15. originalRowAttr.apply([row, index]);
  16. obj[dataTTId.toString()] = index;
  17. if (!row.IsParent) {
  18. obj[dataTTParentId.toString()] = parentId === undefined ? index : parentId;
  19. } else {
  20. parentId = index;
  21. delete obj[dataTTParentId.toString()];
  22. }
  23. return obj;
  24. };
  25. var setObjectKeys = function () {
  26. // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  27. Object.keys = function (o) {
  28. if (o !== Object(o)) {
  29. throw new TypeError('Object.keys called on a non-object');
  30. }
  31. var k = [],
  32. p;
  33. for (p in o) {
  34. if (Object.prototype.hasOwnProperty.call(o, p)) {
  35. k.push(p);
  36. }
  37. }
  38. return k;
  39. }
  40. };
  41. var groupBy = function (array , f) {
  42. var groups = {};
  43. array.forEach(function(o) {
  44. var group = JSON.stringify(f(o));
  45. groups[group] = groups[group] || [];
  46. groups[group].push(o);
  47. });
  48. return Object.keys(groups).map(function (group) {
  49. return groups[group];
  50. });
  51. };
  52. var makeGrouped = function (that) {
  53. var newRow = {},
  54. newData = [];
  55. var result = groupBy(that.options.data, function (item) {
  56. return [item[that.options.groupByField]];
  57. });
  58. for (var i = 0; i < result.length; i++) {
  59. newRow[that.options.groupByField.toString()] = result[i][0][that.options.groupByField];
  60. newRow.IsParent = true;
  61. result[i].unshift(newRow);
  62. newRow = {};
  63. }
  64. newData = newData.concat.apply(newData, result);
  65. if (!that.options.loaded) {
  66. that.options.loaded = true;
  67. that.options.originalData = that.options.data;
  68. that.options.data = newData;
  69. }
  70. };
  71. var calculateObjectValue = function (self, name, args, defaultValue) {
  72. var func = name;
  73. if (typeof name === 'string') {
  74. // support obj.func1.func2
  75. var names = name.split('.');
  76. if (names.length > 1) {
  77. func = window;
  78. $.each(names, function (i, f) {
  79. func = func[f];
  80. });
  81. } else {
  82. func = window[name];
  83. }
  84. }
  85. if (typeof func === 'object') {
  86. return func;
  87. }
  88. if (typeof func === 'function') {
  89. return func.apply(self, args);
  90. }
  91. if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
  92. return sprintf.apply(this, [name].concat(args));
  93. }
  94. return defaultValue;
  95. };
  96. $.extend($.fn.bootstrapTable.defaults, {
  97. groupBy: false,
  98. groupByField: '',
  99. //internal variables
  100. loaded: false,
  101. originalData: undefined
  102. });
  103. $.extend($.fn.bootstrapTable.methods, [
  104. 'collapseAll',
  105. 'expandAll'
  106. ]);
  107. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  108. _init = BootstrapTable.prototype.init,
  109. _initData = BootstrapTable.prototype.initData;
  110. BootstrapTable.prototype.init = function () {
  111. //Temporal validation
  112. if (!this.options.sortName) {
  113. if ((this.options.groupBy) && (this.options.groupByField !== '')) {
  114. var that = this;
  115. // Compatibility: IE < 9 and old browsers
  116. if (!Object.keys) {
  117. setObjectKeys();
  118. }
  119. //Make sure that the internal variables are set correctly
  120. this.options.loaded = false;
  121. this.options.originalData = undefined;
  122. originalRowAttr = this.options.rowAttributes;
  123. this.options.rowAttributes = rowAttr;
  124. this.$el.on('post-body.bs.table', function () {
  125. that.$el.treetable({
  126. expandable: true,
  127. onNodeExpand: function () {
  128. if (that.options.height) {
  129. that.resetHeader();
  130. }
  131. },
  132. onNodeCollapse: function () {
  133. if (that.options.height) {
  134. that.resetHeader();
  135. }
  136. }
  137. }, true);
  138. });
  139. }
  140. }
  141. _init.apply(this, Array.prototype.slice.apply(arguments));
  142. };
  143. BootstrapTable.prototype.initData = function () {
  144. //Temporal validation
  145. if (!this.options.sortName) {
  146. if ((this.options.groupBy) && (this.options.groupByField !== '')) {
  147. makeGrouped(this);
  148. }
  149. }
  150. _initData.apply(this, Array.prototype.slice.apply(arguments));
  151. };
  152. BootstrapTable.prototype.expandAll = function () {
  153. this.$el.treetable("expandAll");
  154. };
  155. BootstrapTable.prototype.collapseAll = function () {
  156. this.$el.treetable("collapseAll");
  157. };
  158. }(jQuery);