Browse Source

Merge pull request #1168 from djhvscf/master

Support for expand all nodes or expand just one node and added sumGroup option per column
文翼 10 years ago
parent
commit
d1fef9fe10

+ 6 - 0
src/extensions/cookie/README.md

@@ -47,6 +47,12 @@ Use Plugin: [bootstrap-table-cookie](https://github.com/wenzhixin/bootstrap-tabl
 * description: You must set this property if the cookie property is enabled to set an unique cookie with an identifier for each table in your page or project. You must set this property because we need create cookies with an identifier.
 * default: ``
 
+### cookiesEnabled
+
+* type: Array
+* description: Set this array with the table properties (sortOrder, sortName, pageNumber, pageList, columns, searchText, filterControl) that you want to save
+* default: `['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl']`
+
 ## This plugin saves
 
 * Sort order

+ 24 - 9
src/extensions/cookie/bootstrap-table-cookie.js

@@ -46,6 +46,10 @@
             return;
         }
 
+        if ($.inArray(cookieName.toLowerCase(), that.options.cookiesEnabled) === -1) {
+            return;
+        }
+
         cookieName = that.options.cookieIdTable + '.' + cookieName;
         if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
             return false;
@@ -55,11 +59,17 @@
         return true;
     };
 
-    var getCookie = function (tableName, cookieName) {
-        cookieName = tableName + '.' + cookieName;
+    var getCookie = function (that, tableName, cookieName) {
         if (!cookieName) {
             return null;
         }
+
+        if ($.inArray(cookieName.toLowerCase(), that.options.cookiesEnabled) === -1) {
+            return null;
+        }
+
+        cookieName = tableName + '.' + cookieName;
+
         return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null;
     };
 
@@ -117,6 +127,7 @@
         cookieDomain: null,
         cookieSecure: null,
         cookieIdTable: '',
+        cookiesEnabled: ['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl'],
         //internal variable
         filterControls: [],
         filterControlValuesLoaded: false
@@ -143,6 +154,10 @@
         this.options.filterControls = [];
         this.options.filterControlValuesLoaded = false;
 
+
+        this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
+            this.options.cookiesEnabled.replace('[', '').replace(']', '').replace(/ /g, '').toLowerCase().split(',') : this.options.cookiesEnabled;
+
         if (this.options.filterControl) {
             var that = this;
             this.$el.on('column-search.bs.table', function (e, field, text) {
@@ -167,7 +182,7 @@
                 setTimeout(function () {
                     if (!that.options.filterControlValuesLoaded) {
                         that.options.filterControlValuesLoaded = true;
-                        var filterControl = JSON.parse(getCookie(that.options.cookieIdTable, cookieIds.filterControl));
+                        var filterControl = JSON.parse(getCookie(that, that.options.cookieIdTable, cookieIds.filterControl));
                         if (filterControl) {
                             var field = null,
                                 result = [],
@@ -208,12 +223,12 @@
             return;
         }
 
-        var sortOrderCookie = getCookie(this.options.cookieIdTable, cookieIds.sortOrder),
-            sortOrderNameCookie = getCookie(this.options.cookieIdTable, cookieIds.sortName),
-            pageNumberCookie = getCookie(this.options.cookieIdTable, cookieIds.pageNumber),
-            pageListCookie = getCookie(this.options.cookieIdTable, cookieIds.pageList),
-            columnsCookie = JSON.parse(getCookie(this.options.cookieIdTable, cookieIds.columns)),
-            searchTextCookie = getCookie(this.options.cookieIdTable, cookieIds.searchText);
+        var sortOrderCookie = getCookie(this, this.options.cookieIdTable, cookieIds.sortOrder),
+            sortOrderNameCookie = getCookie(this, this.options.cookieIdTable, cookieIds.sortName),
+            pageNumberCookie = getCookie(this, this.options.cookieIdTable, cookieIds.pageNumber),
+            pageListCookie = getCookie(this, this.options.cookieIdTable, cookieIds.pageList),
+            columnsCookie = JSON.parse(getCookie(this, this.options.cookieIdTable, cookieIds.columns)),
+            searchTextCookie = getCookie(this, this.options.cookieIdTable, cookieIds.searchText);
 
         //sortOrder
         this.options.sortOrder = sortOrderCookie ? sortOrderCookie : 'asc';

+ 12 - 3
src/extensions/group-by/README.md

@@ -27,14 +27,14 @@ 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
 
 * type: Boolean
-* description: Set to True to expand the first node of the table.
-* default: `false`
+* description: You can use the node number (parent row index) or you can use the `all` option in order to expand all nodes of the table.
+* default: `undefined`
 
 ## Methods
 
@@ -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

+ 51 - 9
src/extensions/group-by/bootstrap-table-group-by.js

@@ -14,16 +14,43 @@
         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]');
+
+        for (var i = 0; i < parentRows.length; i++) {
+            if (i === id) {
+                return $(parentRows[i]).attr('data-tt-id');
+            }
+        }
+
+        return undefined;
+    };
+
     var sumData = function (that, data) {
         var sumRow = {};
         $.each(data, function (i, row) {
             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];
                     }
                 }
             }
@@ -78,7 +105,8 @@
 
     var makeGrouped = function (that, data) {
         var newRow = {},
-            newData = [];
+            newData = [],
+            sumRow = {};
 
         var result = groupBy(data, function (item) {
             return [item[that.options.groupByField]];
@@ -89,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 = {};
         }
@@ -109,7 +140,7 @@
         groupBy: false,
         groupByField: '',
         groupBySumGroup: false,
-        groupByInitExpanded: false,
+        groupByInitExpanded: undefined, //node, 'all'
         //internal variables
         loaded: false,
         originalData: undefined
@@ -120,6 +151,10 @@
         'expandAll'
     ]);
 
+    $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
+        groupBySumGroup: false
+    });
+
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
         _init = BootstrapTable.prototype.init,
         _initData = BootstrapTable.prototype.initData;
@@ -156,8 +191,12 @@
                         }
                     }, true);
 
-                    if (that.options.groupByInitExpanded) {
-                        that.expandNode('0');
+                    if (that.options.groupByInitExpanded !== undefined) {
+                        if (typeof that.options.groupByInitExpanded === 'number') {
+                            that.expandNode(that.options.groupByInitExpanded);
+                        } else if (that.options.groupByInitExpanded.toLowerCase() === 'all') {
+                            that.expandAll();
+                        }
                     }
                 });
             }
@@ -184,6 +223,9 @@
     };
 
     BootstrapTable.prototype.expandNode = function (id) {
-        this.$el.treetable('expandNode', id);
+        id = getParentRowId(this, id);
+        if (id !== undefined) {
+            this.$el.treetable('expandNode', id);
+        }
     }
 }(jQuery);