如何将 MUI X Data Grid 单元格方向更改为行级而不是列级?

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

How to change MUI X Data Grid cell orientation to be row-level instead of column-level?

问题

我正在致力于为基于表格的用户界面实现MUI X数据网格组件。然而,我有一个特定的要求,我需要将表格单元格与行级关联,而不是传统的列级关联。

默认情况下,MUI X数据网格以基于列的方式运作,每个单元格属于特定列。然而,在我的情况下,我希望定制行为,使得每个单元格对应一个行。这意味着我想为每一行渲染自定义单元格组件,而这些组件应该能够了解相应的行数据。

我尝试过查阅官方文档并搜索示例,但我没有找到直接解决这个特定要求的内置选项或配置。

是否有人可以指导我如何在MUI X数据网格中实现这种行级单元格关联?是否有任何可用的属性、钩子或方法可以用于此目的?如果没有,那么实现这一点的最佳方法是什么?

如何将 MUI X Data Grid 单元格方向更改为行级而不是列级?

英文:

I am working on implementing the MUI X Data Grid component for my table-based UI. However, I have a specific requirement where I need the table cells to be associated with the row level, rather than the traditional column-level association.

By default, the MUI X Data Grid operates in a column-based manner, where each cell belongs to a specific column. However, in my case, I want to customize the behavior so that each cell corresponds to a row instead. This means that I want to render custom cell components for each row, and these components should be aware of the corresponding row data.

I've tried exploring the official documentation and searching for examples, but I haven't found any built-in options or configurations that directly address this specific requirement.

Can anyone guide me on how to achieve this row-level cell association with the MUI X Data Grid? Are there any available props, hooks, or methods that can be utilized for this purpose? If not, what would be the best approach to customize the table cell behavior to achieve this?

如何将 MUI X Data Grid 单元格方向更改为行级而不是列级?

答案1

得分: 1

通过自定义单元格渲染,您可以在 MUI X 数据表格中实现此行级单元格关联。这允许您定义一个自定义组件,用于呈现网格中的每个单元格。

为此,您需要向每一行传递一些额外的数据,如单元格组件的 typecolor。例如,您可以有不同类型的单元格组件,如 buttoninputselecttext。您还可以为每个单元格组件定义不同的颜色,如 primarysecondarydefault

然后,您需要创建一个自定义单元格组件,根据行数据的类型和颜色呈现不同的元素。例如,您可以使用 MUI 组件,如 ButtonTextFieldSelectTypography。您还可以使用 valuefield 等属性来访问单元格的值和字段名称。

最后,您需要将自定义单元格组件传递给网格中每列的 renderCell 属性。这将覆盖默认的单元格呈现,并使用您的自定义组件。

例如:

const CustomCell = ({ row: { type, color }, value, field, ...rest }) => {
  if (field === "id") {
    return <Typography justifyContent="center">{value}</Typography>;
  } else if (type === "button") {
    return (
      <Button fullWidth variant="contained" color={color}>
        {value}
      </Button>
    );
  } else if (type === "input") {
    return (
      <TextField fullWidth color={color} size="small" defaultValue={value} />
    );
  } else if (type === "select") {
    return (
      <Select fullWidth color={color} size="small" defaultValue={value}>
        <MenuItem value=""></MenuItem>
        <MenuItem value={0}>0</MenuItem>
        <MenuItem value={1}>1</MenuItem>
        <MenuItem value={2}>2</MenuItem>
      </Select>
    );
  } else {
    return <Typography color={color}>{value}</Typography>;
  }
};

const rows = [
  {
    id: 1,
    type: "button",
    color: "primary",
    firstName: "a",
    lastName: "a",
    gender: "a"
  },
  {
    id: 2,
    type: "input",
    color: "primary",
    firstName: "a",
    lastName: "a",
    gender: "a"
  }
];

return (
  <DataGrid
    rows={rows}
    columns={columns.map((column) => ({
      ...column,
      renderCell: (props) => <CustomCell {...props} />
    }))}
  />
);

您可以在此处查看完整示例:codesandbox.io

英文:

By customizing cell rendering, you can achieve this row-level cell association with the MUI X Data Grid. This allows you to define a custom component that will be rendered for each cell in the grid.

To do this, you need to pass some additional data to each row, such as the type and color of the cell component. For example, you can have different types of cell components, such as button, input, select, or text. You can also have different colors for each cell component, such as primary, secondary, or default.

Then, you need to create a custom cell component that will render different elements based on the type and color of the row data. For example, you can use MUI components such as Button, TextField, Select, or Typography. You can also use props such as value and field to access the cell value and field name.

Finally, you need to pass your custom cell component to the renderCell prop of each column in the grid. This will override the default rendering of the cells and use your custom component instead.

For example:

const CustomCell = ({ row: { type, color }, value, field, ...rest }) =&gt; {
if (field === &quot;id&quot;) {
return &lt;Typography justifyContent=&quot;center&quot;&gt;{value}&lt;/Typography&gt;;
} else if (type === &quot;button&quot;) {
return (
&lt;Button fullWidth variant=&quot;contained&quot; color={color}&gt;
{value}
&lt;/Button&gt;
);
} else if (type === &quot;input&quot;) {
return (
&lt;TextField fullWidth color={color} size=&quot;small&quot; defaultValue={value} /&gt;
);
} else if (type === &quot;select&quot;) {
return (
&lt;Select fullWidth color={color} size=&quot;small&quot; defaultValue={value}&gt;
&lt;MenuItem value=&quot;&quot;&gt;&lt;/MenuItem&gt;
&lt;MenuItem value={0}&gt;0&lt;/MenuItem&gt;
&lt;MenuItem value={1}&gt;1&lt;/MenuItem&gt;
&lt;MenuItem value={2}&gt;2&lt;/MenuItem&gt;
&lt;/Select&gt;
);
} else {
return &lt;Typography color={color}&gt;{value}&lt;/Typography&gt;;
}
};
const rows = [
{
id: 1,
type: &quot;button&quot;,
color: &quot;primary&quot;,
firstName: &quot;a&quot;,
lastName: &quot;a&quot;,
gender: &quot;a&quot;
},
{
id: 2,
type: &quot;input&quot;,
color: &quot;primary&quot;,
firstName: &quot;a&quot;,
lastName: &quot;a&quot;,
gender: &quot;a&quot;
}
]
return (
&lt;DataGrid
rows={rows}
columns={columns.map((column) =&gt; ({
...column,
renderCell: (props) =&gt; &lt;CustomCell {...props} /&gt;
}))}
/&gt;
);

You can see the whole example here: codesandbox.io

huangapple
  • 本文由 发表于 2023年7月20日 15:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727767.html
匿名

发表评论

匿名网友

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

确定