Tanstack Table and React JS: How to show/hide buttons based on state when using React JS and Tanstack Table
P粉042455250
P粉042455250 2023-09-02 23:04:17
0
2
691
<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>
P粉042455250
P粉042455250

reply all(2)
P粉555682718

Have you ever used the ternary operator, Conditional (ternary) operator

P粉006847750

You can use cell.row.original.status to access the status value of each row and display the button based on that value instead of storing it in a status variable.

cell: (cell: any) => (
        <div className="inline-flex gap-x-2">
          <Link to={`./${cell.row.original.id}/edit`}>Edit</Link>
          {cell.row.original.status === "Approved" && (
            <Link to={`./${cell.row.original.id}/agenda`}>Agenda</Link>
          )}
        </div>
      ),
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template