search

Home  >  Q&A  >  body text

Tanstack Table and React JS: How to show/hide buttons based on state when using React JS and Tanstack Table

<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粉042455250P粉042455250527 days ago757

reply all(2)I'll reply

  • P粉555682718

    P粉5556827182023-09-03 20:38:40

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

    reply
    0
  • P粉006847750

    P粉0068477502023-09-03 19:44:13

    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>
          ),

    reply
    0
  • Cancelreply