Skip to content

Commit

Permalink
Update table.js
Browse files Browse the repository at this point in the history
Fixing bug. Code was clearing all content from the element the table was assigned to. It will now only clear content that it creates.
  • Loading branch information
scottgruenewald authored Nov 3, 2023
1 parent ff6bfd2 commit 9494b81
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions table.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

class GoogleSheetTable {
constructor(url, mainSelector = 'main', title = '', noDataMessage = '') {
this.url = url;
Expand All @@ -7,6 +8,7 @@ constructor(url, mainSelector = 'main', title = '', noDataMessage = '') {
this.noDataMessage = noDataMessage
this.injectStyles();

this.uniqueClass = 'google-sheet-table-generated-content'; // A unique class to identify script-generated elements

this.uniqueId = Math.random().toString(36).substring(2); // Generate a random unique ID for each table

Expand Down Expand Up @@ -54,7 +56,11 @@ setupResizeListener() {

updateView(data, columns) {
// Clear the current content
this.mainElement.innerHTML = '';
const elementsToRemove = this.mainElement.querySelectorAll(`.${this.uniqueClass}`);
elementsToRemove.forEach(element => element.remove());




// Check for data length to decide if we need to display noDataMessage
if (!data || data.length === 0) {
Expand All @@ -64,6 +70,7 @@ updateView(data, columns) {
if (this.title) {
const titleElement = document.createElement('h2');
titleElement.innerText = this.title;
titleElement.classList.add(this.uniqueClass)
this.mainElement.appendChild(titleElement);
}

Expand Down Expand Up @@ -213,7 +220,11 @@ renderMobileSearchFields(columns) {

renderMobile(data, columns) {
const container = document.createElement('div');
container.className = 'mobile-table-container';
container.className = `mobile-table-container ${this.uniqueClass}`;




container.classList.add('mt-3')
const searchFields = this.renderMobileSearchFields(columns);
container.appendChild(searchFields);
Expand Down Expand Up @@ -251,11 +262,15 @@ renderTable(data) {
if (this.title) {
const titleElement = document.createElement('h2');
titleElement.innerText = this.title;
titleElement.classList.add(this.uniqueClass)
this.mainElement.appendChild(titleElement);
}

this.table = document.createElement('table');
this.table.className = 'table table-striped table-bordered'; // Bootstrap classes for styling
this.table.className = `table table-striped table-bordered ${this.uniqueClass}`;



const columns = Object.keys(data[0]);
const thead = this.createTableHeader(columns);
const tbody = this.createTableBody(data, columns);
Expand Down

0 comments on commit 9494b81

Please sign in to comment.