关于defaultSortOrder或sortOrder降序问题,切换数据后就失效 antd

http404
  • 251

页面刷新初始化正常,一旦下拉切换age选项数据的话,降序就失效,怎样每次切换下拉数据不影响降序呢

image

antd Table columns降序问题

const {  Table, Button,Select  } = antd;
const { Option } = Select;
const title = [
  {
    title: 'Name',
    dataIndex: 'name',
  },
  {
    title: 'Age',
    dataIndex: 'age',
    defaultSortOrder: 'descend',
    sorter: (a,b)=>a.age-b.age,
  },
];

const data = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    age2: 12,
    age3: 2,
    age4: 52,
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    age2: 2,
    age3: 12,
    age4: 22,
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 12,
    age2: 62,
    age3: 31,
    age4: 31,
  },
];

class App extends React.Component {
  state = {
    age: 'age',
    columns:title,
  };
  
  onChageAge = age => {
    const {columns} = this.state;
    console.log(age,'=age');
    columns[1].dataIndex = age;
    //columns[1].sortOrder = 'descend',
    columns[1].sorter = (a,b)=>a[age]-b[age],
    this.setState({age,columns});
  };

  render() {
    const {columns} = this.state;
    return (
      <div>
        <Select
         value={this.state.age}
         onChange={this.onChageAge}
         style={{ width: 200 }}
        >
           <Option value='age'>age</Option>
           <Option value='age2'>age2</Option>
           <Option value='age3'>age3</Option>
           <Option value='age4'>age4</Option>
        </Select>
        <Table columns={columns} dataSource={data} />
      </div>
    );
  }
}

ReactDOM.render(<App />, mountNode);
回复
阅读 79
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏