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

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

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

问题

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

// 如何在@mui/x-data-grid中使用onclick选择和取消选择行,而不使用checkboxSelection属性,
// 我正在尝试在Mui中实现行的选择或取消选择
// 以下是表格组件文件

import * as React from "react";
import { v1 } from "uuid";
import {
  DataGrid,
  GridToolbarContainer,
  GridToolbarExport
} from "@mui/x-data-grid";

// CSS
import css from "./index.module.css";

export default function Table(props) {
  const { tableHeader, data } = props;

  data.forEach((element) => {
    element["id"] = v1();
  });

  function CustomToolbar() {
    return (
      <GridToolbarContainer>
        <GridToolbarExport className={`${css.btn}`} />
      </GridToolbarContainer>
    );
  }

  return (
    <div style={{ height: 350, width: "100%" }}>
      <DataGrid
        className={`${css.root}`}
        rows={data}
        columns={tableHeader}
        pageSize={10}
        rowHeight={30}
        autoPageSize={true}
        disableColumnMenu={true}
        components={{
          Toolbar: CustomToolbar,
        }}
      />
    </div>
  );
}

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

英文:

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

import * as React from &quot;react&quot;;
import { v1 } from &quot;uuid&quot;;
import {
  DataGrid,
  GridToolbarContainer,
  GridToolbarExport
} from &quot;@mui/x-data-grid&quot;;

//css
import css from &quot;./index.module.css&quot;;

export default function Table(props) {
  const { tableHeader, data } = props;


  data.forEach((element) =&gt; {
    element[&quot;id&quot;] = v1();
  });

  function CustomToolbar() {
    return (
      &lt;GridToolbarContainer&gt;
        &lt;GridToolbarExport className={`${css.btn}`} /&gt;
      &lt;/GridToolbarContainer&gt;
    );
  }

  return (
    &lt;div style={{ height: 350, width: &quot;100%&quot; }}&gt;
      &lt;DataGrid
        className={`${css.root}`}
        rows={data}
        columns={tableHeader}
        pageSize={10}
        rowHeight={30}
        autoPageSize={true}
        disableColumnMenu={true}
        components={{
          Toolbar: CustomToolbar,
        }}
      /&gt;
    &lt;/div&gt;
  );
}

答案1

得分: 1

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

import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

export default function SingleRowSelectionGrid() {
  const { data } = useDemoData({
    dataSet: 'Commodity',
    rowLength: 10,
    maxColumns: 6,
  });

  return (
    <div style={{ height: 400, width: '100%' }}>
      <DataGrid {...data} />
    </div>
  );
}

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

英文:

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

import * as React from &#39;react&#39;;
import { DataGrid } from &#39;@mui/x-data-grid&#39;;
import { useDemoData } from &#39;@mui/x-data-grid-generator&#39;;

export default function SingleRowSelectionGrid() {
  const { data } = useDemoData({
    dataSet: &#39;Commodity&#39;,
    rowLength: 10,
    maxColumns: 6,
  });

  return (
    &lt;div style={{ height: 400, width: &#39;100%&#39; }}&gt;
      &lt;DataGrid {...data} /&gt;
    &lt;/div&gt;
  );
}

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:

确定