ant-design / pro-components

🏆 Use Ant Design like a Pro!
https://pro-components.antdigital.dev
MIT License
4.04k stars 1.29k forks source link

🐛[BUG][ProTable] defaultSortOrder 没有传递到 request 的 sort 参数中 #1179

Closed Grapedge closed 3 years ago

Grapedge commented 3 years ago

🐛 bug 描述

表格列设置了 defaultSortOrder 属性,本地排序表现正常,但 request 的参数 sort 没有显示此排序项目。

📷 复现步骤

见代码

🏞 期望结果

期望 sort 中可以获取到相应的排序信息以便远程排序。

💻 复现代码

export default () => {
  return (
    <ProTable<any>
      rowKey="sort"
      columns={[
        {
          title: '希望该列可以默认排序,如创建时间',
          dataIndex: 'sort',
          // 期望默认排序项目可以传递到 request 中
          defaultSortOrder: 'descend',
          sorter: (a, b) => a.sort - b.sort,
        },
      ]}
      request={async (params, sort, filters) => {
        // 希望在请求中的sort中可以获取到 { sort: 'descend' }
        console.log(params, sort, filters);
        // 实际:=> {current: 1, pageSize: 20} {} {}
        return {
          total: 2,
          data: [
            {
              sort: 1,
            },
            {
              sort: 2,
            },
          ],
        };
      }}
    />
  );
};

© 版本信息

🚑 其他信息

DTnTT1.png

补充:当点击刷新按钮时表现仍然异常,但切换分页后就可以正常传递

ccloveak commented 3 years ago

我也要开始做排序了,握抓。直观感觉可能是 protable的版本跟react的版本有点不匹配。

chenshuai2144 commented 3 years ago

protable 的排序不是受控的,所以有点麻烦

ccloveak commented 3 years ago

在获取数据时,可以设个sort的初始值,假装获取的是已经排序的数据,然后前端点击倒序正序后的页面显示跟第一次渲染的结果相同

renanwilliam commented 3 years ago

I have the same bug here, defaultsSort order is defined but requests ignores it.

minosss commented 3 years ago

+1 设置了默认排序有显示状态,第一次点击无法触发 request 方法,第二次点击可以正常触发

proxcentb commented 3 years ago

Is the fix coming? Created codesandbox with the bug: https://codesandbox.io/s/pro-table-bug-d8pms

chenshuai2144 commented 3 years ago

dup of https://github.com/ant-design/pro-components/issues/1581

proxcentb commented 3 years ago

@chenshuai2144 I am sorry, but I don't quite get it, this issue is about defaultSortOrder not passing to request, but https://github.com/ant-design/pro-components/issues/1581 is different. My chinese knowledge is very low, can you explain how these are related please?

renanwilliam commented 3 years ago

@proxcentb to set a default value, use the property initialValue in ProColumnType.

proxcentb commented 3 years ago

@renanwilliam I'm sorry, I still don't get it... I just want defaultSortOrder to be passed to request, but setting defaultSortOrder for some column doesn't work. What should I do exactly?

renanwilliam commented 3 years ago

Don't worry, I spent a lot of time on this too. Instead use defaultSortOrder property, just use initialValue property and it will works like expected.

proxcentb commented 3 years ago

@renanwilliam I tried to replace defaultSortOrder with InitialValue, but still no luck, can you please fork and edit this codesandbox so that sort order is passed to request? https://codesandbox.io/s/pro-table-bug-84qho

renanwilliam commented 3 years ago

Hi @proxcentb, Sorry, I mistook me with the defaultFilter/ initialValue options.

The defaultSortOrder bug really exists, the workaround can be checked at https://codesandbox.io/s/pro-table-bug-forked-c4ezr?file=/src/index.tsx

Is not pretty but is a viable alternative

proxcentb commented 3 years ago

@renanwilliam I've been thinking about this workaround as well, but what if user doesn't want any sorting? In your example sort property will always be sent. I will try to find a different workaround, but nontheless, bug still persists and I don't believe it is a duplicate of https://github.com/ant-design/pro-components/issues/1581. Thank you for trying to help though 😃

jifeon commented 3 years ago

@chenshuai2144 can we reopen this bug? Looks like the guys found out above that it really exists.

wmy1992 commented 3 years ago

这还没解决呢怎么就close了?

fancygeek commented 3 years ago

I‘m use params filed to replace the sorter in the request function.store the sorter use useState hook. 我用params这个参数来代替request方法中的sorter参数。然后自己使用useState来存储sorter,在onChange中将sorter存在state里面

yanghuanrong commented 2 years ago

我在请求函数中使用params提交来替换排序 器。存储排序器使用useState hook。我用params这个参数来代替请求方法中的排序器参数。然后自己使用useState来存储排序器,在onChange中将排序器存在状态里面

这个onChange是在什么时候触发的呢可以看看相关代码吗?