浏览代码

Added option sumGroup per column

Dennis Hernández 10 年之前
父节点
当前提交
531476e9b1
共有 2 个文件被更改,包括 38 次插入6 次删除
  1. 10 1
      src/extensions/group-by/README.md
  2. 28 5
      src/extensions/group-by/bootstrap-table-group-by.js

+ 10 - 1
src/extensions/group-by/README.md

@@ -27,7 +27,7 @@ You must include the bootstrap-table-group-by.css file in order to get the appro
 ### groupBySumGroup
 
 * type: Boolean
-* description: Set to True to include a sum row.
+* description: Set to True to include a sum per column.
 * default: `false`
 
 ### groupByInitExpanded
@@ -46,6 +46,15 @@ You must include the bootstrap-table-group-by.css file in order to get the appro
 
 * Collapse all the nodes in the table.
 
+## Column options
+
+### groupBySumGroup
+
+* type: Boolean
+* description: Set to True to sum the column values.
+* default: `false`
+
+
 ## Known issues
 
 ### OnSort

+ 28 - 5
src/extensions/group-by/bootstrap-table-group-by.js

@@ -14,6 +14,19 @@
         obj = {},
         parentId = undefined;
 
+    var getFieldIndex = function (columns, field) {
+        var index = -1;
+
+        $.each(columns, function (i, column) {
+            if (column.field === field) {
+                index = i;
+                return false;
+            }
+            return true;
+        });
+        return index;
+    };
+
     var getParentRowId = function (that, id) {
         var parentRows = that.$body.find('tr').not('[' + 'data-tt-parent-id]');
 
@@ -32,10 +45,12 @@
             for(var prop in row) {
                 if (!row.IsParent) {
                     if (!isNaN(parseFloat(row[prop]))) {
-                        if (sumRow[prop] === undefined) {
-                            sumRow[prop] = 0;
+                        if (that.columns[getFieldIndex(that.columns, prop)].groupBySumGroup) {
+                            if (sumRow[prop] === undefined) {
+                                sumRow[prop] = 0;
+                            }
+                            sumRow[prop] += +row[prop];
                         }
-                        sumRow[prop] += +row[prop];
                     }
                 }
             }
@@ -90,7 +105,8 @@
 
     var makeGrouped = function (that, data) {
         var newRow = {},
-            newData = [];
+            newData = [],
+            sumRow = {};
 
         var result = groupBy(data, function (item) {
             return [item[that.options.groupByField]];
@@ -101,7 +117,10 @@
             newRow.IsParent = true;
             result[i].unshift(newRow);
             if (that.options.groupBySumGroup) {
-                result[i].push(sumData(that, result[i]));
+                sumRow = sumData(that, result[i])
+                if (!$.isEmptyObject(sumRow)) {
+                    result[i].push(sumRow);
+                }
             }
             newRow = {};
         }
@@ -132,6 +151,10 @@
         'expandAll'
     ]);
 
+    $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
+        groupBySumGroup: false
+    });
+
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
         _init = BootstrapTable.prototype.init,
         _initData = BootstrapTable.prototype.initData;