1
1
import {
2
2
For ,
3
3
Show ,
4
+ createEffect ,
4
5
createMemo ,
5
6
createSignal ,
6
7
splitProps ,
@@ -14,9 +15,11 @@ import {
14
15
} from "@tanstack/solid-table" ;
15
16
import type {
16
17
ColumnDef ,
17
- Table as TableType ,
18
- PaginationState ,
19
18
OnChangeFn ,
19
+ PaginationState ,
20
+ Row ,
21
+ RowSelectionState ,
22
+ Table as TableType ,
20
23
} from "@tanstack/solid-table" ;
21
24
22
25
import { Button } from "@/components/ui/button" ;
@@ -60,13 +63,18 @@ type Props<TData, TValue> = {
60
63
pagination ?: PaginationState ;
61
64
onPaginationChange ?: OnChangeFn < PaginationState > ;
62
65
63
- onRowSelection ?: ( idx : number , row : TData , value : boolean ) => void ;
66
+ onRowSelection ?: ( rows : Row < TData > [ ] , value : boolean ) => void ;
64
67
onRowClick ?: ( idx : number , row : TData ) => void ;
65
68
} ;
66
69
67
70
export function DataTable < TData , TValue > ( props : Props < TData , TValue > ) {
68
71
const [ local ] = splitProps ( props , [ "columns" , "data" ] ) ;
69
- const [ rowSelection , setRowSelection ] = createSignal ( { } ) ;
72
+ const [ rowSelection , setRowSelection ] = createSignal < RowSelectionState > ( { } ) ;
73
+ createEffect ( ( ) => {
74
+ // NOTE: because we use our own state for row selection, reset it when data changes.
75
+ local . data ( ) ;
76
+ setRowSelection ( { } ) ;
77
+ } ) ;
70
78
71
79
const paginationEnabled = ( ) => props . onPaginationChange !== undefined ;
72
80
const [ paginationState , setPaginationState ] =
@@ -82,7 +90,7 @@ export function DataTable<TData, TValue>(props: Props<TData, TValue>) {
82
90
} ;
83
91
} ) ;
84
92
85
- const columns = createMemo ( ( ) => {
93
+ const columns = ( ) => {
86
94
const onRowSelection = props . onRowSelection ;
87
95
if ( ! onRowSelection ) {
88
96
return local . columns ( ) ;
@@ -93,18 +101,28 @@ export function DataTable<TData, TValue>(props: Props<TData, TValue>) {
93
101
id : "select" ,
94
102
header : ( ctx ) => (
95
103
< Checkbox
96
- indeterminate = { ctx . table . getIsSomePageRowsSelected ( ) }
97
104
checked = { ctx . table . getIsAllPageRowsSelected ( ) }
98
- onChange = { ( value ) => ctx . table . toggleAllPageRowsSelected ( ! ! value ) }
105
+ onChange = { ( value : boolean ) => {
106
+ console . debug (
107
+ "Select all" ,
108
+ value ,
109
+ ctx . table . getIsSomeRowsSelected ( ) ,
110
+ ) ;
111
+ ctx . table . toggleAllPageRowsSelected ( value ) ;
112
+
113
+ const allRows = ctx . table . getRowModel ( ) ;
114
+ onRowSelection ( allRows . rows , value ) ;
115
+ } }
99
116
aria-label = "Select all"
100
117
/>
101
118
) ,
102
119
cell : ( ctx ) => (
103
120
< Checkbox
104
121
checked = { ctx . row . getIsSelected ( ) }
105
- onChange = { ( value ) => {
106
- onRowSelection ( ctx . row . index , ctx . row . original , value ) ;
122
+ onChange = { ( value : boolean ) => {
107
123
ctx . row . toggleSelected ( value ) ;
124
+
125
+ onRowSelection ( [ ctx . row ] , value ) ;
108
126
} }
109
127
aria-label = "Select row"
110
128
onClick = { ( event : Event ) => {
@@ -118,13 +136,13 @@ export function DataTable<TData, TValue>(props: Props<TData, TValue>) {
118
136
} as ColumnDef < TData , TValue > ,
119
137
...local . columns ( ) ,
120
138
] ;
121
- } ) ;
139
+ } ;
122
140
123
- const table = createMemo ( ( ) =>
124
- createSolidTable ( {
125
- get data ( ) {
126
- return local . data ( ) || [ ] ;
127
- } ,
141
+ const table = createMemo ( ( ) => {
142
+ console . debug ( "table data rebuild" ) ;
143
+
144
+ return createSolidTable ( {
145
+ data : local . data ( ) || [ ] ,
128
146
state : {
129
147
pagination : paginationState ( ) ,
130
148
rowSelection : rowSelection ( ) ,
@@ -136,7 +154,7 @@ export function DataTable<TData, TValue>(props: Props<TData, TValue>) {
136
154
// enableColumnResizing: true,
137
155
// columnResizeMode: 'onChange',
138
156
139
- // pagination {
157
+ // pagination:
140
158
manualPagination : paginationEnabled ( ) ,
141
159
onPaginationChange : ( state ) => {
142
160
setPaginationState ( state ) ;
@@ -146,7 +164,6 @@ export function DataTable<TData, TValue>(props: Props<TData, TValue>) {
146
164
}
147
165
} ,
148
166
rowCount : props . rowCount ,
149
- // } pagination
150
167
151
168
// Just means, the input data is already filtered.
152
169
manualFiltering : true ,
@@ -157,10 +174,11 @@ export function DataTable<TData, TValue>(props: Props<TData, TValue>) {
157
174
// NOTE: In our current setup this causes infinite reload cycles when paginating.
158
175
autoResetPageIndex : false ,
159
176
177
+ enableRowSelection : true ,
160
178
enableMultiRowSelection : props . onRowSelection ? true : false ,
161
179
onRowSelectionChange : setRowSelection ,
162
- } ) ,
163
- ) ;
180
+ } ) ;
181
+ } ) ;
164
182
165
183
return (
166
184
< >
0 commit comments