1
1
import { LitElement , html , css } from 'lit' ;
2
2
import { EventType } from '../core/EventType' ;
3
3
import { TableHeadElement } from './TableHeadElement' ;
4
+ import { TableColumnElement } from './TableColumnElement' ;
4
5
5
6
/**
6
- * @class TableBodyElement
7
+ * @class TableElement
7
8
*
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.
9
11
*
10
12
* @example
11
- * <js-tablebody data="#data-source-id"></js-tablebody >
13
+ * <js-table data="#data-source-id"><!-- .... -->< /js-table >
12
14
*/
13
- export class TableBodyElement extends LitElement {
15
+ export class TableElement extends LitElement {
14
16
#data = null ;
15
17
16
18
#head = null ;
17
19
18
20
static get localName ( ) {
19
- return 'js-tablebody ' ;
21
+ return 'js-table ' ;
20
22
}
21
23
22
24
static get properties ( ) {
@@ -48,8 +50,7 @@ export class TableBodyElement extends LitElement {
48
50
th {
49
51
text-transform: capitalize;
50
52
}
51
- .wrap {
52
- max-height: 40px;
53
+ .cell {
53
54
overflow: hidden;
54
55
}
55
56
code, pre {
@@ -92,6 +93,21 @@ export class TableBodyElement extends LitElement {
92
93
firstUpdated ( ) {
93
94
// Set the table header
94
95
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
+ }
95
111
}
96
112
97
113
render ( ) {
@@ -112,25 +128,35 @@ export class TableBodyElement extends LitElement {
112
128
}
113
129
114
130
#renderColumns( row ) {
115
- const columns = [ ] ;
116
-
131
+ const cells = [ ] ;
117
132
if ( row instanceof Object ) {
118
133
Object . keys ( row ) . forEach ( ( key ) => {
119
134
if ( this . columns . indexOf ( key ) === - 1 ) {
120
135
this . columns . push ( key ) ;
121
136
}
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 > ` ;
123
138
} ) ;
124
139
} else {
125
140
this . columns . push ( 'value' ) ;
126
- columns . push ( html `< td > ${ this . #renderCell( row ) } </ td > ` ) ;
141
+ cells . push ( html `< td > ${ this . #renderCell( row ) } </ td > ` ) ;
127
142
}
128
143
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 ;
130
153
}
131
154
132
155
// eslint-disable-next-line class-methods-use-this
133
156
#renderCell( cell ) {
157
+ if ( cell === null || cell === undefined || cell === '' ) {
158
+ return html `nil` ;
159
+ }
134
160
if ( cell instanceof Object ) {
135
161
return html `< code > ${ JSON . stringify ( cell ) } </ code > ` ;
136
162
}
0 commit comments