Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧐 问题 proTable在自定义valueType的render中拿不到当前行的数据 #2521

Closed
zhangfu-git opened this issue Apr 16, 2021 · 4 comments

Comments

@zhangfu-git
Copy link

zhangfu-git commented Apr 16, 2021

🧐 问题: proTable在自定义valueType的render中拿不到当前行的数据

默认提供的类型无法得到满足, 我想自定义valueType,但发现在valueTypeMap中定义的render和在cloumns中配置的render,参数不同, 无法拿到当前行的数据record?我想更多场景下是需要这个数据的�

@zhangfu-git zhangfu-git changed the title proTable在自定义valueType的render中拿不到当前行的数据 🧐 问题 proTable在自定义valueType的render中拿不到当前行的数据 Apr 16, 2021
@jiyingzhi
Copy link
Contributor

+1

@zhangfu-git
Copy link
Author

@jiyingzhi 目前可以通过valueTypeMap的item.render覆盖columns的item.render进行render自定义,这样可以拿到当前行的数据

@chenshuai2144
Copy link
Contributor

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns.
此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

感谢反馈!

确实,在 valueTypeMap 中定义的 render 和在 columns 中配置的 render 的参数是不同的。但是,你可以通过在 valueTypeMap 中的 render 中使用 record 参数来获取当前行的数据。

以下是一个示例代码:

import ProTable, { ProColumns } from '@ant-design/pro-table';

interface DataType {
  name: string;
  age: number;
  address: string;
}

const data: DataType[] = [
  {
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
  },
  {
    name: 'Joe Black',
    age: 42,
    address: 'London No. 1 Lake Park',
  },
];

const columns: ProColumns<DataType>[] = [
  {
    title: 'Name',
    dataIndex: 'name',
    valueType: 'text',
  },
  {
    title: 'Age',
    dataIndex: 'age',
    valueType: 'custom',
    // 定义一个自定义的 render,可以在其中获取当前行的数据
    valueEnum: {
      male: { text: 'Male', status: 'Success' },
      female: { text: 'Female', status: 'Error' },
    },
    render: (text, record) => {
      const { age } = record;
      const gender = age % 2 === 0 ? 'female' : 'male';
      return (columns[1].valueEnum || {})[gender]?.text ?? text;
    },
  },
  {
    title: 'Address',
    dataIndex: 'address',
    valueType: 'text',
  },
];

export default () => (
  <ProTable<DataType>
    columns={columns}
    dataSource={data}
  />
);

在上面的示例代码中,我们在 columns 中定义了一个 custom 类型的列,并且在 valueEnum 中定义了两个枚举值,并且在 render 中使用了 record 参数来获取当前行的数据,根据数据动态计算出要显示的值。

希望这可以解决你的问题!

@wells974
Copy link

@chenshuai2144 怎么能获取当前render中是哪个字段?我的场景是在ProTable中点击某个字段(Switch组件),能够提交请求{id:record.id,[currentKey]:record[currentKey]},但是在render中好像无法知道当前操作的是哪一列?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants