Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(column-reordering): fixed issue with reordered columns and cookies #7468

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ class BootstrapTable {
}

const tds = this.header.fields.map((field, j) => {
const column = this.columns[j]
const column = this.options.columns[0][j]
const value_ = Utils.getItemField(item, field, this.options.escape, column.escape)
let value = ''
const attrs = {
Expand Down
29 changes: 29 additions & 0 deletions src/extensions/cookie/bootstrap-table-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,40 @@ $.BootstrapTable = class extends $.BootstrapTable {

initHeader (...args) {
if (this.options.reorderableColumns && this.options.cookie) {
if (this.columnsSortOrder) {
this.options.columns[0] = this.reorderColumnsFromCookies(this.columnsSortOrder)
}
this.columnsSortOrder = JSON.parse(UtilsCookie.getCookie(this, UtilsCookie.cookieIds.reorderColumns))
}
super.initHeader(...args)
}

reorderColumnsFromCookies (reorderedColumns) {
const reorderedList = []
const notInReorderedList = []
const reorderedKeys = Object.keys(reorderedColumns)

this.options.columns[0].forEach(column => {
if (reorderedKeys.includes(column.field)) {
const fieldIndex = reorderedColumns[column.field] - 1

reorderedList[fieldIndex] = column
} else {
notInReorderedList.push(column)
}
})

let finalList = [...reorderedList, ...notInReorderedList]

finalList = finalList.map((item, index) => {
item.fieldIndex = index
item.colspanIndex = index
return item
})

return finalList
}

persistReorderColumnsState (that) {
UtilsCookie.setCookie(that, UtilsCookie.cookieIds.reorderColumns, JSON.stringify(that.columnsSortOrder))
}
Expand Down