浏览代码

Fixing some issues with cookie

djhvscf 4 年之前
父节点
当前提交
95fe29bbcb

+ 24 - 0
site/docs/extensions/filter-control.md

@@ -192,6 +192,30 @@ toc: true
 
 - **Default:** `undefined`
 
+### filterControlMultipleSelect
+
+- **Attribute:** `data-filter-multiple-select`
+
+- **type:** `boolean`
+
+- **Detail:**
+
+   Use this option to configure the select as multipleSelect.
+
+- **Default:** `false`
+
+### filterControlMultipleSelectMultiple
+
+- **Attribute:** `data-filter-multiple-select-multiple`
+
+- **type:** `boolean`
+
+- **Detail:**
+
+   If the filterControlMultipleSelect option is set to true, use this option to configure the select as multiple select.
+
+- **Default:** `false`
+
 ### filterMultipleSelectOptions
 
 - **Attribute:** `data-filter-multiple-select-options`

+ 53 - 25
src/extensions/cookie/bootstrap-table-cookie.js

@@ -34,27 +34,18 @@ const UtilsCookie = {
 
     return searchControls
   },
-  cookieEnabled () {
+  isCookieSupportedByBrowser () {
     return !!(navigator.cookieEnabled)
   },
-  inArrayCookiesEnabled (cookieName, cookiesEnabled) {
-    let index = -1
-
-    for (let i = 0; i < cookiesEnabled.length; i++) {
-      if (cookieName.toLowerCase() === cookiesEnabled[i].toLowerCase()) {
-        index = i
-        break
-      }
-    }
-
-    return index
+  isCookieEnabled (that, cookieName) {
+    return that.options.cookiesEnabled.indexOf(cookieName) !== -1
   },
   setCookie (that, cookieName, cookieValue) {
-    if ((!that.options.cookie) || (!UtilsCookie.cookieEnabled()) || (that.options.cookieIdTable === '')) {
+    if (!that.options.cookie) {
       return
     }
 
-    if (UtilsCookie.inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
+    if (UtilsCookie.isCookieEnabled(that, cookieName)) {
       return
     }
 
@@ -99,7 +90,7 @@ const UtilsCookie = {
       return null
     }
 
-    if (UtilsCookie.inArrayCookiesEnabled(cookieName, that.options.cookiesEnabled) === -1) {
+    if (UtilsCookie.isCookieEnabled(that, cookieName)) {
       return null
     }
 
@@ -129,8 +120,8 @@ const UtilsCookie = {
         return null
     }
   },
-  deleteCookie (that, tableName, cookieName) {
-    cookieName = `${tableName}.${cookieName}`
+  deleteCookie (that, cookieName) {
+    cookieName = `${that.options.cookieIdTable}.${cookieName}`
 
     switch (that.options.cookieStorage) {
       case 'cookieStorage':
@@ -301,6 +292,9 @@ $.extend($.fn.bootstrapTable.utils, {
 $.BootstrapTable = class extends $.BootstrapTable {
   init () {
     if (this.options.cookie) {
+      if (this.options.cookieStorage === 'cookieStorage' && !UtilsCookie.isCookieSupportedByBrowser()) {
+        throw new Error('Cookies are not enabled in this browser.')
+      }
       // FilterBy logic
       const filterByCookieValue = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy)
 
@@ -370,12 +364,19 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
   initTable (...args) {
     super.initTable(...args)
+    if (!this.options.cookie) {
+      return
+    }
     this.initCookie()
   }
 
   onSort (...args) {
     super.onSort(...args)
 
+    if (!this.options.cookie) {
+      return
+    }
+
     if (this.options.sortName === undefined || this.options.sortOrder === undefined) {
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName)
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
@@ -388,44 +389,66 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
   onPageNumber (...args) {
     super.onPageNumber(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
 
   onPageListChange (...args) {
     super.onPageListChange(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageList, this.options.pageSize)
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
 
   onPagePre (...args) {
     super.onPagePre(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
 
   onPageNext (...args) {
     super.onPageNext(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, this.options.pageNumber)
   }
 
   _toggleColumn (...args) {
     super._toggleColumn(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
   }
 
   _toggleAllColumns (...args) {
     super._toggleAllColumns(...args)
-
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
   }
 
   selectPage (page) {
     super.selectPage(page)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.pageNumber, page)
   }
 
   onSearch (event) {
-    super.onSearch(event)
-
+    super.onSearch(event, false)
+    if (!this.options.cookie) {
+      return
+    }
     if (this.options.search) {
       UtilsCookie.setCookie(this, UtilsCookie.cookieIds.searchText, this.searchText)
     }
@@ -433,7 +456,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
   }
 
   initHeader (...args) {
-    if (this.options.reorderableColumns) {
+    if (this.options.reorderableColumns && this.options.cookie) {
       this.columnsSortOrder = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.reorderColumns))
     }
     super.initHeader(...args)
@@ -445,6 +468,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
   filterBy (...args) {
     super.filterBy(...args)
+    if (!this.options.cookie) {
+      return
+    }
     UtilsCookie.setCookie(this, UtilsCookie.cookieIds.filterBy, JSON.stringify(this.filterColumns))
   }
 
@@ -453,7 +479,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
       return
     }
 
-    if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '') || (!UtilsCookie.cookieEnabled())) {
+    if ((this.options.cookieIdTable === '') || (this.options.cookieExpire === '')) {
       console.error('Configuration error. Please review the cookieIdTable and the cookieExpire property. If the properties are correct, then this browser does not support cookies.')
       this.options.cookie = false // Make sure that the cookie extension is disabled
       return
@@ -488,7 +514,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
     // pageSize
     this.options.pageSize = pageListCookie ? pageListCookie === this.options.formatAllRows() ? pageListCookie : +pageListCookie : this.options.pageSize
     // searchText
-    this.options.searchText = searchTextCookie ? searchTextCookie : ''
+    if (UtilsCookie.isCookieEnabled(this, 'bs.table.searchText') && this.options.searchText === '') {
+      this.options.searchText = searchTextCookie ? searchTextCookie : ''
+    }
 
     if (columnsCookie) {
       for (const column of this.columns) {
@@ -525,10 +553,10 @@ $.BootstrapTable = class extends $.BootstrapTable {
   }
 
   deleteCookie (cookieName) {
-    if ((cookieName === '') || (!UtilsCookie.cookieEnabled())) {
+    if (!cookieName) {
       return
     }
 
-    UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds[cookieName])
+    UtilsCookie.deleteCookie(this, UtilsCookie.cookieIds[cookieName])
   }
 }

+ 16 - 7
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -33,7 +33,7 @@ $.extend($.fn.bootstrapTable.defaults, {
         '<select class="form-control bootstrap-table-filter-control-%s %s" %s style="width: 100%;" dir="%s"></select>',
         column.field,
         column.filterControlMultipleSelect ? 'fc-multipleselect' : '',
-        column.filterControlMultipleSelect ? 'multiple="multiple"' : '',
+        column.filterControlMultipleSelectMultiple ? 'multiple="multiple"' : '',
         UtilsFilterControl.getDirectionOfSelectOptions(
           options.alignmentSelectControlOptions
         )
@@ -59,10 +59,11 @@ $.extend($.fn.bootstrapTable.defaults, {
 $.extend($.fn.bootstrapTable.columnDefaults, {
   filterControl: undefined, // input, select, datepicker
   filterControlMultipleSelect: false,
+  filterControlMultipleSelectMultiple: false,
+  filterMultipleSelectOptions: {},
   filterDataCollector: undefined,
   filterData: undefined,
   filterDatepickerOptions: {},
-  filterMultipleSelectOptions: {},
   filterStrictSearch: false,
   filterStartsWithSearch: false,
   filterControlPlaceholder: '',
@@ -170,10 +171,12 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
   initBody () {
     super.initBody()
-    setTimeout(() => {
-      UtilsFilterControl.initFilterSelectControls(this)
-      UtilsFilterControl.setValues(this)
-    }, 3)
+    if (this.options.filterControl) {
+      setTimeout(() => {
+        UtilsFilterControl.initFilterSelectControls(this)
+        UtilsFilterControl.setValues(this)
+      }, 3)
+    }
   }
 
   initHeader () {
@@ -457,6 +460,8 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
         filterObj[$field] = filterByArray
         this.filterBy(filterObj)
+      } else {
+        this.filterBy({})
       }
     } else {
       const text = currentTargetValue ? currentTargetValue.trim() : ''
@@ -471,7 +476,11 @@ $.BootstrapTable = class extends $.BootstrapTable {
       }
     }
 
-    this.options.pageNumber = 1
+    // Cookie support
+    if (!this.options.cookie) {
+      this.options.pageNumber = 1
+    }
+
     this.onSearch({ currentTarget }, false)
   }
 

+ 6 - 1
src/extensions/filter-control/utils.js

@@ -55,7 +55,9 @@ export function existOptionInSelectControl (selectControl, value) {
 }
 
 export function addOptionToSelectControl (selectControl, _value, text, selected) {
-  const value = (_value === undefined || _value === null) ? '' : _value.toString().trim()
+  let value = (_value === undefined || _value === null) ? '' : _value.toString().trim()
+
+  value = value.replace(/(<([^>]+)>)/ig, '')
   const $selectControl = $(selectControl.get(selectControl.length - 1))
 
   if (!existOptionInSelectControl(selectControl, value)) {
@@ -84,6 +86,9 @@ export function sortSelectControl (selectControl, orderBy) {
 }
 
 export function fixHeaderCSS ({ $tableHeader }, pixels = '89px') {
+  if ($tableHeader.hasClass('table-sm') && pixels === '89px') {
+    $tableHeader.css('height', '49px')
+  }
   $tableHeader.css('height', pixels)
 }