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

🧐[request参数不一致]request的sort新版本返回undefined #7369

Closed
lichdkimba opened this issue Jul 14, 2023 · 2 comments
Closed

🧐[request参数不一致]request的sort新版本返回undefined #7369

lichdkimba opened this issue Jul 14, 2023 · 2 comments
Labels

Comments

@lichdkimba
Copy link

提问前先看看:

https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md

🧐 问题描述

之前是返回{}的,现在第一页返回{},翻页后返回undefined

💻 示例代码

import { Button } from 'antd';
import './App.css';
import {ProTable} from '@ant-design/pro-table';

const App = () => (
    <div className="App">
      <Button type="primary">Button</Button>
      <ProTable request={function (params, sort) {
          console.log(params, sort)
          return {data: [{
              index: 1
          },{
              index: 2
          },{
              index: 3
          },{
              index: 4
          },{
              index: 5
          }],
              total: 15
          }
      }} columns={[
          {
              dataIndex: 'index',
              title: '测试',
              width: 100,
              align: 'center',
          },]} pagination={{
          size: 10,
          pageSize: 5
      }}></ProTable>
    </div>
);

export default App;

🚑 其他信息

image

@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 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

你好!根据你的描述和示例代码,我看到你在使用 Ant Design ProTable 组件,并且在 request 属性中传入了一个函数。根据我理解的情况,你的问题可能是在新版本中,request 函数的第二个参数 sort 返回了 undefined,而之前的版本是正常返回的 {}。我会为你提供一个可能的解决方案。

根据 Ant Design ProTable 的文档,request 函数可以接收两个参数:paramssort,其中 params 是包含查询参数的对象,sort 是用于排序的参数。在旧版本中,当没有进行排序操作时,sort 参数会被传入一个默认空对象 {}。然而,在新版本中,如果没有进行排序操作,sort 参数将返回 undefined,这是一个在文档中没有明确定义的行为。

为了解决这个问题,你可以在 request 函数内部对 sort 参数进行判断处理,当它为 undefined 时,给它一个默认的空对象值,以确保代码的正常运行。

以下是一个示例的修改代码:

import React from 'react';
import { Button } from 'antd';
import './App.css';
import {ProTable} from '@ant-design/pro-table';

const App = () => (
    <div className="App">
      <Button type="primary">Button</Button>
      <ProTable request={function (params, sort) {
          console.log(params, sort)

          // 对 sort 参数进行判断和处理
          if (typeof sort === 'undefined') {
              sort = {};
          }

          return {
              data: [{
                  index: 1
              }, {
                  index: 2
              }, {
                  index: 3
              }, {
                  index: 4
              }, {
                  index: 5
              }],
              total: 15
          }
      }} columns={[
          {
              dataIndex: 'index',
              title: '测试',
              width: 100,
              align: 'center',
          },
      ]} pagination={{
          size: 10,
          pageSize: 5
      }}></ProTable>
    </div>
);

export default App;

通过上述的修改,无论 sort 参数是否为 undefined,都会被赋予一个默认的空对象值,以避免出现错误。

另外,你也可以查阅 Ant Design ProTable 的官方文档,了解更多关于 request 函数和数据请求的信息,这样你可以更好地理解和使用这个组件:

希望这个解决方案对你有帮助!如果你还有其他问题,请随时提问。

@chenshuai2144
Copy link
Contributor

是故意这么做的,不然sort 总是有值,让一些java服务直接挂掉了

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

No branches or pull requests

2 participants