这就是我渲染表格主体的方式:
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<Row {...row.getRowProps()}>
{row.cells.map((cell) => {
// return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;
return cell.render("Cell");
})}
</Row>
);
})}
</tbody>
Run Code Online (Sandbox Code Playgroud)
这就是我设置列的方式。我为每个单元格创建了独特的组件。
[
{
Header: "Main Header",
Footer: "Foot",
columns: [
{
Header: "Code",
accessor: "NominalCode",
Cell: (props) => {
return <CodeCell>{props.cell.value}</CodeCell>;
},
Footer: () => {
return <FooterTotalCell>Total</FooterTotalCell>;
}
},
{
Header: "Description",
accessor: "Description",
Cell: (props) => {
return (
<DescriptionCell country={props.row.values.Currency}>
{String(props.cell.value)}
</DescriptionCell>
);
},
Footer: () => {
return <td />;
}
}
]
Run Code Online (Sandbox Code Playgroud)
我想将一个函数作为道具从我的主 App.jsx 文件传递给DescriptionCell组件。此函数将用于在 内部执行一些 onClick 功能DescriptionCell。
我怎样才能做到这一点?
谢谢。
小智 18
您还可以通过将 props 对象直接传递给渲染函数Cell来Cell逐个执行此操作:
...
{rows.cells.map(cell => cell.render('Cell', { test: 'this is a test'}))}
...
Run Code Online (Sandbox Code Playgroud)
然后在您的列定义中
columns: [
...
{
Cell: (props) => {
console.log(props.test) // the value is 'this is a test'
return (
<CustomComponent test={props.test} />
);
},
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以通过使用column传递给Cell组件的 prop 来完成此操作:
columns: [
...
{
Header: "Description",
accessor: "Description",
onClick: () => { alert('click!'); },
Cell: (props) => {
return (
<DescriptionCell onClick={props.column.onClick} country={props.row.values.Currency}>
{String(props.cell.value)}
</DescriptionCell>
);
},
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您的函数可能被许多其他单元使用(例如更新后端),您可以将该函数传递给主要Table组件,如下例所示: https: //github.com/tannerlinsley/react-table/blob/master/示例/厨房水槽控制/src/App.js#L622
| 归档时间: |
|
| 查看次数: |
9081 次 |
| 最近记录: |