11import { LitElement , html , css } from 'lit' ;
22import { EventType } from '../core/EventType' ;
33import { TableHeadElement } from './TableHeadElement' ;
4+ import { TableColumnElement } from './TableColumnElement' ;
45
56/**
6- * @class TableBodyElement
7+ * @class TableElement
78 *
8- * This class provides a table body element.
9+ * This class provides a table element, in which the header, footer
10+ * and columns are rendered.
911 *
1012 * @example
11- * <js-tablebody data="#data-source-id"></js-tablebody >
13+ * <js-table data="#data-source-id"><!-- .... -->< /js-table >
1214 */
13- export class TableBodyElement extends LitElement {
15+ export class TableElement extends LitElement {
1416 #data = null ;
1517
1618 #head = null ;
1719
1820 static get localName ( ) {
19- return 'js-tablebody ' ;
21+ return 'js-table ' ;
2022 }
2123
2224 static get properties ( ) {
@@ -48,8 +50,7 @@ export class TableBodyElement extends LitElement {
4850 th {
4951 text-transform: capitalize;
5052 }
51- .wrap {
52- max-height: 40px;
53+ .cell {
5354 overflow: hidden;
5455 }
5556 code, pre {
@@ -92,6 +93,21 @@ export class TableBodyElement extends LitElement {
9293 firstUpdated ( ) {
9394 // Set the table header
9495 this . #head = this . querySelector ( TableHeadElement . localName ) ;
96+
97+ // Get the table columns
98+ const elements = this . childNodes ;
99+ for ( let i = 0 ; i < elements . length ; i += 1 ) {
100+ if ( elements [ i ] instanceof TableColumnElement ) {
101+ const name = elements [ i ] . getAttribute ( 'name' ) ;
102+ if ( name && name !== '' ) {
103+ // Append the column to the list
104+ if ( this . columns . indexOf ( name ) === - 1 ) {
105+ this . columns . push ( elements [ i ] . getAttribute ( 'name' ) ) ;
106+ }
107+ // TODO: Set this column as the renderer
108+ }
109+ }
110+ }
95111 }
96112
97113 render ( ) {
@@ -112,25 +128,35 @@ export class TableBodyElement extends LitElement {
112128 }
113129
114130 #renderColumns( row ) {
115- const columns = [ ] ;
116-
131+ const cells = [ ] ;
117132 if ( row instanceof Object ) {
118133 Object . keys ( row ) . forEach ( ( key ) => {
119134 if ( this . columns . indexOf ( key ) === - 1 ) {
120135 this . columns . push ( key ) ;
121136 }
122- columns . push ( html `< td > < div class ="wrap "> ${ this . #renderCell( row [ key ] ) } </ div > </ td > ` ) ;
137+ cells [ this . columns . indexOf ( key ) ] = html `< td > < div class ="cell "> ${ this . #renderCell( row [ key ] ) } </ div > </ td > ` ;
123138 } ) ;
124139 } else {
125140 this . columns . push ( 'value' ) ;
126- columns . push ( html `< td > ${ this . #renderCell( row ) } </ td > ` ) ;
141+ cells . push ( html `< td > ${ this . #renderCell( row ) } </ td > ` ) ;
127142 }
128143
129- return columns ;
144+ // Any missing columns we fill
145+ for ( let i = 0 ; i < this . columns . length ; i += 1 ) {
146+ if ( ! cells [ i ] ) {
147+ cells [ i ] = html `< td > </ td > ` ;
148+ }
149+ }
150+
151+ // Return cells for rendering in a row
152+ return cells ;
130153 }
131154
132155 // eslint-disable-next-line class-methods-use-this
133156 #renderCell( cell ) {
157+ if ( cell === null || cell === undefined || cell === '' ) {
158+ return html `nil` ;
159+ }
134160 if ( cell instanceof Object ) {
135161 return html `< code > ${ JSON . stringify ( cell ) } </ code > ` ;
136162 }
0 commit comments