Browse Source

Merge pull request #1942 from ioctaptceb/refactor-filter-cookies-to-avoid-unnecessary-database-calls

refactor(extension): refactor filter cookies extension to avoid dbcalls
wenzhixin 9 years ago
parent
commit
499b1dd12e

+ 1 - 0
src/bootstrap-table.js

@@ -1236,6 +1236,7 @@
             this.data = f ? $.grep(this.options.data, function (item, i) {
             this.data = f ? $.grep(this.options.data, function (item, i) {
                 for (var key in f) {
                 for (var key in f) {
                     if ($.isArray(f[key])) {
                     if ($.isArray(f[key])) {
+                        // TODO: remove this extra if statement
                         if ($.inArray(item[key], f[key]) === -1) {
                         if ($.inArray(item[key], f[key]) === -1) {
                             return false;
                             return false;
                         }
                         }

+ 33 - 26
src/extensions/cookie/bootstrap-table-cookie.js

@@ -133,6 +133,38 @@
         return cookieExpire === undefined ? '' : '; max-age=' + cookieExpire;
         return cookieExpire === undefined ? '' : '; max-age=' + cookieExpire;
     };
     };
 
 
+    var initCookieFilters = function (bootstrapTable) {
+        setTimeout(function () {
+            var parsedCookieFilters = JSON.parse(getCookie(bootstrapTable, bootstrapTable.options.cookieIdTable, cookieIds.filterControl));
+
+            if (!bootstrapTable.options.filterControlValuesLoaded && parsedCookieFilters) {
+                bootstrapTable.options.filterControlValuesLoaded = true;
+
+                var cachedFilters = {},
+                    header = getCurrentHeader(bootstrapTable),
+                    searchControls = getCurrentSearchControls(bootstrapTable),
+
+                    applyCookieFilters = function (element, filteredCookies) {
+                        $(filteredCookies).each(function (i, cookie) {
+                            $(element).val(cookie.text);
+                            cachedFilters[cookie.field] = cookie.text;
+                        });
+                    };
+
+                header.find(searchControls).each(function () {
+                    var field = $(this).closest('[data-field]').data('field'),
+                        filteredCookies = $.grep(parsedCookieFilters, function (cookie) {
+                            return cookie.field === field;
+                        });
+
+                    applyCookieFilters(this, filteredCookies);
+                });
+
+                bootstrapTable.initColumnSearch(cachedFilters);
+            }
+        }, 250);
+    }
+
     $.extend($.fn.bootstrapTable.defaults, {
     $.extend($.fn.bootstrapTable.defaults, {
         cookie: false,
         cookie: false,
         cookieExpire: '2h',
         cookieExpire: '2h',
@@ -191,32 +223,7 @@
                 }
                 }
 
 
                 setCookie(that, cookieIds.filterControl, JSON.stringify(that.options.filterControls));
                 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, 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).closest('[data-field]').data('field');
-                                result = $.grep(filterControl, function (valueObj) {
-                                    return valueObj.field === field;
-                                });
-
-                                if (result.length > 0) {
-                                    $(this).val(result[0].text);
-                                    that.onColumnSearch({currentTarget: $(this)});
-                                }
-                            });
-                        }
-                    }
-                }, 250);
-            });
+            }).on('post-body.bs.table', initCookieFilters(that));
         }
         }
         _init.apply(this, Array.prototype.slice.apply(arguments));
         _init.apply(this, Array.prototype.slice.apply(arguments));
 
 

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

@@ -574,6 +574,19 @@
         }) : this.data;
         }) : this.data;
     };
     };
 
 
+    BootstrapTable.prototype.initColumnSearch = function(filterColumnsDefaults) {
+        copyValues(this);
+
+        if (filterColumnsDefaults) {
+            this.filterColumnsPartial = filterColumnsDefaults;
+            this.updatePagination();
+
+            for (var filter in filterColumnsDefaults) {
+              this.trigger('column-search', filter, filterColumnsDefaults[filter]);
+            }
+        }
+    };
+
     BootstrapTable.prototype.onColumnSearch = function (event) {
     BootstrapTable.prototype.onColumnSearch = function (event) {
         if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
         if ($.inArray(event.keyCode, [37, 38, 39, 40]) > -1) {
             return;
             return;
@@ -601,7 +614,6 @@
 
 
         this.options.pageNumber = 1;
         this.options.pageNumber = 1;
         this.onSearch(event);
         this.onSearch(event);
-        this.updatePagination();
         this.trigger('column-search', $field, text);
         this.trigger('column-search', $field, text);
     };
     };