ant design table隔行换色以及自定义单元格数据

2019-03-20  本文已影响0人  晴窗细语

最近做个表格,想做隔行换色以及自定义单元格数据展示的效果,到网上找了一些方法,算是大概实现了,记录一下。(总结还是一句话,多看API文档)

table页面展示
constructor() {
    ...
    this.columns = [{
        title: 'Research Topic',
        dataIndex: 'name',
        sorter: true,
        className: 'topic-info',
        render: (text, record) => (
            <div>
                <p><Link to="/">{text.split("|")[0]}</Link></p>
                <p title={`${text.split("|")[1]}`}>{text.split("|")[1]}</p>
            </div>
        ),
    }, {
        title: 'Researchers',
        dataIndex: 'creatorName',
        sorter:true,
        width: '15%',
    }, {
        title: 'Created',
        dataIndex: 'created',
        sorter: true,
        width: '12%',
    }, {
        title: 'Updated',
        dataIndex: 'updated',
        sorter: true,
        width: '12%',
    }, {
        title: 'Operation',
        dataIndex: 'operation',
        width: '12%',
        render: (text, record) => (
            <span>
                <a>Edit</a>
                <Divider type="vertical" />
                <Popconfirm title="Sure to delete?" onConfirm={() => this.handleDelete(record.key)}>
                <a>Delete</a>
                </Popconfirm>
            </span>
        ),
    }];
    ...
}

render() {
    const { loading, error, topics, tags, topicNum, tagNum, current } = this.state;
    let data = [];
    topics.forEach((topic, idx) => {
        //data属性name是把两项内容拼接在一起,用的时候split一下
        data.push({
            key: topic.id,
            name: topic.name + "|" + topic.description,
            creatorName: topic.creatorName,
            created: formatDate(topic.created, "yyyy-MM-dd"),
            updated: formatDate(topic.updated, "yyyy-MM-dd"),
        });
    });
    return(
        <Table
            className={styles['table']}
            loading={loading}
            rowSelection={rowSelection}
            columns={this.columns}
            dataSource={data}
            pagination={false}
            bordered
            rowClassName={(record, idx) => {
                if(idx % 2 === 1)
                    return 'bg-row';
            }}
        />
    );
}
样式
:global{
    .bg-row {
        background-color: $gray-color-12;
    }
    .topic-info {       
        p:first-child{
            font-family: Arial, sans-serif;
        }
        p+p {
            color: $gray-color-5;
            margin-left: 20px;
            text-indent: ellipsis;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读