You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为MUI DataGrid的preProcessEditCellProps添加错误提示信息?

MUI DataGrid 添加编辑单元格错误提示文本

要实现编辑单元格时显示“Supplier Name cannot be empty”的错误提示,你需要结合preProcessEditCellProps和自定义编辑单元格渲染函数来实现——因为默认的编辑组件不直接支持helperText属性。具体修改如下:

修改后的列配置代码

{
  field: "supplierName",
  headerName: "Name",
  width: 300,
  editable: true,
  preProcessEditCellProps: (params: GridPreProcessEditCellProps) => {
    const hasError = params.props.value === '';
    return { 
      ...params.props, 
      error: hasError,
      // 自定义错误消息属性
      errorMessage: hasError ? "Supplier Name cannot be empty" : undefined
    };
  },
  // 自定义编辑单元格渲染函数
  renderEditCell: (params) => {
    return (
      <TextField
        {...params.props}
        // 使用传递过来的错误消息作为helperText
        helperText={params.props.errorMessage}
        fullWidth
        variant="standard"
      />
    );
  }
}

说明

  1. preProcessEditCellProps:除了设置error标记外,新增自定义的errorMessage属性存储错误文本,该属性会传递给后续的渲染组件。
  2. renderEditCell:通过自定义渲染函数替换默认编辑组件,使用MUI的TextField组件,将errorMessage赋值给helperText属性,同时继承原有props配置(如error状态),输入为空时就能显示指定的错误提示。

内容的提问来源于stack exchange,提问作者izyben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 03:22:44