浏览代码

Support for expand all nodes or expand just one

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

+ 2 - 2
src/extensions/group-by/README.md

@@ -33,8 +33,8 @@ You must include the bootstrap-table-group-by.css file in order to get the appro
 ### 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
 

+ 23 - 4
src/extensions/group-by/bootstrap-table-group-by.js

@@ -14,6 +14,18 @@
         obj = {},
         parentId = undefined;
 
+    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) {
@@ -109,7 +121,7 @@
         groupBy: false,
         groupByField: '',
         groupBySumGroup: false,
-        groupByInitExpanded: false,
+        groupByInitExpanded: undefined, //node, 'all'
         //internal variables
         loaded: false,
         originalData: undefined
@@ -156,8 +168,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 +200,9 @@
     };
 
     BootstrapTable.prototype.expandNode = function (id) {
-        this.$el.treetable('expandNode', id);
+        id = getParentRowId(this, id);
+        if (id !== undefined) {
+            this.$el.treetable('expandNode', id);
+        }
     }
 }(jQuery);