浏览代码

bootstrap-table-group-by header formatter (#4109)

* bootstrap-table-group-by header formatter

An additional option for bootstrap-table-group-by to allow formatting of group headers.
Option is in the form of a function with the signature groupByFormatter(value, idx, data) where:
value: is the original header text
idx: is the index of the group
data: is an array of the row objects for this group
return: a string to be used as the group header html

This is useful when you require the group header to be formatted differently from the field value.

* Fixed Formatting

Remove spaces

* Add groupByFormatter docs

* Tidy up group by doc
Sam Boyne 7 年之前
父节点
当前提交
07c7f709f1
共有 2 个文件被更改,包括 29 次插入12 次删除
  1. 16 3
      site/docs/extensions/group-by-v2.md
  2. 13 9
      src/extensions/group-by-v2/bootstrap-table-group-by.js

+ 16 - 3
site/docs/extensions/group-by-v2.md

@@ -10,10 +10,9 @@ Use Plugin: [bootstrap-table-group-by-v2](https://github.com/wenzhixin/bootstrap
 You must include the bootstrap-table-group-by.css file in order to get the appropriate style
 You must include the bootstrap-table-group-by.css file in order to get the appropriate style
 
 
 ## Usage
 ## Usage
-
-{% highlight html %}
+```
 <script src="extensions/group-by-v2/bootstrap-table-group-by.js"></script>
 <script src="extensions/group-by-v2/bootstrap-table-group-by.js"></script>
-{% endhighlight %}
+```
 
 
 ## Options
 ## Options
 
 
@@ -36,3 +35,17 @@ You must include the bootstrap-table-group-by.css file in order to get the appro
    Set the fields name that you want to group the data.
    Set the fields name that you want to group the data.
 
 
 - **Default:** `''`
 - **Default:** `''`
+
+### groupByFormatter
+
+- **type:** `Function`
+
+- **Detail:**
+
+   The group row formatter function, takes three parameters:
+
+   * `value`: the group by value.
+   * `idx`: the index of the group.
+   * `data`: an array of rows in the group.
+
+- **Default:** `Undefined`

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

@@ -1,9 +1,9 @@
 /**
 /**
  * @author: Yura Knoxville
  * @author: Yura Knoxville
- * @version: v1.0.0
+ * @version: v1.1.0
  */
  */
 
 
-!function ($) {
+(function ($) {
 
 
     'use strict';
     'use strict';
 
 
@@ -27,7 +27,7 @@
         });
         });
         return flag ? str : '';
         return flag ? str : '';
     };
     };
-
+    
     var groupBy = function (array , f) {
     var groupBy = function (array , f) {
         var groups = {};
         var groups = {};
         array.forEach(function(o) {
         array.forEach(function(o) {
@@ -41,7 +41,8 @@
 
 
     $.extend($.fn.bootstrapTable.defaults, {
     $.extend($.fn.bootstrapTable.defaults, {
         groupBy: false,
         groupBy: false,
-        groupByField: ''
+        groupByField: '',
+        groupByFormatter: undefined
     });
     });
 
 
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
@@ -72,7 +73,8 @@
             $.each(groups, function(key, value) {
             $.each(groups, function(key, value) {
                 tableGroups.push({
                 tableGroups.push({
                     id: index,
                     id: index,
-                    name: key
+                    name: key,
+                    data: value
                 });
                 });
 
 
                 value.forEach(function(item) {
                 value.forEach(function(item) {
@@ -127,10 +129,13 @@
                         '</td>'
                         '</td>'
                     );
                     );
                 }
                 }
-
+                var formattedValue = item.name;
+                if (typeof(that.options.groupByFormatter) == "function") {
+                    formattedValue = that.options.groupByFormatter(item.name, item.id, item.data);
+                }
                 html.push('<td',
                 html.push('<td',
                     sprintf(' colspan="%s"', visibleColumns),
                     sprintf(' colspan="%s"', visibleColumns),
-                    '>', item.name, '</td>'
+                    '>', formattedValue, '</td>'
                 );
                 );
 
 
                 html.push('</tr>');
                 html.push('</tr>');
@@ -214,7 +219,6 @@
 
 
         this.$selectItem.filter(filter).prop('checked', checked);
         this.$selectItem.filter(filter).prop('checked', checked);
 
 
-
         this.updateRows();
         this.updateRows();
         this.updateSelected();
         this.updateSelected();
         if (checked) {
         if (checked) {
@@ -223,4 +227,4 @@
         this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
         this.trigger(checked ? 'check-all' : 'uncheck-all', rows);
     };
     };
 
 
-}(jQuery);
+})(jQuery);