Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

antd table sort时能不能保证第一行永远在表头呢

表格里第一行是总览行,需要一直置顶,但是自带的sorter排序会把他加进去一起排序,假如给第一行加一个最大的权重,升序的时候又回排到最后一行,希望能保证这一行永远在第一行不变,怎么办呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我大概实现了一下,思路就是

  1. 对于第一行会有一个独有的标记,能让我在sorter函数里面知道这是第一行
  2. 在sorter里面异步修改排序的值,使其永远保证最大或者最小,异步保证本次排序不受影响

const columns = [
    {
        title: 'test',
        dataIndex: 'test',
        sorter: (a, b) => {
            const result = a.num - b.num;
            setTimeout(() => {
                if (a.test === 'total') {
                    a.num = 0 - a.num;
                }
                if (b.test === 'total') {
                    b.num = 0 - b.num;
                }
                console.log(a, b);
            });
            return result;
        }
    }
];

const dataSource = [
    { test: 'total', num: -1000 },
    { test: '1', num: 1 },
    { test: '22', num: 2 },
    { test: '33', num: 33 },
    { test: '4', num: 4 }
];

<Table columns={columns} dataSource={dataSource} rowKey="test"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...