自定义antd table子表格的展开和收起,一键展开收起

 效果:

代码: 

import React from 'react';
import {Table} from 'antd';
const key = []; // 定义一个空的数组。
export default class xiaoyu extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            open: false,

            arr: [],
        };
    }

    open = (e, record) => {
        if (e.target.innerHTML == '展开') {
            key.push(record.key); // 点击的每一行的key的值保存下来。

            this.setState({
                arr: key,
            });

            e.target.innerHTML = '收起';
        } else if (e.target.innerHTML == '收起') {
            key.splice(key.indexOf(record.key), 1); // 再次点击的时候从数组删除上次存入的key值。

            this.setState({
                arr: key,
            });

            e.target.innerHTML = '展开';
        }
    };

    render() {
        const columns = [
            {title: 'Name', dataIndex: 'name', key: 'name'},

            {title: 'Age', dataIndex: 'age', key: 'age'},

            {title: 'Address', dataIndex: 'address', key: 'address'},

            {
                title: 'Action',
                dataIndex: '',
                key: 'x',
                render: (text, record) => (
                    <a href="javascript:;" onClick={e => this.open(e, record)}>
                        展开
                    </a>
                ),
            },
        ];

        const data = [
            {
                key: 1,
                name: 'John Brown',
                age: 32,
                address: 'New York No. 1 Lake Park',
                description: 'My name is John Brown, I am 32 years old, living in New York No. 1 Lake Park.',
            },

            {
                key: 2,
                name: 'Jim Green',
                age: 42,
                address: 'London No. 1 Lake Park',
                description: 'My name is Jim Green, I am 42 years old, living in London No. 1 Lake Park.',
            },

            {
                key: 3,
                name: 'Joe Black',
                age: 32,
                address: 'Sidney No. 1 Lake Park',
                description: 'My name is Joe Black, I am 32 years old, living in Sidney No. 1 Lake Park.',
            },
        ];

        return (
            <div>
                <Table
                    columns={columns}
                    expandedRowRender={record => <p style={{margin: 0}}>{record.description}</p>}
                    dataSource={data}
                    expandedRowKeys={this.state.arr}
                    rowKey={row => row.key}
                />
            </div>
        );
    }
}

原理:

表格 Table - Ant Design

 可以用来操作一键展开和一键收起

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用 Ant Design Vue 的表格树组件,您需要先安装和导入 Table 和 TreeSelect 组件。 在表格中,您需要使用 `customExpandIcon` 属性来自定义展开/折叠图标,并使用 `customRow` 属性来自定义行的渲染。 以下是一个示例代码: ```vue <template> <a-table :columns="columns" :data-source="data" :custom-row="customRow" :custom-expand-icon="customExpandIcon"> <template slot="name" slot-scope="{ record }"> {{ record.name }} </template> </a-table> </template> <script> import { Table, TreeSelect } from 'ant-design-vue'; export default { components: { Table, TreeSelect, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name', scopedSlots: { customRender: 'name' }, }, { title: 'Age', dataIndex: 'age', key: 'age', }, { title: 'Address', dataIndex: 'address', key: 'address', }, ], data: [ { key: '1', name: 'Parent 1', age: 30, address: 'New York No. 1 Lake Park', children: [ { key: '1-1', name: 'Child 1-1', age: 12, address: 'New York No. 2 Lake Park', }, { key: '1-2', name: 'Child 1-2', age: 13, address: 'New York No. 3 Lake Park', }, ], }, { key: '2', name: 'Parent 2', age: 32, address: 'London No. 1 Lake Park', children: [ { key: '2-1', name: 'Child 2-1', age: 11, address: 'London No. 2 Lake Park', }, { key: '2-2', name: 'Child 2-2', age: 16, address: 'London No. 3 Lake Park', }, ], }, ], }; }, methods: { customExpandIcon({ record, onExpand }) { if (record.children) { return ( <a onClick={onExpand}> {record._expanded ? <a-icon type="minus-square" /> : <a-icon type="plus-square" />} </a> ); } return null; }, customRow(record, index, indent, expanded) { return { class: `custom-row-${record.key}`, on: { click: () => { console.log(record); }, }, }; }, }, }; </script> ``` 在上面的代码中,您可以看到我们自定义展开/折叠图标和行的渲染,以及使用了插槽 `scopedSlots` 来渲染名称列。您还可以根据您的需求来添加其他自定义的组件或插槽。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值