Tanstack Table 和 React JS:使用 React JS 和 Tanstack Table 时如何根据状态显示/隐藏按钮
P粉042455250
P粉042455250 2023-09-02 23:04:17
0
2
634
<p>我有react js并实现了tanstack表(react-table)。我需要帮助来根据每行的状态使按钮/链接显示或隐藏。 假设如果有行包含状态“批准”按钮将显示,否则它将隐藏。</p> <pre class="brush:php;toolbar:false;">......some initiliaze data const [sorting, setSorting] = useState<SortingState>([]); const [rowSelection, setRowSelection] = useState({}); const [isApproved, setIsApprove] = useState(false); ......calling column const columns = useMemo<ColumnDef<IEvent>[]>( ......list of column { 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>我希望可以显示/隐藏按钮(“议程”)依赖于每行的状态</p>
P粉042455250
P粉042455250

全部回复(2)
P粉555682718

您是否使用过三元运算符,条件 (三元)运算符

P粉006847750

您可以使用 cell.row.original.status 访问每行的 status 值,并根据该值显示按钮,而不是将其存储在状态中变量。

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>
      ),
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!