【问题标题】:Material-UI get all rows from DataGridMaterial-UI 从 DataGrid 获取所有行
【发布时间】:2021-03-20 21:03:06
【问题描述】:

有谁知道如何从 Material-UI DataGrid 获取所有行数据?例如,我更改了 DataGrid 行中的一些值,并希望在进行更改后获取我的所有行。示例:

import { Button, TextField } from "@material-ui/core";
import { DataGrid } from "@material-ui/data-grid";
import moment from "moment";

const columns = [
  {
    field: "Col1",
    headerName: "Col1",
    flex: 1.0,
    disableClickEventBubbling: true,
    sortable: false,
    disableColumnMenu: true
  },
  {
    field: "Col2",
    headerName: "Col2",
    flex: 1.0,
    disableClickEventBubbling: true,
    sortable: false,
    disableColumnMenu: true,
    renderCell: (params) => (
      <>
        <TextField
          type="date"
          defaultValue={moment(Date.parse(params.row.Date)).format(
            "YYYY-MM-DD"
          )}
          InputLabelProps={{
            shrink: true
          }}
        />
      </>
    )
  },
  {
    field: "Col3",
    headerName: "Col3",
    flex: 1.0,
    disableClickEventBubbling: true,
    sortable: false,
    disableColumnMenu: true,
    renderCell: (params) => <TextField />
  }
];

const rows = [
  { id: 1, Col1: "col1 data", Col2: null, Col3: null },
  { id: 2, Col1: "col2 data", Col2: null, Col3: null },
  { id: 3, Col1: "col3 data", Col2: null, Col3: null }
];

export default function App() {
  const handleClickButton = () => {
    console.log(rows); //I want to see grid changed data
  };

  return (
    <div className="App">
      <DataGrid
        density="compact"
        rows={rows}
        columns={columns}
        pageSize={25}
        autoHeight={true}
        hideFooter={true}
      />
      <Button variant="contained" color="primary" onClick={handleClickButton}>
        Show me grid data
      </Button>
    </div>
  );
}

【问题讨论】:

  • 请详细说明。向我们展示一些代码以及您的确切含义。
  • 我已经添加了示例。

标签: reactjs material-ui


【解决方案1】:

Material-UI 的DataGrid 内部存储行数据,并有its own API 修改行数据。这是onChange 触发时更新行数据的方式:

{
  field: "Col3",
  headerName: "Col3",
  renderCell: (params) => (
    <TextField
      onChange={(e) =>
        params.api.updateRows([{ ...params.row, Col3: e.target.value }])
      }
    />
  )
}

在版本 5 中,您只需将列标记为editable。 (见cell editing)

{
  field: "Col3",
  headerName: "Col3",
  editable: true
}

要读取DataGrid的当前行状态,可以使用apiRef.current.getRowModels()。但是apiRef 来自Material-UI X Pro。如果您仍想访问它,则必须使用以下 hack:

function useApiRef() {
  const apiRef = useRef(null);
  const _columns = useMemo(
    () =>
      columns.concat({
        field: "__HIDDEN__",
        width: 0,
        renderCell: (params) => {
          apiRef.current = params.api;
          return null;
        }
      }),
    [columns]
  );

  return { apiRef, columns: _columns };
}

用法

const { apiRef, columns } = useApiRef();
const handleClickButton = () => {
  console.log(apiRef.current.getRowModels());
};

return (
  <>
    <DataGrid rows={rows} columns={columns} />
    <Button onClick={handleClickButton}>
      Show data
    </Button>
  </>
);

现场演示

【讨论】:

  • 很好的解决方案,谢谢!你知道有没有像 apiRef.current.getRowModels() 这样的方法来获取 selectionModel 吗?
  • @RuslanErmahgerd 试试apiRef.current.getSelectedRows()
  • 那么除了使用付费版本或破解它之外,没有办法将编辑后的数据从数据网格中取出?
  • @QiongWu 如果你想在提交之前获得edited data,还有其他方法可以做到这一点。有关详细信息,请参阅this 答案。
  • 谢谢!并在其承诺之后?那我怎么得到呢?
【解决方案2】:

1.将组件TextField转换为受控组件

<TextField
    onChange={(e) =>
      updateRows(e.target.value, params.row.id, params.field)
    }
  />

2.更改时更新行

const updateRows = (value, id, field) => {
  const item = rows.find((item) => item.id === id);
  item[field] = value;
  console.log(rows);
};
  1. 从行中获取所需的一切

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-19
    • 2021-08-13
    • 2021-07-22
    • 2022-01-23
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多