Browse Source

Merge pull request #5721 from Naruto-kyun/cookie-multi-sort

Cookie multi sort
Dustin Utecht 4 years ago
parent
commit
9e9a3c3555
2 changed files with 50 additions and 8 deletions
  1. 3 2
      site/docs/extensions/cookie.md
  2. 47 6
      src/extensions/cookie/bootstrap-table-cookie.js

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

@@ -178,9 +178,9 @@ toc: true
 
 
 - **Detail:**
 - **Detail:**
 
 
-   Set this array with the table properties (sortOrder, sortName, pageNumber, pageList, columns, searchText, filterControl) that you want to save
+   Set this array with the table properties (sortOrder, sortName, sortPriority, pageNumber, pageList, columns, searchText, filterControl) that you want to save
 
 
-- **Default:** `['bs.table.sortOrder', 'bs.table.sortName', 'bs.table.pageNumber', 'bs.table.pageList', 'bs.table.columns', 'bs.table.searchText', 'bs.table.filterControl']`
+- **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']`
 
 
 ## Methods
 ## Methods
 
 
@@ -207,4 +207,5 @@ toc: true
 * Search text
 * Search text
 * Search filter control
 * Search filter control
 * Sort order
 * Sort order
+* Multiple Sort order
 * Visible columns
 * Visible columns

+ 47 - 6
src/extensions/cookie/bootstrap-table-cookie.js

@@ -8,6 +8,7 @@ const UtilsCookie = {
   cookieIds: {
   cookieIds: {
     sortOrder: 'bs.table.sortOrder',
     sortOrder: 'bs.table.sortOrder',
     sortName: 'bs.table.sortName',
     sortName: 'bs.table.sortName',
+    sortPriority: 'bs.table.sortPriority',
     pageNumber: 'bs.table.pageNumber',
     pageNumber: 'bs.table.pageNumber',
     pageList: 'bs.table.pageList',
     pageList: 'bs.table.pageList',
     columns: 'bs.table.columns',
     columns: 'bs.table.columns',
@@ -275,7 +276,7 @@ $.extend($.fn.bootstrapTable.defaults, {
   cookieSameSite: 'Lax',
   cookieSameSite: 'Lax',
   cookieIdTable: '',
   cookieIdTable: '',
   cookiesEnabled: [
   cookiesEnabled: [
-    'bs.table.sortOrder', 'bs.table.sortName',
+    'bs.table.sortOrder', 'bs.table.sortName', 'bs.table.sortPriority',
     'bs.table.pageNumber', 'bs.table.pageList',
     'bs.table.pageNumber', 'bs.table.pageList',
     'bs.table.columns', 'bs.table.searchText',
     'bs.table.columns', 'bs.table.searchText',
     'bs.table.filterControl', 'bs.table.filterBy',
     'bs.table.filterControl', 'bs.table.filterBy',
@@ -379,11 +380,28 @@ $.BootstrapTable = class extends $.BootstrapTable {
     if (this.options.sortName === undefined || this.options.sortOrder === undefined) {
     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.sortName)
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
       UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
-      return
+    } else {
+      this.options.sortPriority = null
+      UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortPriority)
+
+      UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortOrder, this.options.sortOrder)
+      UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortName, this.options.sortName)
     }
     }
+  }
 
 
-    UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortOrder, this.options.sortOrder)
-    UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortName, this.options.sortName)
+  onMultipleSort (...args) {
+    super.onMultipleSort(...args)
+
+    if (this.options.sortPriority === undefined) {
+      UtilsCookie.deleteCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortPriority)
+    } else {
+      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)
+
+      UtilsCookie.setCookie(this, UtilsCookie.cookieIds.sortPriority, JSON.stringify(this.options.sortPriority))
+    }
   }
   }
 
 
   onPageNumber (...args) {
   onPageNumber (...args) {
@@ -461,6 +479,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
 
     const sortOrderCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
     const sortOrderCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortOrder)
     const sortOrderNameCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName)
     const sortOrderNameCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortName)
+    let sortPriorityCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.sortPriority)
     const pageNumberCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageNumber)
     const pageNumberCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageNumber)
     const pageListCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageList)
     const pageListCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.pageList)
     const searchTextCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.searchText)
     const searchTextCookie = UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.searchText)
@@ -479,10 +498,32 @@ $.BootstrapTable = class extends $.BootstrapTable {
       throw new Error('Could not parse the json of the columns cookie!', columnsCookieValue)
       throw new Error('Could not parse the json of the columns cookie!', columnsCookieValue)
     }
     }
 
 
+    try {
+      sortPriorityCookie = JSON.parse(sortPriorityCookie)
+    } catch (e) {
+      throw new Error('Could not parse the json of the sortPriority cookie!', sortPriorityCookie)
+    }
+
     // sortOrder
     // sortOrder
-    this.options.sortOrder = sortOrderCookie ? sortOrderCookie : this.options.sortOrder
+    this.options.sortOrder = undefined
     // sortName
     // sortName
-    this.options.sortName = sortOrderNameCookie ? sortOrderNameCookie : this.options.sortName
+    this.options.sortName = undefined
+
+    if (!sortPriorityCookie) {
+      // sortOrder
+      this.options.sortOrder = sortOrderCookie ? sortOrderCookie : this.options.sortOrder
+      // sortName
+      this.options.sortName = sortOrderNameCookie ? sortOrderNameCookie : this.options.sortName
+    }
+
+    // sortPriority
+    this.options.sortPriority = sortPriorityCookie ? sortPriorityCookie : this.options.sortPriority
+
+    if (this.options.sortOrder || this.options.sortName) {
+      // sortPriority
+      this.options.sortPriority = null
+    }
+
     // pageNumber
     // pageNumber
     this.options.pageNumber = pageNumberCookie ? +pageNumberCookie : this.options.pageNumber
     this.options.pageNumber = pageNumberCookie ? +pageNumberCookie : this.options.pageNumber
     // pageSize
     // pageSize