浏览代码

Added a new cookie "hiddenColumns" which will replace the "columns" cookies in a few versions (#6310)

Dustin Utecht 3 年之前
父节点
当前提交
a96b85cbd9
共有 2 个文件被更改,包括 33 次插入12 次删除
  1. 3 2
      site/docs/extensions/cookie.md
  2. 30 10
      src/extensions/cookie/bootstrap-table-cookie.js

+ 3 - 2
site/docs/extensions/cookie.md

@@ -178,9 +178,9 @@ toc: true
 
 - **Detail:**
 
-   Set this array with the table properties (sortOrder, sortName, sortPriority, pageNumber, pageList, columns, searchText, filterControl) that you want to save
+   Set this array with the table properties (sortOrder, sortName, sortPriority, pageNumber, pageList, hiddenColumns, columns, searchText, filterControl) that you want to save
 
-- **Default:** `['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.sortPriority', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl', 'bs.table.cardView']`
+- **Default:** `['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.sortPriority', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.hiddenColumns', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl', 'bs.table.cardView']`
 
 ## Methods
 
@@ -210,4 +210,5 @@ toc: true
 * Sort name
 * Multiple Sort order
 * Visible columns
+* Hidden columns
 * Card view state

+ 30 - 10
src/extensions/cookie/bootstrap-table-cookie.js

@@ -12,6 +12,7 @@ const UtilsCookie = {
     pageNumber: 'bs.table.pageNumber',
     pageList: 'bs.table.pageList',
     columns: 'bs.table.columns',
+    hiddenColumns: 'bs.table.hiddenColumns',
     cardView: 'bs.table.cardView',
     searchText: 'bs.table.searchText',
     reorderColumns: 'bs.table.reorderColumns',
@@ -178,7 +179,7 @@ $.extend($.fn.bootstrapTable.defaults, {
   cookiesEnabled: [
     'bs.table.sortOrder', 'bs.table.sortName', 'bs.table.sortPriority',
     'bs.table.pageNumber', 'bs.table.pageList',
-    'bs.table.columns', 'bs.table.searchText',
+    'bs.table.hiddenColumns', 'bs.table.columns', 'bs.table.searchText',
     'bs.table.filterControl', 'bs.table.filterBy',
     'bs.table.reorderColumns', 'bs.table.cardView'
   ],
@@ -361,7 +362,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
     if (!this.options.cookie) {
       return
     }
-    UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
+    UtilsCookie.setCookie(this, UtilsCookie.cookieIds.hiddenColumns, JSON.stringify(this.getHiddenColumns().map(column => column.field)))
   }
 
   _toggleAllColumns (...args) {
@@ -369,7 +370,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
     if (!this.options.cookie) {
       return
     }
-    UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns, JSON.stringify(this.getVisibleColumns().map(column => column.field)))
+    UtilsCookie.setCookie(this, UtilsCookie.cookieIds.hiddenColumns, JSON.stringify(this.getHiddenColumns().map(column => column.field)))
   }
 
   toggleView () {
@@ -435,6 +436,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
     const cardViewCookie = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.cardView)
 
     const columnsCookieValue = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.columns)
+    const hiddenColumnsCookieValue = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.hiddenColumns)
 
     if (typeof columnsCookieValue === 'boolean' && !columnsCookieValue) {
       throw new Error('The cookie value of filterBy must be a json!')
@@ -448,6 +450,14 @@ $.BootstrapTable = class extends $.BootstrapTable {
       throw new Error('Could not parse the json of the columns cookie!', columnsCookieValue)
     }
 
+    let hiddenColumnsCookie = {}
+
+    try {
+      hiddenColumnsCookie = JSON.parse(hiddenColumnsCookieValue)
+    } catch (e) {
+      throw new Error('Could not parse the json of the hidden columns cookie!', hiddenColumnsCookieValue)
+    }
+
     try {
       sortPriorityCookie = JSON.parse(sortPriorityCookie)
     } catch (e) {
@@ -483,7 +493,22 @@ $.BootstrapTable = class extends $.BootstrapTable {
     // cardView
     this.options.cardView = cardViewCookie === 'true' ? cardViewCookie : false
 
-    if (columnsCookie) {
+    if (hiddenColumnsCookie) {
+      for (const column of this.columns) {
+        column.visible = !hiddenColumnsCookie.filter(columnField => {
+          if (this.isSelectionColumn(column)) {
+            return true
+          }
+
+          return columnField === column.field
+        }).length > 0 || !column.switchable
+      }
+    } else if (columnsCookie) {
+      /**
+       * This is needed for the old saved cookies!
+       * It can be removed in 2-3 Versions Later!!
+       * TODO: Remove this part (column cookie) some versions later e.g. 1.22.0
+       */
       for (const column of this.columns) {
         if (!column.switchable) {
           continue
@@ -493,11 +518,6 @@ $.BootstrapTable = class extends $.BootstrapTable {
           if (this.isSelectionColumn(column)) {
             return true
           }
-          /**
-           * This is needed for the old saved cookies or the table will show no columns!
-           * It can be removed in 2-3 Versions Later!!
-           * TODO: Remove this part some versions later e.g. 1.17.3
-           */
           if (columnField instanceof Object) {
             return columnField.field === column.field
           }
@@ -514,7 +534,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
     $.each(UtilsCookie.cookieIds, (key, value) => {
       cookies[key] = UtilsCookie.getCookie(bootstrapTable, value)
-      if (key === 'columns') {
+      if (key === 'columns' || key === 'hiddenColumns' || key === 'sortPriority') {
         cookies[key] = JSON.parse(cookies[key])
       }
     })