【发布时间】:2021-07-14 22:52:47
【问题描述】:
我在 NextJS 项目中使用react-table,我想知道如何在同一页面上拥有多个表。一直在挖掘,但没有发现任何关于它的信息。
我已经在页面上有一个实例,我不确定我是否想多了,但我想我可以创建一个 tableInstance2 但是,我将如何告诉第二个表 JSX 使用它第二批数据?
const tableInstance = useTable({
columns: COLUMNS,
data: tableData
},
useSortBy);
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow
} = tableInstance;
这是我当前的表格标记:
{tableData.length > 0 &&
<div className={styles.tableSpace}>
<table {...getTableProps()} className={styles.table}>
<thead className={styles.thead}>
{headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()} className={styles.tr}>
{headerGroup.headers.map((column) => (
<th {...column.getHeaderProps(column.getSortByToggleProps())} className={styles.th}>
{column.render('Header')}
{column.Header !== 'Description' &&
<span>
{column.isSorted ? (column.isSortedDesc ? <FontAwesomeIcon className={styles.faCaretDown} icon={faCaretDown} /> : <FontAwesomeIcon className={styles.faCaretUp} icon={faCaretUp} />) : <FontAwesomeIcon className={styles.faArrowsAltV} icon={faArrowsAltV} />}
</span>
}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps} className={styles.tbody}>
{rows.map(row => {
prepareRow(row)
return (
<tr {...row.getRowProps()} className={styles.tr}>
{row.cells.map((cell) => {
return (
<td {...cell.getCellProps()} className={styles.td}>
{cell.render('Cell')}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
</div>
}
在同一个页面(在同一个文件中)是否有可能超过 1 个? 将不胜感激任何提示。非常感谢!
【问题讨论】:
-
没有理由不能在一个组件中拥有两个 react-table 表。但是,如果这两个表具有相似的属性,为什么不创建自己的 Table 组件来处理
useTable钩子,然后让您的父组件呈现两个表? -
@j-petty 感谢您的评论。它们都有不同的列、不同的数据和一点不同的 JSX 标记。我可能不得不将它们分开,但希望我可以将它们放在同一个文件中。这真的归结为我如何为他们提供各自的 tableInstance。
标签: reactjs react-table react-table-v7