@@ -13,10 +13,18 @@ import { TableColumnElement } from './TableColumnElement';
13
13
* <js-table data="#data-source-id"><!-- .... --></js-table>
14
14
*/
15
15
export class TableElement extends LitElement {
16
+ // Data source node
16
17
#data = null ;
17
18
19
+ // Table header node
18
20
#head = null ;
19
21
22
+ // Table column renderers
23
+ #renderer = { } ;
24
+
25
+ // Default renderer
26
+ #default;
27
+
20
28
static get localName ( ) {
21
29
return 'js-table' ;
22
30
}
@@ -98,13 +106,19 @@ export class TableElement extends LitElement {
98
106
const elements = this . childNodes ;
99
107
for ( let i = 0 ; i < elements . length ; i += 1 ) {
100
108
if ( elements [ i ] instanceof TableColumnElement ) {
109
+ // Column name and title
101
110
const name = elements [ i ] . getAttribute ( 'name' ) ;
111
+ // If the name is not empty, add it to the column list
102
112
if ( name && name !== '' ) {
103
113
// Append the column to the list
104
114
if ( this . columns . indexOf ( name ) === - 1 ) {
105
115
this . columns . push ( elements [ i ] . getAttribute ( 'name' ) ) ;
106
116
}
107
- // TODO: Set this column as the renderer
117
+ // Set column renderer
118
+ this . #renderer[ name ] = elements [ i ] ;
119
+ } else {
120
+ // Set the default renderer
121
+ this . #default = elements [ i ] ;
108
122
}
109
123
}
110
124
}
@@ -127,14 +141,28 @@ export class TableElement extends LitElement {
127
141
return rows ;
128
142
}
129
143
144
+ #rendererFor( key ) {
145
+ const renderer = this . #renderer[ key ] ;
146
+ if ( renderer ) {
147
+ return renderer ;
148
+ }
149
+ return this . #default;
150
+ }
151
+
152
+ #hidden( key ) {
153
+ return this . #rendererFor( key ) . hidden ;
154
+ }
155
+
130
156
#renderColumns( row ) {
131
157
const cells = [ ] ;
132
158
if ( row instanceof Object ) {
133
159
Object . keys ( row ) . forEach ( ( key ) => {
134
- if ( this . columns . indexOf ( key ) === - 1 ) {
135
- this . columns . push ( key ) ;
160
+ if ( ! this . #hidden( key ) ) {
161
+ if ( this . columns . indexOf ( key ) === - 1 ) {
162
+ this . columns . push ( key ) ;
163
+ }
164
+ cells [ this . columns . indexOf ( key ) ] = html `< td > < div class ="cell "> ${ this . #renderCell( row , key ) } </ div > </ td > ` ;
136
165
}
137
- cells [ this . columns . indexOf ( key ) ] = html `< td > < div class ="cell "> ${ this . #renderCell( row [ key ] ) } </ div > </ td > ` ;
138
166
} ) ;
139
167
} else {
140
168
this . columns . push ( 'value' ) ;
@@ -152,14 +180,7 @@ export class TableElement extends LitElement {
152
180
return cells ;
153
181
}
154
182
155
- // eslint-disable-next-line class-methods-use-this
156
- #renderCell( cell ) {
157
- if ( cell === null || cell === undefined || cell === '' ) {
158
- return html `nil` ;
159
- }
160
- if ( cell instanceof Object ) {
161
- return html `< code > ${ JSON . stringify ( cell ) } </ code > ` ;
162
- }
163
- return html `${ cell } ` ;
183
+ #renderCell( value , key ) {
184
+ return this . #rendererFor( key ) . render ( value , key ) ;
164
185
}
165
186
}
0 commit comments