I'm struggling to get the checkbox
working using TanStack
in Solidjs
. The docs doesn't have much information about creating a checkbox column which is why i followed the TanStack React Table Examples
and some of the Solidjs Table Examples
. The checkbox would not update and i am assuming this has something to do with Solidjs
reactivity. Here props is not updating since i tried to console.log it and it only ran once which was when the component rendered.
const checkbox = {
accessorKey: "checkbox",
header: (props) => (
<Checkbox
{...{
checked: props.table.getIsAllRowsSelected(),
indeterminate: props.table.getIsSomeRowsSelected(),
onChange: props.table.getToggleAllRowsSelectedHandler(),
}}
/>
),
cell: (props) => {
return (
<Checkbox
{...{
checked: props.row.getIsSelected(),
disabled: !props.row.getCanSelect(),
onChange: props.row.getToggleSelectedHandler(),
}}
/>
);
},
};
If i do it without TanStack, it would look something like this but it defeats the purpose of using TanStack
cell: () => {
const [isSelected , setIsSelected] = createSignal(false)
const toggleSelected = (event , checked) => {
setIsSelected(checked)
}
return (
<Checkbox
{...{
checked: isSelected(),
onChange: toggleSelected,
}}
/>
);
},
*** Update 1: Here is the whole code of creating the table
const generateColumns = (columns) => {
const result = columns.map((value) => {
return {
accessorKey: value,
};
});
return result;
};
const DataTable = (props) => {
const [rowSelection, setRowSelection] = createSignal({});
const checkbox = {
accessorKey: "checkbox",
header: (props) => (
<Checkbox
{...{
checked: props.table.getIsAllRowsSelected,
indeterminate: props.table.getIsSomeRowsSelected,
onChange: props.table.getToggleAllRowsSelectedHandler,
}}
/>
),
cell: () => {
const [isSelected , setIsSelected] = createSignal(false)
const toggleSelected = (event , checked) => {
setIsSelected(checked)
}
return (
<Checkbox
{...{
checked: isSelected(),
onChange: toggleSelected,
}}
/>
);
},
};
const columns = [checkbox, ...generateColumns(props.columns)];
const table = createSolidTable({
get data() {
return props.data;
},
columns: columns,
state: {
get rowSelection() {
return rowSelection();
},
},
onRowSelectionChange: setRowSelection,
getCoreRowModel: getCoreRowModel(),
});
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<For each={table.getHeaderGroups()}>
{(headerGroup) => (
<TableRow>
<For each={headerGroup.headers}>
{(header) => (
<TableCell>
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
</TableCell>
)}
</For>
</TableRow>
)}
</For>
</TableHead>
<TableBody>
<For each={table.getRowModel().rows}>
{(row) => (
<TableRow>
<For each={row.getVisibleCells()}>
{(cell) => <TableCell>{flexRender(cell.column.columnDef.cell, cell.getContext())}</TableCell>}
</For>
</TableRow>
)}
</For>
</TableBody>
</Table>
</TableContainer>
);
};
I was in a similar spot, coming from the React version of Tanstack Table.
Having looked at your code I was able to make mine work with some minor modifications based on your implementation.
Hopefully this can help you finalize a working solution as well.
Here's the code for my SolidJS table component with working select column:
Excerpt usage:
The
selectColumn
column helper variable (whereCheckbox
component is a custom component in this case. You might have to accommodate any properties or parameters for you checkbox component):