Skip to content

Commit

Permalink
Merge pull request #7606 from wenzhixin/fix/cookie-columns
Browse files Browse the repository at this point in the history
Fixed cookie columns display error after adding column
  • Loading branch information
wenzhixin authored Nov 13, 2024
2 parents 1a957f7 + 70f417e commit f1e2b28
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/extensions/cookie/bootstrap-table-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const UtilsCookie = {
pageNumber: 'bs.table.pageNumber',
pageList: 'bs.table.pageList',
hiddenColumns: 'bs.table.hiddenColumns',
columns: 'bs.table.columns',
cardView: 'bs.table.cardView',
customView: 'bs.table.customView',
searchText: 'bs.table.searchText',
Expand All @@ -28,6 +29,9 @@ const UtilsCookie = {
return navigator.cookieEnabled
},
isCookieEnabled (that, cookieName) {
if (cookieName === 'bs.table.columns') {
return that.options.cookiesEnabled.includes('bs.table.hiddenColumns')
}
return that.options.cookiesEnabled.includes(cookieName)
},
setCookie (that, cookieName, cookieValue) {
Expand Down Expand Up @@ -242,27 +246,25 @@ $.BootstrapTable = class extends $.BootstrapTable {
this.options.cookiesEnabled

if (this.options.filterControl) {
const that = this

this.$el.on('column-search.bs.table', (e, field, text) => {
let isNewField = true

for (let i = 0; i < that._filterControls.length; i++) {
if (that._filterControls[i].field === field) {
that._filterControls[i].text = text
for (let i = 0; i < this._filterControls.length; i++) {
if (this._filterControls[i].field === field) {
this._filterControls[i].text = text
isNewField = false
break
}
}
if (isNewField) {
that._filterControls.push({
this._filterControls.push({
field,
text
})
}

UtilsCookie.setCookie(that, UtilsCookie.cookieIds.filterControl, JSON.stringify(that._filterControls))
}).on('created-controls.bs.table', UtilsCookie.initCookieFilters(that))
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.filterControl, JSON.stringify(this._filterControls))
}).on('created-controls.bs.table', UtilsCookie.initCookieFilters(this))
}
}
super.init()
Expand Down Expand Up @@ -365,15 +367,21 @@ $.BootstrapTable = class extends $.BootstrapTable {
if (!this.options.cookie) {
return
}
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.hiddenColumns, JSON.stringify(this.getHiddenColumns().map(column => column.field)))
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.hiddenColumns,
JSON.stringify(this.getHiddenColumns().map(column => column.field)))
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns,
JSON.stringify(this.columns.map(column => column.field)))
}

_toggleAllColumns (...args) {
super._toggleAllColumns(...args)
if (!this.options.cookie) {
return
}
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.hiddenColumns, JSON.stringify(this.getHiddenColumns().map(column => column.field)))
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.hiddenColumns,
JSON.stringify(this.getHiddenColumns().map(column => column.field)))
UtilsCookie.setCookie(this, UtilsCookie.cookieIds.columns,
JSON.stringify(this.columns.map(column => column.field)))
}

toggleView () {
Expand Down Expand Up @@ -444,13 +452,16 @@ $.BootstrapTable = class extends $.BootstrapTable {
const cardViewCookie = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.cardView)
const customViewCookie = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.customView)
const hiddenColumnsCookieValue = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.hiddenColumns)
const columnsCookieValue = UtilsCookie.getCookie(this, UtilsCookie.cookieIds.columns)

let hiddenColumnsCookie = {}
let columnsCookie = {}

try {
hiddenColumnsCookie = JSON.parse(hiddenColumnsCookieValue)
columnsCookie = JSON.parse(columnsCookieValue)
} catch (e) {
throw new Error('Could not parse the json of the hidden columns cookie!', hiddenColumnsCookieValue)
throw new Error('Could not parse the json of the columns cookie!')
}

try {
Expand Down Expand Up @@ -493,8 +504,13 @@ $.BootstrapTable = class extends $.BootstrapTable {
this.customViewDefaultView = customViewCookie === 'true'

if (hiddenColumnsCookie) {
columnsCookie = columnsCookie || this.columns.map(column => column.field)

for (const column of this.columns) {
if (!column.switchable) {
if (
!column.switchable ||
!columnsCookie.includes(column.field)
) {
continue
}

Expand All @@ -505,12 +521,11 @@ $.BootstrapTable = class extends $.BootstrapTable {
}

getCookies () {
const bootstrapTable = this
const cookies = {}

for (const [key, value] of Object.entries(UtilsCookie.cookieIds)) {
cookies[key] = UtilsCookie.getCookie(bootstrapTable, value)
if (key === 'columns' || key === 'hiddenColumns' || key === 'sortPriority') {
cookies[key] = UtilsCookie.getCookie(this, value)
if (['columns', 'hiddenColumns', 'sortPriority'].includes(key)) {
cookies[key] = JSON.parse(cookies[key])
}
}
Expand All @@ -526,19 +541,17 @@ $.BootstrapTable = class extends $.BootstrapTable {
}

configureStorage () {
const that = this

this._storage = {}
switch (this.options.cookieStorage) {
case 'cookieStorage':
this._storage.setItem = function (cookieName, cookieValue) {
document.cookie = [
cookieName, '=', encodeURIComponent(cookieValue),
`; expires=${UtilsCookie.calculateExpiration(that.options.cookieExpire)}`,
that.options.cookiePath ? `; path=${that.options.cookiePath}` : '',
that.options.cookieDomain ? `; domain=${that.options.cookieDomain}` : '',
that.options.cookieSecure ? '; secure' : '',
`;SameSite=${that.options.cookieSameSite}`
`; expires=${UtilsCookie.calculateExpiration(this.options.cookieExpire)}`,
this.options.cookiePath ? `; path=${this.options.cookiePath}` : '',
this.options.cookieDomain ? `; domain=${this.options.cookieDomain}` : '',
this.options.cookieSecure ? '; secure' : '',
`;SameSite=${this.options.cookieSameSite}`
].join('')
}
this._storage.getItem = function (cookieName) {
Expand All @@ -551,9 +564,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
document.cookie = [
encodeURIComponent(cookieName), '=',
'; expires=Thu, 01 Jan 1970 00:00:00 GMT',
that.options.cookiePath ? `; path=${that.options.cookiePath}` : '',
that.options.cookieDomain ? `; domain=${that.options.cookieDomain}` : '',
`;SameSite=${that.options.cookieSameSite}`
this.options.cookiePath ? `; path=${this.options.cookiePath}` : '',
this.options.cookieDomain ? `; domain=${this.options.cookieDomain}` : '',
`;SameSite=${this.options.cookieSameSite}`
].join('')
}
break
Expand Down Expand Up @@ -589,13 +602,13 @@ $.BootstrapTable = class extends $.BootstrapTable {
}

this._storage.setItem = function (cookieName, cookieValue) {
Utils.calculateObjectValue(that.options, that.options.cookieCustomStorageSet, [cookieName, cookieValue], '')
Utils.calculateObjectValue(this.options, this.options.cookieCustomStorageSet, [cookieName, cookieValue], '')
}
this._storage.getItem = function (cookieName) {
return Utils.calculateObjectValue(that.options, that.options.cookieCustomStorageGet, [cookieName], '')
return Utils.calculateObjectValue(this.options, this.options.cookieCustomStorageGet, [cookieName], '')
}
this._storage.removeItem = function (cookieName) {
Utils.calculateObjectValue(that.options, that.options.cookieCustomStorageDelete, [cookieName], '')
Utils.calculateObjectValue(this.options, this.options.cookieCustomStorageDelete, [cookieName], '')
}

break
Expand Down

0 comments on commit f1e2b28

Please sign in to comment.