reactjstailwind-csssolid-jstanstack-table

Tanstack Table: Clickable row except for 2 columns


A React solution will work, even though I'm using Solid because the implementation is virtually the same in this case. I'm also using Tailwind, if that matters to anyone (it shouldn't since it's functional, rather than stylistic).

I am trying to make my rows clickable with 2 exceptions: the first column (a star icon which is already clickable for favoriting the row entity) and the last column (a list of buttons used for modifying/replicating the row entity).

I feel the solution lies in filtering on the row in some way, but what I've tried has failed to produce what I want. Either it makes the entire row clickable (in which case, any clickable item does what it should, then does what the row click event does), or (when adding the onClick to the column specifically) it's only clickable on the text of the column value.

Here is one of my column specific implementations:

{ cell: ({row}) => <span onClick={() => openAction('View', row.original)}>{row.original.name}</span>, accessorKey: 'name', minSize: 200, header: 'Name', enableMultiSort: true },

Again, this only works if your mouse is on the text of the column.

And here is my table row implementation:

                        <tbody>
                            <For each={table.getRowModel().rows}>
                                {(row, i) => (
                                    <tr key={row.id} class={`${i() % 2 === 1 && 'bg-gray-600'} border-gray-700 hover:bg-primary hover:border-darkprimary`} onClick={() => clickEvent('View', row.original)}>
                                        <For each={row.getVisibleCells()}>
                                            {(cell) => <td key={cell.id} class={`p-1`}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</td>}
                                        </For>
                                    </tr>
                                )}
                            </For>
                        </tbody>

Again, this propagates multiple effects when clicking on a clickable column object.

I feel like this is closer to the right answer, if I could figure out how to filter properly. I don't want "Faves" or "Actions" to propagate the click event.


Solution

  • I figured it out. I needed to trade the span tags for div tags. In this way, it doesn't just wrap the text of the field value with the onClick action, it wraps the whole cell area.

    For reference, here is my fixed column example:

    { cell: ({row}) => <div onClick={() => openAction('View', row.original)} title={row.original.desc}>{row.original.name}</div>, accessorKey: 'name', minSize: 200, header: 'Name', enableMultiSort: true }
    

    This has the added benefit of giving the description on mouse-hover and being sortable by the name field in addition to any other fields that have already been selected for sorting.