@@ -2,16 +2,18 @@ import { customElement } from 'lit/decorators.js'
22import { LitElement , html } from 'lit'
33import { repeat } from 'lit/directives/repeat.js'
44import {
5+ ColumnDef ,
6+ RowSorting ,
57 TableController ,
6- createColumnHelper ,
7- createCoreRowModel ,
88 flexRender ,
9+ tableFeatures ,
910} from '@tanstack/lit-table'
1011import install from '@twind/with-web-components'
1112import config from '../twind.config'
1213
1314const withTwind = install ( config )
1415
16+ // 1. Define what the shape of your data will be for each row
1517type Person = {
1618 firstName : string
1719 lastName : string
@@ -21,36 +23,44 @@ type Person = {
2123 progress : number
2224}
2325
24- const columnHelper = createColumnHelper < any , Person > ( )
26+ // 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features
27+ const _features = tableFeatures ( {
28+ RowSorting : RowSorting ,
29+ } )
2530
26- const columns = [
27- columnHelper . accessor ( 'firstName' , {
31+ // const columnHelper = createColumnHelper<typeof _features, Person>()
32+
33+ // 4. Define the columns for your table. This uses the new `ColumnDef` type to define columns. Alternatively, check out the createTableHelper/createColumnHelper util for an even more type-safe way to define columns.
34+ const columns : Array < ColumnDef < typeof _features , Person > > = [
35+ {
36+ accessorKey : 'firstName' , // accessorKey method (most common for simple use-cases)
37+ header : 'First Name' ,
2838 cell : ( info ) => info . getValue ( ) ,
29- footer : ( info ) => info . column . id ,
30- } ) ,
31- columnHelper . accessor ( ( row ) => row . lastName , {
39+ } ,
40+ {
41+ accessorFn : ( row ) => row . lastName , // accessorFn used (alternative) along with a custom id
3242 id : 'lastName' ,
33- cell : ( info ) => html `< i > ${ info . getValue ( ) } </ i > ` ,
3443 header : ( ) => html `< span > Last Name</ span > ` ,
35- footer : ( info ) => info . column . id ,
36- } ) ,
37- columnHelper . accessor ( 'age' , {
44+ cell : ( info ) => html `< i > ${ info . getValue < string > ( ) } </ i > ` ,
45+ } ,
46+ {
47+ accessorFn : ( row ) => Number ( row . age ) , // accessorFn used to transform the data
48+ id : 'age' ,
3849 header : ( ) => 'Age' ,
3950 cell : ( info ) => info . renderValue ( ) ,
40- footer : ( info ) => info . column . id ,
41- } ) ,
42- columnHelper . accessor ( 'visits' , {
51+ } ,
52+ {
53+ accessorKey : 'visits' ,
4354 header : ( ) => html `< span > Visits</ span > ` ,
44- footer : ( info ) => info . column . id ,
45- } ) ,
46- columnHelper . accessor ( 'status' , {
55+ } ,
56+ {
57+ accessorKey : 'status' ,
4758 header : 'Status' ,
48- footer : ( info ) => info . column . id ,
49- } ) ,
50- columnHelper . accessor ( 'progress' , {
59+ } ,
60+ {
61+ accessorKey : 'progress' ,
5162 header : 'Profile Progress' ,
52- footer : ( info ) => info . column . id ,
53- } ) ,
63+ } ,
5464]
5565
5666const data : Array < Person > = [
@@ -91,15 +101,20 @@ const data: Array<Person> = [
91101@customElement ( 'lit-table-example' )
92102@withTwind
93103class LitTableExample extends LitElement {
94- private tableController = new TableController < any , Person > ( this )
104+ private tableController = new TableController < typeof _features , Person > ( this )
95105
96106 protected render ( ) : unknown {
97107 const table = this . tableController . table ( {
108+ _features, // new required option in V9. Tell the table which features you are importing and using (better tree-shaking)
109+ _rowModels : { } , // `Core` row model is now included by default, but you can still override it here
98110 columns,
99111 data,
100- getCoreRowModel : createCoreRowModel ( ) ,
112+ // add additional table options here
113+ debugTable : true ,
101114 } )
102115
116+ console . log ( table )
117+
103118 return html `
104119 < table >
105120 < thead >
@@ -129,7 +144,7 @@ class LitTableExample extends LitElement {
129144 ( row ) => html `
130145 < tr >
131146 ${ repeat (
132- row . getVisibleCells ( ) ,
147+ row . getAllCells ( ) ,
133148 ( cell ) => cell . id ,
134149 ( cell ) =>
135150 html ` < td >
0 commit comments