Skip to content

Commit

Permalink
Added Error message if no table data
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoremus committed May 28, 2024
1 parent 82e779e commit beef84a
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions components/wc/TableWC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type TableDataItem = {
email: string;
};

class MYTable extends HTMLElement {
class MyTable extends HTMLElement {
css = `
table {
border: 4px solid black;
Expand All @@ -14,6 +14,10 @@ class MYTable extends HTMLElement {
border: 3px solid black;
text-align: center;
}
div {
font-size:2rem;
font-weight:900;
}
`;
constructor() {
super();
Expand All @@ -23,19 +27,28 @@ class MYTable extends HTMLElement {
const json = this.getAttribute("data-feed") ?? "[]";
const data: [TableDataItem] = JSON.parse(json);
const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.innerHTML = `
<style>${this.css}</style>
<table>
<thead><th>ID</th><th>Name</th><th>Email</th></thead>
<tbody>
${
let html = `<style>${this.css}</style>`;
if (data.length > 0) {
html = html + this.displayData(data);
} else {
html = html + `<div>There was a problem fetching the user data</div>`;
}
shadowRoot.innerHTML = html;
}

displayData(data: [TableDataItem]) {
return `
<table>
<thead><th>ID</th><th>Name</th><th>Email</th></thead>
<tbody>
${
data.map((item) =>
`<tr><td>${item.id}</td><td>${item.name}</td><td>${item.email}</td></tr>`
)
}
</tbody>
</table>
</tbody>
</table>
`;
}
}
customElements.define("data-table", MYTable);
customElements.define("data-table", MyTable);

0 comments on commit beef84a

Please sign in to comment.