浏览代码

Fix Filter-controls doesn't have any reset button

Fix #1050
Dennis Hernández 10 年之前
父节点
当前提交
f0ddf1f4a3

+ 7 - 0
src/extensions/filter-control/README.md

@@ -17,6 +17,13 @@ Dependence if you use the datepicker option: [bootstrap-datepicker](https://gith
 * description: Set true to add an `input` or `select` into the column.
 * default: `false`
 
+### filterShowClear
+
+* type: Boolean
+* description: Set true to add a button to clear all the controls added by this plugin
+* default: `false`
+
+
 ## Column options
 
 ### filterControl

+ 46 - 1
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -274,7 +274,8 @@
     $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
         filterControl: undefined,
         filterData: undefined,
-        filterDatepickerOptions: undefined
+        filterDatepickerOptions: undefined,
+        filterShowClear: false
     });
 
     $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
@@ -283,6 +284,7 @@
 
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
         _init = BootstrapTable.prototype.init,
+        _initToolbar = BootstrapTable.prototype.initToolbar,
         _initHeader = BootstrapTable.prototype.initHeader,
         _initBody = BootstrapTable.prototype.initBody,
         _initSearch = BootstrapTable.prototype.initSearch;
@@ -319,6 +321,30 @@
         _init.apply(this, Array.prototype.slice.apply(arguments));
     };
 
+    BootstrapTable.prototype.initToolbar = function () {
+        if (!this.showToolbar) {
+            this.showToolbar = this.options.filterControl;
+        }
+
+        _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
+
+        if (this.options.filterControl && this.options.filterShowClear) {
+            var $btnGroup = this.$toolbar.find('>.btn-group'),
+                $btnClear = $btnGroup.find('div.export');
+
+            if (!$btnClear.length) {
+              $btnClear = $([
+                    '<button class="btn btn-default " ' +
+                        'type="button">',
+                    '<i class="glyphicon glyphicon-trash icon-share"></i> ',
+                    '</button>',
+                    '</ul>'].join('')).appendTo($btnGroup);
+
+                $btnClear.off('click').on('click', $.proxy(this.refreshFilterControl, this));
+            }
+        }
+    };
+
     BootstrapTable.prototype.initHeader = function () {
         _initHeader.apply(this, Array.prototype.slice.apply(arguments));
 
@@ -408,4 +434,23 @@
         this.updatePagination();
         this.trigger('column-search', $field, text);
     };
+
+    BootstrapTable.prototype.refreshFilterControl = function () {
+        $.each(this.options.values, function (i, obj) {
+            obj.value = '';
+        });
+
+        setValues(this);
+
+        var controls = getCurrentHeader(this).find(getCurrentSearchControls(this)),
+            timeoutId = 0;
+
+        if (controls.length > 0) {
+            this.filterColumnsPartial = {};
+            clearTimeout(timeoutId);
+            timeoutId = setTimeout(function () {
+                $(controls[0]).trigger(controls[0].tagName === 'INPUT' ? 'keyup' : 'change');
+            }, this.options.searchTimeOut);
+        }
+    };
 }(jQuery);