ソースを参照

Remove extra db call to server when possible

Bootstrap table does a db call for initial data to the server. This
causes the client to make two db calls. one when the client initializes(
gets default sorting), and another when filtering the database based on
the cookies that are saved on the browser. this change removes that
extra initial db call under three
conditions:

1) the cookies option is set to true
2) there are cookies associated to this table
3) all select dropdown filters have default values

this change also updates the documentation for filterControl, in order
to add the proper usage for the property filterData.
ioctaptceb 9 年 前
コミット
617ad18f03

+ 46 - 1
src/extensions/cookie/bootstrap-table-cookie.js

@@ -151,6 +151,7 @@
     var BootstrapTable = $.fn.bootstrapTable.Constructor,
         _init = BootstrapTable.prototype.init,
         _initTable = BootstrapTable.prototype.initTable,
+        _initServer = BootstrapTable.prototype.initServer,
         _onSort = BootstrapTable.prototype.onSort,
         _onPageNumber = BootstrapTable.prototype.onPageNumber,
         _onPageListChange = BootstrapTable.prototype.onPageListChange,
@@ -221,6 +222,40 @@
         _init.apply(this, Array.prototype.slice.apply(arguments));
     };
 
+    BootstrapTable.prototype.initServer = function () {
+        var bootstrapTable = this,
+            selectsWithoutDefaults = [],
+
+            columnHasSelectControl = function (column) {
+                return column.filterControl && column.filterControl === 'select';
+            },
+
+            columnHasDefaultSelectValues = function (column) {
+                return column.filterData && column.filterData !== 'column';
+            },
+
+            cookiesPresent = function() {
+                return bootstrapTable.options.cookie && bootstrapTable.getCookies(bootstrapTable);
+            };
+
+        selectsWithoutDefaults = $.grep(bootstrapTable.columns, function(column) {
+            return columnHasSelectControl(column) && !columnHasDefaultSelectValues(column);
+        });
+
+        // reset variable to original initServer function, so that future calls to initServer
+        // use the original function from this point on.
+        BootstrapTable.prototype.initServer = _initServer;
+
+        // early return if we don't need to populate any select values with cookie values
+        if (cookiesPresent() && selectsWithoutDefaults.length === 0) {
+            return;
+        }
+
+        // call BootstrapTable.prototype.initServer
+        _initServer.apply(this, Array.prototype.slice.apply(arguments));
+    }
+
+
     BootstrapTable.prototype.initTable = function () {
         _initTable.apply(this, Array.prototype.slice.apply(arguments));
         this.initCookie();
@@ -310,7 +345,7 @@
 
         setCookie(this, cookieIds.columns, JSON.stringify(visibleColumns));
     };
-    
+
     BootstrapTable.prototype.selectPage = function (page) {
         _selectPage.apply(this, Array.prototype.slice.apply(arguments));
         setCookie(this, cookieIds.pageNumber, page);
@@ -325,6 +360,16 @@
         }
     };
 
+    BootstrapTable.prototype.getCookies = function(bootstrapTable) {
+        var cookies = [];
+        $.each( cookieIds, function( key, value ) {
+            var cookie = JSON.parse(getCookie(bootstrapTable, bootstrapTable.options.cookieIdTable, cookieIds.filterControl));
+            cookies.concat(cookie);
+        });
+
+        return cookies;
+    };
+
     BootstrapTable.prototype.deleteCookie = function (cookieName) {
         if ((cookieName === '') || (!cookieEnabled())) {
             return;

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

@@ -47,6 +47,13 @@ Dependence if you use the datepicker option: [bootstrap-datepicker](https://gith
 * description: Set to true if you want to use the strict search mode.
 * default: `false`
 
+### filterData
+
+* type: Object
+* description: Used to set default values for the `select` filterControl. Use this way:(in the document head)`var filterDefaults = {somekey: 'somevalue'}` (in the column th definition)`data-filter-data='var:filterDefaults'`. In order for the server to make only one call with serverside pagination, this field must be set for all selects. Otherwise, the client will make an initial call to the server to populate the dropdown list with data.
+* default: `undefined`
+
+
 ### Icons
 * clear: 'glyphicon-trash icon-clear'