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

使用Table组件并设置为前端分页和过滤后,点击过滤时,列的render和onFilter函数得到的record对象类型和初始传入的不一致 #48642

Closed
louz opened this issue Apr 26, 2024 · 1 comment

Comments

@louz
Copy link

louz commented Apr 26, 2024

Reproduction link

https://github.com/louz/antd-issue

Steps to reproduce

// 用户类定义
export class User {
  id: string;
  name: string;
  gender: 'M' | 'F';

  constructor(args: {
   id: string;
  name: string;
  gender: 'M' | 'F';
  }) {
    this.id = args.id;
    this.name = args.name;
    this.gender = args.gender;
  }

  get genderText(): string {
     return this.gender === 'M' ? '男' : '女';
  }
}

// column配置
const columns = [{
  title: 'id',
  dataIndex: 'id',
  key: 'id'
}, {
  title: 'name',
  dataIndex: 'name',
  key: 'name'
}, {
  title: 'gender',
  dataIndex: 'gender',
  key: 'gender',
  filters: [{
    text: 'M',
    value: '男'
  }, {
    text: 'F',
    value: '女'
  }],
  onFilter: (value, record) => record.gender === value,
  render: (_, record) => {
     console.log({record});
     return record.genderText;
  }
}]

const users: User[] = getUsers();   // 初始化得到一个User数组

// 然后使用table组件进行渲染
<Table rowKey="id" dataSource={users} columns={columns} />
  1. 第一次渲染时,可以正确显示gender列的,gender列的render函数得到的record对象也是User类型的
  2. 但当点击gender列的过滤按钮后,gender列的render函数得到的record对象并不是User类型的,只是一个有id、name、gender字段的普通对象,没有genderText属性或方法,因此也不能正确显示出该列的值

What is expected?

点击gender列的过滤按钮后,gender列的render函数得到的record对象仍是User类型的

What is actually happening?

但当点击gender列的过滤按钮后,gender列的render函数得到的record对象并不是User类型的,只是一个有id、name、gender字段的普通对象,没有genderText属性或方法,因此也不能正确显示出该列的值;

就是说,希望在任何时候获取到的行数据对象,仍然和当初传入dataSource的元素对象类型保持一致

Environment Info
antd 5.16.4
React 18.2
System win10
Browser edge 113
@MadCcc
Copy link
Member

MadCcc commented May 8, 2024

需要保证 dataSource 可序列化,内部不会保留 class

@MadCcc MadCcc closed this as completed May 8, 2024
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

2 participants