如何在使用 Data Grid Mui 时选择或取消选择表中的行

huangapple go评论109阅读模式
英文:

How to select or deselect a row in a Table using Data Grid Mui

问题

以下是您要翻译的代码部分:

  1. // 如何在@mui/x-data-grid中使用onclick选择和取消选择行,而不使用checkboxSelection属性,
  2. // 我正在尝试在Mui中实现行的选择或取消选择
  3. // 以下是表格组件文件
  4. import * as React from "react";
  5. import { v1 } from "uuid";
  6. import {
  7. DataGrid,
  8. GridToolbarContainer,
  9. GridToolbarExport
  10. } from "@mui/x-data-grid";
  11. // CSS
  12. import css from "./index.module.css";
  13. export default function Table(props) {
  14. const { tableHeader, data } = props;
  15. data.forEach((element) => {
  16. element["id"] = v1();
  17. });
  18. function CustomToolbar() {
  19. return (
  20. <GridToolbarContainer>
  21. <GridToolbarExport className={`${css.btn}`} />
  22. </GridToolbarContainer>
  23. );
  24. }
  25. return (
  26. <div style={{ height: 350, width: "100%" }}>
  27. <DataGrid
  28. className={`${css.root}`}
  29. rows={data}
  30. columns={tableHeader}
  31. pageSize={10}
  32. rowHeight={30}
  33. autoPageSize={true}
  34. disableColumnMenu={true}
  35. components={{
  36. Toolbar: CustomToolbar,
  37. }}
  38. />
  39. </div>
  40. );
  41. }

请注意,我只翻译了代码部分,没有包括您要翻译的问题。

英文:

How to select and deselect a row using onclick in @mui/x-data-grid with out the checkboxSelection prop,
I am trying to implement selection or deselection of a row in Mui
Below is Table Component file

  1. import * as React from &quot;react&quot;;
  2. import { v1 } from &quot;uuid&quot;;
  3. import {
  4. DataGrid,
  5. GridToolbarContainer,
  6. GridToolbarExport
  7. } from &quot;@mui/x-data-grid&quot;;
  8. //css
  9. import css from &quot;./index.module.css&quot;;
  10. export default function Table(props) {
  11. const { tableHeader, data } = props;
  12. data.forEach((element) =&gt; {
  13. element[&quot;id&quot;] = v1();
  14. });
  15. function CustomToolbar() {
  16. return (
  17. &lt;GridToolbarContainer&gt;
  18. &lt;GridToolbarExport className={`${css.btn}`} /&gt;
  19. &lt;/GridToolbarContainer&gt;
  20. );
  21. }
  22. return (
  23. &lt;div style={{ height: 350, width: &quot;100%&quot; }}&gt;
  24. &lt;DataGrid
  25. className={`${css.root}`}
  26. rows={data}
  27. columns={tableHeader}
  28. pageSize={10}
  29. rowHeight={30}
  30. autoPageSize={true}
  31. disableColumnMenu={true}
  32. components={{
  33. Toolbar: CustomToolbar,
  34. }}
  35. /&gt;
  36. &lt;/div&gt;
  37. );
  38. }

答案1

得分: 1

根据MUI文档,您需要通过@mui/x-data-grid-generator包中的useDemoData钩子来操作:

  1. import * as React from 'react';
  2. import { DataGrid } from '@mui/x-data-grid';
  3. import { useDemoData } from '@mui/x-data-grid-generator';
  4. export default function SingleRowSelectionGrid() {
  5. const { data } = useDemoData({
  6. dataSet: 'Commodity',
  7. rowLength: 10,
  8. maxColumns: 6,
  9. });
  10. return (
  11. <div style={{ height: 400, width: '100%' }}>
  12. <DataGrid {...data} />
  13. </div>
  14. );
  15. }

如果您想要取消选择一行,只需按住Ctrl键并单击它。

英文:

According to MUI docs, you will need to go through the useDemoData hook in the package @mui/x-data-grid-generator:

  1. import * as React from &#39;react&#39;;
  2. import { DataGrid } from &#39;@mui/x-data-grid&#39;;
  3. import { useDemoData } from &#39;@mui/x-data-grid-generator&#39;;
  4. export default function SingleRowSelectionGrid() {
  5. const { data } = useDemoData({
  6. dataSet: &#39;Commodity&#39;,
  7. rowLength: 10,
  8. maxColumns: 6,
  9. });
  10. return (
  11. &lt;div style={{ height: 400, width: &#39;100%&#39; }}&gt;
  12. &lt;DataGrid {...data} /&gt;
  13. &lt;/div&gt;
  14. );
  15. }

In case you would like to unselect a row, simply hold <kbd>Ctrl</kbd> and click on it.

huangapple
  • 本文由 发表于 2023年2月23日 20:56:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定