admin管理员组文章数量:1334923
I want to implement context menus like this:
But I can only achieve this so far: =/src/DataGridTest.tsx
The contextmenu
event fired after clicking on a cell (or row).
But I want to immediately fire the contextmenu
event.
What should I do?
I want to implement context menus like this: https://fancygrid./samples/miscellaneous/context-menu
But I can only achieve this so far: https://codesandbox.io/s/xenodochial-snow-pz1fr?file=/src/DataGridTest.tsx
The contextmenu
event fired after clicking on a cell (or row).
But I want to immediately fire the contextmenu
event.
What should I do?
Share Improve this question edited Jul 23, 2021 at 6:30 ehiller 1,56721 silver badges35 bronze badges asked Jul 22, 2021 at 5:30 mochi0708mochi0708 231 silver badge3 bronze badges2 Answers
Reset to default 5In addition to Chris Wong's answer, you can also do the following:
<DataGridPremium
rows={rows}
slotProps={{
row: {
onContextMenu: (e) => handleRowContextMenu(e),
style: { cursor: 'context-menu' },
},
}}
{...props}
/>
Then, you can get the row:
const handleRowContextMenu = (event: React.MouseEvent<HTMLDivElement>) => {
event.preventDefault();
if (!event.currentTarget) {
return;
}
const rowId = Number((event.currentTarget as HTMLDivElement).getAttribute('data-id'));
const record = rows.find((row) => row.id === rowId);
if (!record) {
return;
}
// open a context menu
setContextMenu(
contextMenu === null ? { mouseX: event.clientX, mouseY: event.clientY, record } : null
);
};
Use onContextMenu
on the parent:
const onContextMenu = (e: React.SyntheticEvent): void => {
let target = e.target as HTMLElement;
console.log(target.innerHTML);
};
...
<div style={{ height: 300, width: "100%" }} onContextMenu={onContextMenu}>
...
There is a demo.
本文标签: javascriptHow to implement context menus on MatrialUI DataGridStack Overflow
版权声明:本文标题:javascript - How to implement context menus on Matrial-UI DataGrid? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375533a2463084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论