如何在材料表外部点击按钮时调用材料表的增行功能
发布于 2021-12-04 05:09:07
导入import { forwardRef } from "react";
调用const addActionRef = React.useRef();
在你想要你的按钮的地方包含这段代码。我在这里使用material ui按钮
<Button
color="primary"
variant="contained"
onClick={() => addActionRef.current.click()} //The line you need
>
Add new item
</Button>并覆盖材料表中的操作
components = {
{
Action: props => {
//If isn't the add action
if (typeof props.action === typeof Function || props.action.tooltip !== 'Add') {
return <MTableAction { ...props
}
/>
} else {
return <div ref = {
addActionRef
}
onClick = {
props.action.onClick
}
/>;
}
}
}
}
如果你有任何问题,请提出来。我在这里使用了react js和material ui。
https://github.com/mbrn/material-table/issues/2133
此链接可能对您有帮助...这就是我得到答案的地方
https://stackoverflow.com/questions/67206860
复制相似问题