浏览代码

Merge pull request #4845 from wenzhixin/feature/4840

added a new option to get/set/delete the values by a custom function
文翼 5 年之前
父节点
当前提交
a6e8605c33
共有 2 个文件被更改,包括 111 次插入5 次删除
  1. 53 1
      site/docs/extensions/cookie.md
  2. 58 4
      src/extensions/cookie/bootstrap-table-cookie.js

+ 53 - 1
site/docs/extensions/cookie.md

@@ -98,7 +98,10 @@ toc: true
 
 - **Detail:**
 
-   Set the storage that this extension will use. Use `cookieStorage` or `localStorage` or `sessionStorage`.
+   Set the storage that this extension will use. Use `cookieStorage` or `localStorage` or `sessionStorage` or `customStorage`.
+   
+   Info for `customStorage`:   
+   You have use `cookieCustomStorageGet`, `cookieCustomStorageSet` and `cookieCustomStorageDelete`. 
 
 - **Default:** `cookieStorage`
 
@@ -114,6 +117,55 @@ toc: true
 
 - **Default:** `['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl']`
 
+### cookieCustomStorageGet
+
+- **Attribute:** `data-cookie-custom-storage-get`
+
+- **type:** `function`
+
+- **parameter**
+  - cookieName - The name of the value e.g. the search
+
+- **Detail:**
+
+   This option allows to get the saved value from your custom function.   
+   This option is only required if you use `customStorage` on the `cookieStorage` option! 
+
+- **Default:** `undefined`
+
+### cookieCustomStorageSet
+
+- **Attribute:** `data-cookie-custom-storage-set`
+
+- **type:** `function`
+
+- **parameter**
+  - cookieName - The name of the value e.g. the search
+  - value - The value which will be saved
+
+- **Detail:**
+
+   This option allows to save values with your custom function.   
+   This option is only required if you use `customStorage` on the `cookieStorage` option! 
+
+- **Default:** `undefined`
+
+### cookieCustomStorageDelete
+
+- **Attribute:** `data-cookie-custom-storage-delete`
+
+- **type:** `function`
+
+- **parameter**
+  - cookieName - The name of the value e.g. the search
+
+- **Detail:**
+
+   This option allows to delete values with your custom function.      
+   This option is only required if you use `customStorage` on the `cookieStorage` option! 
+
+- **Default:** `undefined`
+
 ## Methods
 
 ### getCookies

+ 58 - 4
src/extensions/cookie/bootstrap-table-cookie.js

@@ -3,7 +3,7 @@
  * @webSite: http://djhvscf.github.io/Blog
  * @update zhixin wen <wenzhixin2010@gmail.com>
  */
-
+const Utils = $.fn.bootstrapTable.utils
 const UtilsCookie = {
   cookieIds: {
     sortOrder: 'bs.table.sortOrder',
@@ -74,6 +74,17 @@ const UtilsCookie = {
       case 'sessionStorage':
         sessionStorage.setItem(cookieName, cookieValue)
         break
+      case 'customStorage':
+        if (
+          !that.options.cookieCustomStorageSet
+          || !that.options.cookieCustomStorageGet
+          || !that.options.cookieCustomStorageDelete
+        ) {
+          throw new Error('The following options must be set while using the customStorage: cookieCustomStorageSet, cookieCustomStorageGet and cookieCustomStorageDelete')
+        }
+
+        Utils.calculateObjectValue(that.options, that.options.cookieCustomStorageSet, [cookieName, cookieValue], '')
+        break
       default:
         return false
     }
@@ -100,6 +111,16 @@ const UtilsCookie = {
         return localStorage.getItem(cookieName)
       case 'sessionStorage':
         return sessionStorage.getItem(cookieName)
+      case 'customStorage':
+        if (
+          !that.options.cookieCustomStorageSet
+          || !that.options.cookieCustomStorageGet
+          || !that.options.cookieCustomStorageDelete
+        ) {
+          throw new Error('The following options must be set while using the customStorage: cookieCustomStorageSet, cookieCustomStorageGet and cookieCustomStorageDelete')
+        }
+
+        return Utils.calculateObjectValue(that.options, that.options.cookieCustomStorageGet, [cookieName], '')
       default:
         return null
     }
@@ -122,6 +143,17 @@ const UtilsCookie = {
       case 'sessionStorage':
         sessionStorage.removeItem(cookieName)
         break
+      case 'customStorage':
+        if (
+          !that.options.cookieCustomStorageSet
+          || !that.options.cookieCustomStorageGet
+          || !that.options.cookieCustomStorageDelete
+        ) {
+          throw new Error('The following options must be set while using the customStorage: cookieCustomStorageSet, cookieCustomStorageGet and cookieCustomStorageDelete')
+        }
+
+        Utils.calculateObjectValue(that.options, that.options.cookieCustomStorageDelete, [cookieName], '')
+        break
       default:
         return false
     }
@@ -216,7 +248,7 @@ $.extend($.fn.bootstrapTable.defaults, {
     'bs.table.filterControl', 'bs.table.filterBy',
     'bs.table.reorderColumns'
   ],
-  cookieStorage: 'cookieStorage', // localStorage, sessionStorage
+  cookieStorage: 'cookieStorage', // localStorage, sessionStorage, customStorage
   // internal variable
   filterControls: [],
   filterControlValuesLoaded: false
@@ -234,7 +266,17 @@ $.BootstrapTable = class extends $.BootstrapTable {
   init () {
     if (this.options.cookie) {
       // FilterBy logic
-      const filterByCookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy))
+      const filterByCookieValue = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy)
+      if (typeof filterByCookieValue === 'boolean' && !filterByCookieValue) {
+        throw new Error('The cookie value of filterBy must be a json!')
+      }
+
+      let filterByCookie = {}
+      try {
+        filterByCookie = JSON.parse(filterByCookieValue)
+      } catch (e) {
+        throw new Error('Could not parse the json of the filterBy cookie!')
+      }
       this.filterColumns = filterByCookie ? filterByCookie : {}
 
       // FilterControl logic
@@ -377,9 +419,21 @@ $.BootstrapTable = class extends $.BootstrapTable {
     const sortOrderNameCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName)
     const pageNumberCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageNumber)
     const pageListCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageList)
-    const columnsCookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.columns))
     const searchTextCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.searchText)
 
+    const columnsCookieValue = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.columns)
+    if (typeof columnsCookieValue === 'boolean' && !columnsCookieValue) {
+      throw new Error('The cookie value of filterBy must be a json!')
+    }
+
+    let columnsCookie = {}
+    try {
+      columnsCookie = JSON.parse(columnsCookieValue)
+    } catch (e) {
+      throw new Error('Could not parse the json of the columns cookie!', columnsCookieValue)
+    }
+
+
     // sortOrder
     this.options.sortOrder = sortOrderCookie ? sortOrderCookie : this.options.sortOrder
     // sortName