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 {
16
+ // Data source node
14
17
#data = null ;
15
18
19
+ // Table header node
16
20
#head = null ;
17
21
22
+ // Table column renderers
23
+ #renderer = { } ;
24
+
25
+ // Default renderer
26
+ #default;
27
+
18
28
static get localName ( ) {
19
- return 'js-tablebody ' ;
29
+ return 'js-table ' ;
20
30
}
21
31
22
32
static get properties ( ) {
@@ -48,8 +58,7 @@ export class TableBodyElement extends LitElement {
48
58
th {
49
59
text-transform: capitalize;
50
60
}
51
- .wrap {
52
- max-height: 40px;
61
+ .cell {
53
62
overflow: hidden;
54
63
}
55
64
code, pre {
@@ -92,6 +101,27 @@ export class TableBodyElement extends LitElement {
92
101
firstUpdated ( ) {
93
102
// Set the table header
94
103
this . #head = this . querySelector ( TableHeadElement . localName ) ;
104
+
105
+ // Get the table columns
106
+ const elements = this . childNodes ;
107
+ for ( let i = 0 ; i < elements . length ; i += 1 ) {
108
+ if ( elements [ i ] instanceof TableColumnElement ) {
109
+ // Column name and title
110
+ const name = elements [ i ] . getAttribute ( 'name' ) ;
111
+ // If the name is not empty, add it to the column list
112
+ if ( name && name !== '' ) {
113
+ // Append the column to the list
114
+ if ( this . columns . indexOf ( name ) === - 1 ) {
115
+ this . columns . push ( elements [ i ] . getAttribute ( 'name' ) ) ;
116
+ }
117
+ // Set column renderer
118
+ this . #renderer[ name ] = elements [ i ] ;
119
+ } else {
120
+ // Set the default renderer
121
+ this . #default = elements [ i ] ;
122
+ }
123
+ }
124
+ }
95
125
}
96
126
97
127
render ( ) {
@@ -111,29 +141,46 @@ export class TableBodyElement extends LitElement {
111
141
return rows ;
112
142
}
113
143
114
- #renderColumns( row ) {
115
- const columns = [ ] ;
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
+ }
116
155
156
+ #renderColumns( row ) {
157
+ const cells = [ ] ;
117
158
if ( row instanceof Object ) {
118
159
Object . keys ( row ) . forEach ( ( key ) => {
119
- if ( this . columns . indexOf ( key ) === - 1 ) {
120
- 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 > ` ;
121
165
}
122
- columns . push ( html `< td > < div class ="wrap "> ${ this . #renderCell( row [ key ] ) } </ div > </ td > ` ) ;
123
166
} ) ;
124
167
} else {
125
168
this . columns . push ( 'value' ) ;
126
- columns . push ( html `< td > ${ this . #renderCell( row ) } </ td > ` ) ;
169
+ cells . push ( html `< td > ${ this . #renderCell( row ) } </ td > ` ) ;
127
170
}
128
171
129
- return columns ;
172
+ // Any missing columns we fill
173
+ for ( let i = 0 ; i < this . columns . length ; i += 1 ) {
174
+ if ( ! cells [ i ] ) {
175
+ cells [ i ] = html `< td > </ td > ` ;
176
+ }
177
+ }
178
+
179
+ // Return cells for rendering in a row
180
+ return cells ;
130
181
}
131
182
132
- // eslint-disable-next-line class-methods-use-this
133
- #renderCell( cell ) {
134
- if ( cell instanceof Object ) {
135
- return html `< code > ${ JSON . stringify ( cell ) } </ code > ` ;
136
- }
137
- return html `${ cell } ` ;
183
+ #renderCell( value , key ) {
184
+ return this . #rendererFor( key ) . render ( value , key ) ;
138
185
}
139
186
}
0 commit comments