1- import { html } from "lit" ;
1+ import { html , } from "lit" ;
2+ import type { TemplateResult } from '@spectrum-web-components/base' ;
23import { customElement , property , state } from "lit/decorators.js" ;
34import { ref , Ref , createRef } from 'lit/directives/ref.js' ;
45
@@ -10,8 +11,8 @@ import * as d3 from 'd3';
1011
1112import "@spectrum-web-components/table/elements.js" ;
1213import {
13- Table
14-
14+ Table ,
15+ TableCell
1516} from "@spectrum-web-components/table" ;
1617import { PropertyValues } from "lit/development" ;
1718
@@ -118,24 +119,21 @@ export class MayorTable extends Table {
118119 let table = d3 . select ( t ) . selectAll ( 'sp-table' ) ;
119120 if ( ( table === undefined ) || ( table . empty ( ) ) ) {
120121 console . log ( 'div has no sp-table' )
121- t . append ( ( d3 . create ( 'sp-table' ) . node ( ) as Table ) ) ;
122- table = d3 . select ( t ) . selectAll ( 'sp-table' ) ;
122+ return ;
123123 }
124124
125- //first remove old entries
126- table . selectAll ( 'sp-table-head' ) . remove ( ) ;
127- table . selectAll ( 'sp-table-body' ) . remove ( ) ;
128-
129- const thead = table . append ( 'sp-table-head' )
130- const tbody = table . append ( 'sp-table-body' )
131-
125+ for ( let i = 0 ; i < data . length ; i ++ ) {
126+ for ( let j = 0 ; j < columns . length ; j ++ ) {
127+ let row :dsv . DSVRowString < string > = data [ i ] ;
128+ let c :string = columns [ j ] ;
129+ console . log ( `c is ${ c } ` ) ;
130+ let nR :Record < string , dsv . DSVRowString < string > > = { "row" : row }
131+ console . log ( `pushing item ${ j } row is ${ JSON . stringify ( nR ) } ` ) ;
132+ super . items . push ( nR ) ;
133+ ( table . node ( ) as Table ) . items . push ( nR ) ;
134+ }
135+ }
132136
133- table . attr ( "scroller" , true ) ;
134- table . style ( "height" , "400px" )
135- . style ( 'overflow-x' , "auto" )
136- . style ( "max-width" , "80vw" )
137- . style ( 'font-size' , 'var(--spectrum-font-size-75)' )
138- table . classed ( "mayor-table" , true ) ;
139137 table . on ( 'sorted' , ( event ) => {
140138 console . log ( 'called sort event' ) ;
141139 const { sortDirection, sortKey } = event . detail ;
@@ -148,46 +146,30 @@ export class MayorTable extends Table {
148146 this . requestUpdate ( ) ;
149147 } )
150148
151- thead . classed ( "mayor-row" , true )
152- thead . style ( 'overflow-x' , 'auto' )
153- . style ( "max-width" , "80vw" )
154-
155- tbody . style ( 'overflow-x' , 'auto' )
149+ table . attr ( "scroller" , true ) ;
150+ table . style ( "height" , "400px" )
151+ . style ( 'overflow-x' , "auto" )
156152 . style ( "max-width" , "80vw" )
153+ . style ( 'font-size' , 'var(--spectrum-font-size-75)' )
154+ table . classed ( "mayor-table" , true ) ;
157155
158- thead . selectAll ( 'sp-table-head-cell' )
159- . data ( columns )
160- . enter ( )
161- . append ( 'sp-table-head-cell' )
162- . text ( function ( d ) {
163- return d
164- } )
165- . attr ( "sortable" , true )
166- . attr ( "sort-key" , function ( d ) {
167- return d . replace ( / \W / g, '' ) ;
168- } ) ;
169-
170- const rows = tbody . selectAll ( 'sp-table-row' )
171- . data ( data )
172- . enter ( )
173- . append ( 'sp-table-row' ) ;
174-
175- rows . selectAll ( 'sp-table-cell' )
176- . data ( function ( row ) {
177- return columns . map ( function ( column ) {
178- return { column : column , value : row [ column ] }
179- } )
180- } )
181- . classed ( "mayor-row" , true )
182- . enter ( )
183- . append ( 'sp-table-cell' )
184- . text ( function ( d ) {
185- if ( d . value === undefined ) {
186- return "" ;
156+ ( table . node ( ) as Table ) . renderItem = ( item :Record < string , unknown > , index ) => {
157+ let a :dsv . DSVRowString < string > = ( item . row as dsv . DSVRowString < string > ) ;
158+ let tc :TableCell [ ] = [ ] ;
159+ for ( let i = 0 ; i < this . _ids . length ; i ++ ) {
160+ const cell = document . createElement ( 'sp-table-cell' ) ;
161+ console . log ( `adding text content ${ a [ this . _ids [ i ] ] } ` ) ;
162+ let content :string = '' ;
163+ if ( ! a [ this . _ids [ i ] ] ) {
164+ content = '' ;
187165 } else {
188- return d . value ;
166+ content = a [ this . _ids [ i ] ] ! ;
189167 }
190- } )
168+ cell . textContent = content ;
169+ tc . push ( cell ) ;
170+ }
171+ return html `${ tc } ` ;
172+ }
191173
192174 console . log ( 'ready to return table from initTable' ) ;
193175 this . tableRendered = true ;
@@ -197,18 +179,22 @@ export class MayorTable extends Table {
197179
198180 render ( ) {
199181
200- if ( this . tableRendered ) {
201- console . log ( 'render thinks the table rendered' ) ;
202182 return html `
203183 < div id ='${ this . _id } ' ${ ref ( this . tableRef ) } >
204-
184+ < sp-table >
185+ < sp-table-head >
186+ ${ this . _ids . map ( ( id ) =>
187+ html `
188+ < sp-table-head-cell sortable sort-direction ="desc " sort-key ="${ id . replace ( / / g, "" ) } ">
189+ ${ id }
190+ </ sp-table-head-cell >
191+ `
192+ ) }
193+ </ sp-table-head >
194+ </ sp-table >
205195 </ div >
206196 ` ;
207- } else {
208- return html `
209- < div ${ ref ( this . tableRef ) } > Loading....</ div >
210- `
211- }
197+
212198 }
213199}
214200
0 commit comments