Skip to content

Commit 6af8866

Browse files
committed
Uodated table
1 parent 731a957 commit 6af8866

File tree

3 files changed

+80
-22
lines changed

3 files changed

+80
-22
lines changed

src/index.html

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@
4747
</wc-nav>
4848
</wc-sidebar>
4949
<wc-content>
50-
<!-- Table of data -->
51-
<wc-table id="table">Table of Fruit</wc-table>
52-
<script>
53-
document.getElementById('table').data = [
50+
<div class="container">
51+
<h1>Tables</h1>
52+
53+
<!-- Table of data -->
54+
<wc-table id="table">Table of Fruit</wc-table>
55+
<script>
56+
document.getElementById('table').data = [
5457
{ fruit: 'apple', color: 'green', weight: '100gr' },
5558
{ fruit: 'banana', color: 'yellow', weight: '140gr' }
5659
];
57-
</script>
60+
</script>
61+
</div>
62+
<hr>
63+
5864
<div class="container">
5965
<h1>Buttons</h1>
6066

src/table/TableColumn.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class TableColumn {
2+
constructor(name) {
3+
this.name = name;
4+
}
5+
}

src/table/TableElement.js

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import {
2-
LitElement, html,
2+
LitElement, html, css, nothing,
33
} from 'lit';
44

5+
import { TableColumn } from './TableColumn';
6+
57
/**
68
* TableElement - A class that can be used to display a table
79
*
810
* @example
9-
* <wc-table>
10-
* <tr>
11-
* <th>Column A</th>
12-
* <th>Column B</th>
13-
* </tr>
14-
* <tr>
15-
* <td>Row 1, Column A</td>
16-
* <td>Row 1, Column B</td>
17-
* </tr>
18-
* </wc-table>
11+
* <wc-table data="...">Table Caption</wc-table>
1912
*/
2013
export class TableElement extends LitElement {
2114
static get localName() {
@@ -24,28 +17,82 @@ export class TableElement extends LitElement {
2417

2518
static get properties() {
2619
return {
20+
/**
21+
* @property {Array} data
22+
* @memberof TableElement
23+
*
24+
* The array of data loaded into the table. Each element may be an array or object.
25+
*/
2726
data: { type: Array },
2827
};
2928
}
3029

31-
get tableHeader() {
30+
static get styles() {
31+
return css`
32+
table {
33+
width: 100%;
34+
border-collapse: collapse;
35+
border: 1px solid gray;
36+
}
37+
table th {
38+
background-color: lightgray;
39+
border: 1px solid gray;
40+
text-align: left;
41+
}
42+
table td,th {
43+
padding: 2px;
44+
}
45+
`;
46+
}
47+
48+
// eslint-disable-next-line class-methods-use-this
49+
get tableColumns() {
50+
return [
51+
new TableColumn('fruit'),
52+
new TableColumn('color'),
53+
new TableColumn('weight'),
54+
];
55+
}
56+
57+
renderTableHeader() {
3258
return html`
3359
<tr>
34-
<th>A</th><th>B</th>
60+
${this.tableColumns.map((column) => html`<th>${column.name}</th>`)}
3561
</tr>
3662
`;
3763
}
3864

39-
// eslint-disable-next-line class-methods-use-this
65+
renderTableCell(column, i, j) {
66+
const row = this.data[i];
67+
if (Array.isArray(row) && row.length <= j) {
68+
return row[j];
69+
}
70+
if (typeof row === 'object' && column.name in row) {
71+
return row[column.name];
72+
}
73+
return nothing;
74+
}
75+
76+
renderTableRow(i) {
77+
return this.tableColumns.map((column, j) => html`<td>${this.renderTableCell(column, i, j)}</td>`);
78+
}
79+
80+
renderTableRows() {
81+
return html`
82+
${this.data.map((_, i) => html`<tr>${this.renderTableRow(i)}</tr>`)}
83+
`;
84+
}
85+
4086
render() {
41-
// eslint-disable-next-line no-console
42-
console.log('TableElement render', this.data);
87+
const tableHeader = this.renderTableHeader();
88+
const tableRows = this.renderTableRows();
4389
return html`
4490
<table>
4591
<thead>
46-
${this.tableHeader}
92+
${tableHeader}
4793
</thead>
4894
<tbody>
95+
${tableRows}
4996
</tbody>
5097
</table>
5198
<caption><slot></slot></caption>

0 commit comments

Comments
 (0)