ソースを参照

Adding support for filter-control extension

Dennis Hernández 10 年 前
コミット
3ebdad276f
2 ファイル変更91 行追加4 行削除
  1. 6 1
      src/bootstrap-table.js
  2. 85 3
      src/extensions/cookie/bootstrap-table-cookie.js

+ 6 - 1
src/bootstrap-table.js

@@ -385,6 +385,9 @@
         },
         onResetView: function () {
             return false;
+        },
+        onPostInit: function () {
+            return false;
         }
     };
 
@@ -478,7 +481,8 @@
         'expand-row.bs.table': 'onExpandRow',
         'collapse-row.bs.table': 'onCollapseRow',
         'refresh-options.bs.table': 'onRefreshOptions',
-        'reset-view.bs.table': 'onResetView'
+        'reset-view.bs.table': 'onResetView',
+        'post-init.bs.table': 'onPostInit'
     };
 
     BootstrapTable.prototype.init = function () {
@@ -492,6 +496,7 @@
         this.initPagination();
         this.initBody();
         this.initServer();
+        this.trigger('post-init');
     };
 
     BootstrapTable.prototype.initLocale = function () {

+ 85 - 3
src/extensions/cookie/bootstrap-table-cookie.js

@@ -15,7 +15,26 @@
         pageNumber: 'bs.table.pageNumber',
         pageList: 'bs.table.pageList',
         columns: 'bs.table.columns',
-        searchText: 'bs.table.searchText'
+        searchText: 'bs.table.searchText',
+        filterControl: 'bs.table.filterControl'
+    };
+
+    var getCurrentHeader = function (that) {
+        var header = that.$header;
+        if (that.options.height) {
+            header = that.$tableHeader;
+        }
+
+        return header;
+    };
+
+    var getCurrentSearchControls = function (that) {
+        var searchControls = 'select, input';
+        if (that.options.height) {
+            searchControls = 'table select, table input';
+        }
+
+        return searchControls;
     };
 
     var cookieEnabled = function () {
@@ -97,12 +116,17 @@
         cookiePath: null,
         cookieDomain: null,
         cookieSecure: null,
-        cookieIdTable: ''
+        cookieIdTable: '',
+        //internal variable
+        filterControls: [],
+        filterControlValuesLoaded: false,
+        filterControlSearchFired: false
     });
 
     $.fn.bootstrapTable.methods.push('deleteCookie');
 
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
+        _init = BootstrapTable.prototype.init,
         _initTable = BootstrapTable.prototype.initTable,
         _onSort = BootstrapTable.prototype.onSort,
         _onPageNumber = BootstrapTable.prototype.onPageNumber,
@@ -115,7 +139,64 @@
         _selectPage = BootstrapTable.prototype.selectPage,
         _onSearch = BootstrapTable.prototype.onSearch;
 
-    // init save data after initTable function
+    BootstrapTable.prototype.init = function () {
+        this.options.filterControls = [];
+        this.options.filterControlValuesLoaded = false;
+        this.options.filterControlSearchFired = false;
+
+        if (this.options.filterControl) {
+            var that = this;
+            this.$el.on('column-search.bs.table', function (e, field, text) {
+                var isNewField = true;
+
+                for (var i = 0; i < that.options.filterControls.length; i++) {
+                    if (that.options.filterControls[i].field === field) {
+                        that.options.filterControls[i].text = text;
+                        isNewField = false;
+                        break;
+                    }
+                }
+                if (isNewField) {
+                    that.options.filterControls.push({
+                        field: field,
+                        text: text
+                    });
+                }
+
+                setCookie(that, cookieIds.filterControl, JSON.stringify(that.options.filterControls));
+            }).on('post-body.bs.table', function () {
+                setTimeout(function () {
+                    if (!that.options.filterControlValuesLoaded) {
+                        that.options.filterControlValuesLoaded = true;
+                        var filterControl = JSON.parse(getCookie(that.options.cookieIdTable, cookieIds.filterControl));
+                        if (filterControl) {
+                            var field = null,
+                                result = [],
+                                header = getCurrentHeader(that),
+                                searchControls = getCurrentSearchControls(that);
+
+                            header.find(searchControls).each(function (index, ele) {
+                                field = $(this).parent().parent().parent().data('field');
+                                result = $.grep(filterControl, function (valueObj) {
+                                    return valueObj.field === field;
+                                });
+
+                                if (result.length > 0) {
+                                    $(this).val(result[0].text);
+                                    if (!that.options.filterControlSearchFired) {
+                                        that.options.filterControlSearchFired = true;
+                                        $(this).trigger(this.tagName === 'INPUT' ? 'keyup' : 'change');
+                                    }
+                                }
+                            });
+                        }
+                    }
+                }, 250);
+            });
+        }
+        _init.apply(this, Array.prototype.slice.apply(arguments));
+    };
+
     BootstrapTable.prototype.initTable = function () {
         _initTable.apply(this, Array.prototype.slice.apply(arguments));
         this.initCookie();
@@ -127,6 +208,7 @@
         }
 
         if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '') || (!cookieEnabled())) {
+            throw new Error("Configuration error. Please review the cookieIdTable, cookieExpire properties, if those properties are ok, then this browser does not support the cookies");
             return;
         }