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.
 * 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: ``
 * 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
 ## This plugin saves
 
 
 * Sort order
 * Sort order

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

@@ -46,6 +46,10 @@
             return;
             return;
         }
         }
 
 
+        if ($.inArray(cookieName.toLowerCase(), that.options.cookiesEnabled) === -1) {
+            return;
+        }
+
         cookieName = that.options.cookieIdTable + '.' + cookieName;
         cookieName = that.options.cookieIdTable + '.' + cookieName;
         if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
         if (!cookieName || /^(?:expires|max\-age|path|domain|secure)$/i.test(cookieName)) {
             return false;
             return false;
@@ -55,11 +59,17 @@
         return true;
         return true;
     };
     };
 
 
-    var getCookie = function (tableName, cookieName) {
-        cookieName = tableName + '.' + cookieName;
+    var getCookie = function (that, tableName, cookieName) {
         if (!cookieName) {
         if (!cookieName) {
             return null;
             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;
         return decodeURIComponent(document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + encodeURIComponent(cookieName).replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1')) || null;
     };
     };
 
 
@@ -117,6 +127,7 @@
         cookieDomain: null,
         cookieDomain: null,
         cookieSecure: null,
         cookieSecure: null,
         cookieIdTable: '',
         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
         //internal variable
         filterControls: [],
         filterControls: [],
         filterControlValuesLoaded: false
         filterControlValuesLoaded: false
@@ -143,6 +154,10 @@
         this.options.filterControls = [];
         this.options.filterControls = [];
         this.options.filterControlValuesLoaded = false;
         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) {
         if (this.options.filterControl) {
             var that = this;
             var that = this;
             this.$el.on('column-search.bs.table', function (e, field, text) {
             this.$el.on('column-search.bs.table', function (e, field, text) {
@@ -167,7 +182,7 @@
                 setTimeout(function () {
                 setTimeout(function () {
                     if (!that.options.filterControlValuesLoaded) {
                     if (!that.options.filterControlValuesLoaded) {
                         that.options.filterControlValuesLoaded = true;
                         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) {
                         if (filterControl) {
                             var field = null,
                             var field = null,
                                 result = [],
                                 result = [],
@@ -208,12 +223,12 @@
             return;
             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
         //sortOrder
         this.options.sortOrder = sortOrderCookie ? sortOrderCookie : 'asc';
         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
 ### groupBySumGroup
 
 
 * type: Boolean
 * type: Boolean
-* description: Set to True to include a sum row.
+* description: Set to True to include a sum per column.
 * default: `false`
 * default: `false`
 
 
 ### groupByInitExpanded
 ### groupByInitExpanded
 
 
 * type: Boolean
 * 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
 ## 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.
 * 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
 ## Known issues
 
 
 ### OnSort
 ### OnSort

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

@@ -14,16 +14,43 @@
         obj = {},
         obj = {},
         parentId = undefined;
         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 sumData = function (that, data) {
         var sumRow = {};
         var sumRow = {};
         $.each(data, function (i, row) {
         $.each(data, function (i, row) {
             for(var prop in row) {
             for(var prop in row) {
                 if (!row.IsParent) {
                 if (!row.IsParent) {
                     if (!isNaN(parseFloat(row[prop]))) {
                     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 makeGrouped = function (that, data) {
         var newRow = {},
         var newRow = {},
-            newData = [];
+            newData = [],
+            sumRow = {};
 
 
         var result = groupBy(data, function (item) {
         var result = groupBy(data, function (item) {
             return [item[that.options.groupByField]];
             return [item[that.options.groupByField]];
@@ -89,7 +117,10 @@
             newRow.IsParent = true;
             newRow.IsParent = true;
             result[i].unshift(newRow);
             result[i].unshift(newRow);
             if (that.options.groupBySumGroup) {
             if (that.options.groupBySumGroup) {
-                result[i].push(sumData(that, result[i]));
+                sumRow = sumData(that, result[i])
+                if (!$.isEmptyObject(sumRow)) {
+                    result[i].push(sumRow);
+                }
             }
             }
             newRow = {};
             newRow = {};
         }
         }
@@ -109,7 +140,7 @@
         groupBy: false,
         groupBy: false,
         groupByField: '',
         groupByField: '',
         groupBySumGroup: false,
         groupBySumGroup: false,
-        groupByInitExpanded: false,
+        groupByInitExpanded: undefined, //node, 'all'
         //internal variables
         //internal variables
         loaded: false,
         loaded: false,
         originalData: undefined
         originalData: undefined
@@ -120,6 +151,10 @@
         'expandAll'
         'expandAll'
     ]);
     ]);
 
 
+    $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
+        groupBySumGroup: false
+    });
+
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
         _init = BootstrapTable.prototype.init,
         _init = BootstrapTable.prototype.init,
         _initData = BootstrapTable.prototype.initData;
         _initData = BootstrapTable.prototype.initData;
@@ -156,8 +191,12 @@
                         }
                         }
                     }, true);
                     }, 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) {
     BootstrapTable.prototype.expandNode = function (id) {
-        this.$el.treetable('expandNode', id);
+        id = getParentRowId(this, id);
+        if (id !== undefined) {
+            this.$el.treetable('expandNode', id);
+        }
     }
     }
 }(jQuery);
 }(jQuery);