浏览代码

Merge pull request #281 from ice5050/feature/filterBy

bootstrap table method #filterBy
文翼 11 年之前
父节点
当前提交
3e45da032e
共有 1 个文件被更改,包括 23 次插入5 次删除
  1. 23 5
      src/bootstrap-table.js

+ 23 - 5
src/bootstrap-table.js

@@ -651,8 +651,19 @@
 
 
         if (this.options.sidePagination !== 'server') {
         if (this.options.sidePagination !== 'server') {
             var s = this.searchText && this.searchText.toLowerCase();
             var s = this.searchText && this.searchText.toLowerCase();
+            var f = $.isEmptyObject(this.filterColumns) ? null: this.filterColumns;
 
 
-            this.data = s ? $.grep(this.options.data, function (item, i) {
+            // Check filter
+            this.data = f ? $.grep(this.options.data, function (item, i) {
+                for (var key in f) {
+                    if (item[key] !== f[key]) {
+                        return false;
+                    }
+                }
+                return true;
+            }) : this.options.data;
+
+            this.data = s ? $.grep(this.data, function (item, i) {
                 for (var key in item) {
                 for (var key in item) {
                     key = $.isNumeric(key) ? parseInt(key, 10) : key;
                     key = $.isNumeric(key) ? parseInt(key, 10) : key;
                     var value = item[key];
                     var value = item[key];
@@ -670,7 +681,7 @@
                     }
                     }
                 }
                 }
                 return false;
                 return false;
-            }) : this.options.data;
+            }) : this.data;
         }
         }
     };
     };
 
 
@@ -803,7 +814,7 @@
             if (this.options.pageList.length < 2 || this.options.totalRows <= this.options.pageList[1]) {
             if (this.options.pageList.length < 2 || this.options.totalRows <= this.options.pageList[1]) {
                 this.$pagination.find('span.page-list').hide();
                 this.$pagination.find('span.page-list').hide();
             }
             }
-            
+
             // when data is empty, hide the pagination
             // when data is empty, hide the pagination
             this.$pagination[this.getData().length ? 'show' : 'hide']();
             this.$pagination[this.getData().length ? 'show' : 'hide']();
         }
         }
@@ -1313,7 +1324,7 @@
     };
     };
 
 
     BootstrapTable.prototype.getData = function () {
     BootstrapTable.prototype.getData = function () {
-        return this.searchText ? this.data : this.options.data;
+        return (this.searchText || !$.isEmptyObject(this.filterColumns)) ? this.data : this.options.data;
     };
     };
 
 
     BootstrapTable.prototype.load = function (data) {
     BootstrapTable.prototype.load = function (data) {
@@ -1445,6 +1456,12 @@
         this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
         this.toggleColumn(getFieldIndex(this.options.columns, field), false, true);
     };
     };
 
 
+    BootstrapTable.prototype.filterBy = function (columns) {
+        this.filterColumns = $.isEmptyObject(columns) ? {}: columns;
+        this.options.pageNumber = 1;
+        this.initSearch();
+        this.updatePagination();
+    };
 
 
     // BOOTSTRAP TABLE PLUGIN DEFINITION
     // BOOTSTRAP TABLE PLUGIN DEFINITION
     // =======================
     // =======================
@@ -1459,7 +1476,8 @@
         'resetView',
         'resetView',
         'destroy',
         'destroy',
         'showLoading', 'hideLoading',
         'showLoading', 'hideLoading',
-        'showColumn', 'hideColumn'
+        'showColumn', 'hideColumn',
+        'filterBy'
     ];
     ];
 
 
     $.fn.bootstrapTable = function (option, _relatedTarget) {
     $.fn.bootstrapTable = function (option, _relatedTarget) {