浏览代码

Fixing cookie integration

djhvscf 4 年之前
父节点
当前提交
c39a092976

+ 15 - 15
src/extensions/cookie/bootstrap-table-cookie.js

@@ -45,7 +45,7 @@ const UtilsCookie = {
       return
     }
 
-    if (UtilsCookie.isCookieEnabled(that, cookieName)) {
+    if (!UtilsCookie.isCookieEnabled(that, cookieName)) {
       return
     }
 
@@ -90,7 +90,7 @@ const UtilsCookie = {
       return null
     }
 
-    if (UtilsCookie.isCookieEnabled(that, cookieName)) {
+    if (!UtilsCookie.isCookieEnabled(that, cookieName)) {
       return null
     }
 
@@ -195,7 +195,7 @@ const UtilsCookie = {
     setTimeout(() => {
       const parsedCookieFilters = JSON.parse(UtilsCookie.getCookie(bootstrapTable, bootstrapTable.options.cookieIdTable, UtilsCookie.cookieIds.filterControl))
 
-      if (!bootstrapTable.options.filterControlValuesLoaded && parsedCookieFilters) {
+      if (!bootstrapTable._filterControlValuesLoaded && parsedCookieFilters) {
 
         const cachedFilters = {}
         const header = UtilsCookie.getCurrentHeader(bootstrapTable)
@@ -217,6 +217,7 @@ const UtilsCookie = {
               element.value = cookie.text
               cachedFilters[cookie.field] = cookie.text
             } else if (cookie.text !== '' && element.tagName === 'SELECT') {
+              cachedFilters[cookie.field] = cookie.text
               for (let i = 0; i < element.length; i++) {
                 const currentElement = element[i]
 
@@ -231,7 +232,6 @@ const UtilsCookie = {
               option.text = cookie.text
               element.add(option, element[1])
               element.selectedIndex = 1
-              cachedFilters[cookie.field] = cookie.text
             }
           })
         }
@@ -250,7 +250,7 @@ const UtilsCookie = {
         })
 
         bootstrapTable.initColumnSearch(cachedFilters)
-        bootstrapTable.options.filterControlValuesLoaded = true
+        bootstrapTable._filterControlValuesLoaded = true
         bootstrapTable.initServer()
       }
     }, 250)
@@ -277,8 +277,8 @@ $.extend($.fn.bootstrapTable.defaults, {
   cookieCustomStorageSet: null,
   cookieCustomStorageDelete: null,
   // internal variable
-  filterControls: [],
-  filterControlValuesLoaded: false
+  _filterControls: [],
+  _filterControlValuesLoaded: false
 })
 
 $.fn.bootstrapTable.methods.push('getCookies')
@@ -312,8 +312,8 @@ $.BootstrapTable = class extends $.BootstrapTable {
       this.filterColumns = filterByCookie ? filterByCookie : {}
 
       // FilterControl logic
-      this.options.filterControls = []
-      this.options.filterControlValuesLoaded = false
+      this._filterControls = []
+      this._filterControlValuesLoaded = false
 
       this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
         this.options.cookiesEnabled.replace('[', '').replace(']', '')
@@ -326,21 +326,21 @@ $.BootstrapTable = class extends $.BootstrapTable {
         this.$el.on('column-search.bs.table', (e, field, text) => {
           let isNewField = true
 
-          for (let i = 0; i < that.options.filterControls.length; i++) {
-            if (that.options.filterControls[i].field === field) {
-              that.options.filterControls[i].text = text
+          for (let i = 0; i < that._filterControls.length; i++) {
+            if (that._filterControls[i].field === field) {
+              that._filterControls[i].text = text
               isNewField = false
               break
             }
           }
           if (isNewField) {
-            that.options.filterControls.push({
+            that._filterControls.push({
               field,
               text
             })
           }
 
-          UtilsCookie.setCookie(that, UtilsCookie.cookieIds.filterControl, JSON.stringify(that.options.filterControls))
+          UtilsCookie.setCookie(that, UtilsCookie.cookieIds.filterControl, JSON.stringify(that._filterControls))
         }).on('created-controls.bs.table', UtilsCookie.initCookieFilters(that))
       }
     }
@@ -351,7 +351,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
     if (
       this.options.cookie &&
       this.options.filterControl &&
-      !this.options.filterControlValuesLoaded
+      !this._filterControlValuesLoaded
     ) {
       const cookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterControl))
 

+ 9 - 5
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -440,6 +440,15 @@ $.BootstrapTable = class extends $.BootstrapTable {
       return
     }
     UtilsFilterControl.cacheValues(this)
+
+    // Cookie extension support
+    if (!this.options.cookie) {
+      this.options.pageNumber = 1
+    } else {
+      // Force call the initServer method in Cookie extension
+      this._filterControlValuesLoaded = true
+    }
+
     if ($.isEmptyObject(this.filterColumnsPartial)) {
       this.filterColumnsPartial = {}
     }
@@ -481,11 +490,6 @@ $.BootstrapTable = class extends $.BootstrapTable {
       }
     }
 
-    // Cookie support
-    if (!this.options.cookie) {
-      this.options.pageNumber = 1
-    }
-
     this.onSearch({ currentTarget }, false)
   }
 

+ 7 - 3
src/extensions/filter-control/utils.js

@@ -62,7 +62,11 @@ export function addOptionToSelectControl (selectControl, _value, text, selected)
   let value = (_value === undefined || _value === null) ? '' : _value.toString().trim()
 
   value = value.replace(/(<([^>]+)>)/ig, '').replace(/&[#A-Za-z0-9]+;/gi, '').trim()
-  const $selectControl = $(selectControl.get(selectControl.length - 1))
+
+  // Double check if the select control is a jquery object.
+  if (!(selectControl instanceof jQuery)) {
+    throw new Error ('SelectControl is not a jQuery instance.')
+  }
 
   if (!existOptionInSelectControl(selectControl, value)) {
     const option = $(`<option value="${value}">${text}</option>`)
@@ -71,7 +75,7 @@ export function addOptionToSelectControl (selectControl, _value, text, selected)
       option.attr('selected', true)
     }
 
-    $selectControl.append(option)
+    selectControl.append(option)
   }
 }
 
@@ -256,7 +260,7 @@ export function isFilterDataNotGiven ({ filterData }) {
 }
 
 export function hasSelectControlElement (selectControl) {
-  return selectControl && selectControl.length > 0 && selectControl.get(selectControl.length - 1).options.length === 0
+  return selectControl && selectControl.length > 0 // && selectControl.get(selectControl.length - 1).options.length === 0
 }
 
 export function initFilterSelectControls (that) {