Tanstack Table and React JS: How to show/hide buttons based on state when using React JS and Tanstack Table
P粉042455250
2023-09-02 23:04:17
<p>I have react js and implemented tanstack table (react-table). I need help making buttons/links show or hide based on the state of each row.
Suppose if there is a row containing status "Approve" button will be shown otherwise it will be hidden. </p>
<pre class="brush:php;toolbar:false;">......some initialize data
const [sorting, setSorting] = useState<SortingState>([]);
const [rowSelection, setRowSelection] = useState({});
const [isApproved, setIsApprove] = useState(false);
...calling column
const columns = useMemo<ColumnDef<IEvent>[]>(
...list of columns
{
accessorKey: "status",
header: () => <span>Status</span>,
cell: (cell: any) => {
let status = cell.getValue();
return cell.getValue();
},
enableSorting: true,
enableColumnFilter: true,
},
{
accessorKey: "actions",
header: () => <span>Action</span>,
cell: (cell: any) => {
console.log(isApproved);
return (
<div className="inline-flex gap-x-2">
<Link to={`./${cell.row.id}/edit`}>Edit</Link>
<EVSDeleteButton id={cell.row.id} header="event" />
<Link to={`./${cell.row.id}/agenda`}>Agenda</Link>
</div>
);
},
enableSorting: false,
enableColumnFilter: false,
},</pre>
<p>I want to be able to show/hide the button ("Agenda") depending on the state of each row</p>
Have you ever used the ternary operator, Conditional (ternary) operator
You can use
cell.row.original.status
to access thestatus
value of each row and display the button based on that value instead of storing it in a status variable.