【发布时间】:2023-03-17 12:28:01
【问题描述】:
我有一个用于表格的通用组件,该组件通过应用程序在不同页面中使用。现在,行的选择被保存在表格组件本身中。我想在按下按钮时从其父组件访问选定的数据行
【问题讨论】:
-
“每当按下按钮”,你指的是什么按钮?
-
@rzwnahmd 我在表格组件外有一个按钮,我的示例中有它
标签: reactjs react-table
我有一个用于表格的通用组件,该组件通过应用程序在不同页面中使用。现在,行的选择被保存在表格组件本身中。我想在按下按钮时从其父组件访问选定的数据行
【问题讨论】:
标签: reactjs react-table
@jiltender
i fix my problem by doing this to get row data outside the table
rowProps={row =>(console.log(row)
)}
add this on parent components where u call the Table
<Table
sortByProps={[]}
columns={columns}
rowProps={row =>(setSelectedItems(row))} /// pass row props
/>
and the table side
add this in
function props
` `rowProps = () => ({}) })
function Table({ columns, data, showSpinnerProp, hiddenColumnsProps,getCellProps, getRowProps, height, sortByProps, source,
setSelectedItems,rowProps = () => ({}) }) {
this will return u all the select data you can call it before return
useEffect(() => {
setSelectedItems = selectedFlatRows;
rowProps(selectedFlatRows)
}, [selectedFlatRows]);
}
【讨论】:
您可以做的是在父组件中为 selectedRows 维护一些状态,然后将回调传递给 Table 组件,每次选择或取消选择一行时都会调用该组件。这里我对你的沙箱做了一些改动,希望对你有帮助:)
【讨论】:
selectedRows 处于您存储所有选定状态的状态。
selectedRows 中。
在组件外部创建一个状态变量,类似于
const [selectedRows, setSelectedRows] = useState([]);
然后通过 props 将其传递给组件。然后在按钮上你可以有一些这样的代码
onPress = { () => props.setSelectedRows(selectedRows) }
【讨论】: