ソースを参照

Added a new option which allows to collapse groups by default (#5293)

Dustin Utecht 5 年 前
コミット
8796f98fbe

+ 18 - 0
site/docs/extensions/group-by-v2.md

@@ -84,3 +84,21 @@ toc: true
    Set `true` to show icons if the group is collapsed or expanded (see groupByToggle).
 
 - **Default:** `false`
+
+### groupByCollapsedGroups
+
+- **attribute:** `data-group-by-collapsed-groups`
+
+- **type:** `Array|Function`
+
+- **Detail:**
+
+  All group keys (which are in this array will be collapsed by default.   
+  The value of this option can be:
+  - A variable (array)
+  - An Array string e.g. `["circle"]`
+  - A function (returns an array) which gets as parameters:
+     - The group key
+     - The entries of the group 
+
+- **Default:** `[]`

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

@@ -32,7 +32,8 @@ $.extend($.fn.bootstrapTable.defaults, {
   groupByField: '',
   groupByFormatter: undefined,
   groupByToggle: false,
-  groupByShowToggleIcon: false
+  groupByShowToggleIcon: false,
+  groupByCollapsedGroups: []
 })
 
 const Utils = $.fn.bootstrapTable.utils
@@ -96,6 +97,10 @@ BootstrapTable.prototype.initSort = function (...args) {
           item._data = {}
         }
 
+        if (this.isCollapsed(key, value)) {
+          item._class = 'hidden'
+        }
+
         item._data['parent-index'] = index
       })
 
@@ -151,8 +156,13 @@ BootstrapTable.prototype.initBody = function (...args) {
         '>', formattedValue
       )
 
+      let icon = this.options.icons.collapseGroup
+      if (this.isCollapsed(item.name, item.data)) {
+        icon = this.options.icons.expandGroup
+      }
+
       if (this.options.groupByToggle && this.options.groupByShowToggleIcon) {
-        html.push(`<span class="float-right ${this.options.iconsPrefix} ${this.options.icons.collapseGroup}"></span>`)
+        html.push(`<span class="float-right ${this.options.iconsPrefix} ${icon}"></span>`)
       }
 
       html.push('</td></tr>')
@@ -218,6 +228,17 @@ BootstrapTable.prototype.uncheckGroup = function (index) {
   this.checkGroup_(index, false)
 }
 
+BootstrapTable.prototype.isCollapsed = function (groupKey, items) {
+  if (this.options.groupByCollapsedGroups) {
+    const collapsedGroups = Utils.calculateObjectValue(this, this.options.groupByCollapsedGroups, [groupKey, items], true)
+    if ($.inArray(groupKey, collapsedGroups) > -1) {
+      return true
+    }
+  }
+
+  return false
+}
+
 BootstrapTable.prototype.checkGroup_ = function (index, checked) {
   const rowsBefore = this.getSelections()
   let rows