Skip to content

Commit

Permalink
cleaner table destruction, relates to #375
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Apr 10, 2024
1 parent 1d9967d commit 01ef0d3
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/datatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class DataTable {

headerDOM: HTMLDivElement

_initialInnerHTML: string
_initialHTML: string

initialized: boolean

Expand Down Expand Up @@ -102,7 +102,6 @@ export class DataTable {
const dom = typeof table === "string" ?
document.querySelector(table) :
table

if (dom instanceof HTMLTableElement) {
this.dom = dom
} else {
Expand Down Expand Up @@ -134,7 +133,7 @@ export class DataTable {
classes
}

this._initialInnerHTML = this.options.destroyable ? this.dom.innerHTML : "" // preserve in case of later destruction
this._initialHTML = this.options.destroyable ? dom.outerHTML : "" // preserve in case of later destruction

if (this.options.tabIndex) {
this.dom.tabIndex = this.options.tabIndex
Expand Down Expand Up @@ -677,19 +676,19 @@ export class DataTable {
if (!this.options.destroyable) {
return
}
this.dom.innerHTML = this._initialInnerHTML

// Remove the className
this.options.classes.table?.split(" ").forEach(className => this.wrapperDOM.classList.remove(className))

// Remove the containers
if (this.wrapperDOM.parentElement) {
this.wrapperDOM.parentElement.replaceChild(this.dom, this.wrapperDOM)
// Restore the initial HTML
const oldDOM = createElement("div")
oldDOM.innerHTML = this._initialHTML
this.wrapperDOM.parentElement.replaceChild(oldDOM.firstElementChild, this.wrapperDOM)
} else {
// Remove the className
this.options.classes.table?.split(" ").forEach(className => this.wrapperDOM.classList.remove(className))
}

this.initialized = false

window.removeEventListener("resize", this._listeners.onResize)

this.initialized = false
}

/**
Expand Down

0 comments on commit 01ef0d3

Please sign in to comment.