简体   繁体   English

Ant 设计排序表代码不适用于反应 typescript

[英]Ant design sort table code not working on the react typescript

I used following ant design sort table code in react type script, its not working correctly anyone know how to do that correctly我在反应类型脚本中使用了以下ant 设计排序表代码,它无法正常工作任何人都知道如何正确地做到这一点

My code here我的代码在这里

     import { Table } from 'antd';
  import * as React from 'react';

import { Table } from 'antd';
const columns = [
    {
        title: 'Name',
        dataIndex: 'name',
        filters: [
            {
                text: 'Joe',
                value: 'Joe',
            },
            {
                text: 'Jim',
                value: 'Jim',
            },
            {
                text: 'Submenu',
                value: 'Submenu',
                children: [
                    {
                        text: 'Green',
                        value: 'Green',
                    },
                    {
                        text: 'Black',
                        value: 'Black',
                    },
                ],
            },
        ],
        // specify the condition of filtering result
        // here is that finding the name started with `value`
        onFilter: (value:any, record:any) => record.name.indexOf(value) === 0,
        sorter: (a:any, b:any) => a.name.length - b.name.length,
        sortDirections: ['descend'],
    },
    {
        title: 'Age',
        dataIndex: 'age',
        defaultSortOrder: 'descend',
        sorter: (a:any, b:any) => a.age - b.age,
    },
    {
        title: 'Address',
        dataIndex: 'address',
        filters: [
            {
                text: 'London',
                value: 'London',
            },
            {
                text: 'New York',
                value: 'New York',
            },
        ],
        filterMultiple: false,
        onFilter: (value:any, record:any) => record.address.indexOf(value) === 0,
        sorter: (a:any, b:any) => a.address.length - b.address.length,
        sortDirections: ['descend', 'ascend'],
    },
];

const data = [
    {
        key: '1',
        name: 'John Brown',
        age: 32,
        address: 'New York No. 1 Lake Park',
    },
    {
        key: '2',
        name: 'Jim Green',
        age: 42,
        address: 'London No. 1 Lake Park',
    },
    {
        key: '3',
        name: 'Joe Black',
        age: 32,
        address: 'Sidney No. 1 Lake Park',
    },
    {
        key: '4',
        name: 'Jim Red',
        age: 32,
        address: 'London No. 2 Lake Park',
    },
];
function onChange(pagination:any, filters:any, sorter:any, extra:any) {
    console.log('params', pagination, filters, sorter, extra);
}
//Table sample data
export class Customertable extends React.Component {


    render() {
        return (

            /* Start Search button*/
            <div className="customertable-section">
                <div>
                    <Table columns={columns} dataSource={data} onChange={onChange} />

                </div>


            </div>
            /* End of  Search button*/
        );
    }
}

While declaring the variable in typescript, you need to give it like, columns: any = [] and data: any = [] ..在打字稿中声明变量时,您需要像columns: any = []data: any = [] ..

And while making table , you should give the props like,在制作table ,你应该给道具,比如,

<Table columns={this.columns} dataSource={this.data} />

Working sample antd table with typescript here...此处使用打字稿工作示例 antd 表...

To add on to Maniraj's comment, the documentation provides a section for Typescript specific usage: https://ant.design/components/table/#Using-in-TypeScript为了添加 Maniraj 的评论,文档提供了一个关于 Typescript 特定用法的部分: https ://ant.design/components/table/#Using-in-TypeScript

Setting the type of columns is possible by importing ColumnsType from 'antd/es/table'通过从'antd/es/table'导入ColumnsType可以设置columns的类型

Here the solution that may help someone.这里的解决方案可能会帮助某人。 You should write type for your table config using default ant types and then in compare func, your item is DefaultRecordType, func returns number.您应该使用默认的 ant 类型为表配置编写类型,然后在比较 func 时,您的项目是 DefaultRecordType,func 返回数字。

 const TABLE_COLUMNS: ColumnsType<DefaultRecordType> = [ { title: 'ISO #', dataIndex: 'iso', key: 'iso', sorter: (a: DefaultRecordType, b: DefaultRecordType): number => a.iso.localeCompare(b.iso), },]

You can simply sort your ant design table's column by replace your old code with new:您可以通过将旧代码替换为新代码来简单地对ant 设计表的列进行排序:

Old Code:旧代码:

sorter: (a:any, b:any) => a.name.length - b.name.length,

New Code:新代码:

sorter: (a:any, b:any) => a.name.localeCompare(b.name),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM