react ~4.组件table的使用及表格分页处理

antd里面表格数据分为三部分,columns:该对象数组用于设置表头数据,里面的dataIndex是对象数据里面对应的key, data:该对象数组用于设置每行的对应表头columns的数据, pagination:该对象用于设置分页

columns: [{
                title: '序号',
                dataIndex: 'indexs',
                className: 'first-column',
                width: '80px'
            }, {
                title: '服务名称',
                dataIndex: 'asvServiceName',
                className: 'middle-column',
                width: '180px'
            }, {
                title: 'api_name',
                dataIndex: 'asvServiceId',
                className: 'middle-column',
                width: '220px'
            }, {
                title: '所属系统',
                dataIndex: 'asvCallPlateform',
                className: 'middle-column',
                width: '150px',
                render: (text, record) => (
                    
                        {text == '01' ? '连接平台'
                            : text == '02' ? '中台'
                                : text == '03' ? '理财'
                                    : text == '04' ? '网贷'
                                        : text == '05' ? '核心'
                                            : text == '06' ? '风控'
                                                : text == '07' ? 'jarslink' : ''}
                    
                ),
            }, {
                title: '服务状态',
                dataIndex: 'asvStatus',
                width: '100px',
                className: 'middle-column',
                render: (text, record) => (
                    {text == '0' ? '启用' : text == '1' ? '停用' : ''}
                ),
            }, {
                title: '版本号',
                dataIndex: 'apiVersion',
                width: '100px',
                className: 'middle-column',
            }, {
                title: '注册时间',
                dataIndex: 'createDate',
                className: 'middle-column',
                width: '170px'
            }, {
                title: '操作',
                width: '220px',
                dataIndex: 'action',
                className: 'last-column',
                render: (text, record) => (
                    <Fragment>//用于包裹需要包裹的标签,但是不会生成标签
              //之所以不用antd的Button组件,是因为在使用的时候会出现莫名样式,eg:圣诞节的时候出现的顶部白云块。对于要求严苛的设计页面是不允许出现这种情况的 <button className='table-button' style={{ marginRight: 10 }} onClick={() => this.queryHandle(record)}> 查看</button> <button className='table-button' style={{ marginRight: 10 }} onClick={() => this.modifyHandle(record)}>修改</button> {/* <button className='table-button' onClick={() => record.asvStatus == '0' ? this.deleteHandle(record, record.apiVersion) : this.openHandle(record.asvServiceId, record.apiVersion)}>{record.asvStatus == '0' ? '停用' : record.asvStatus == '1' ? '启用' : ''}</button> */} </Fragment> ), }], data: [], paginationNo: { pageSize: 10,//每页的条数 current: 1,//当前页 total: 10,//总数据 onChange: this.pagesNo.bind(this),//点击分页号时触发的方法 hideOnSinglePage: true//为true则数据条数不满或刚好一页,则隐藏分页器 },
  componentDidMount() {
        this.setState({
            loadhold: true,
        });
        this.pagesNo(1, 10);
    }

    toNew(item) {
        this.props.history.push('/service/atom/add');
    }

    queryHandle(item) {
        this.props.history.push({
            pathname: "/service/atom/query",
            state: {
                item: item
            }
        });
    }

    addHandle(record) {
        console.log(record.indexs);
    }

    modifyHandle(record) {
        this.props.history.push({
            pathname: "/service/atom/modify",
            state: {
                item: record
            }
        });
    }

    deleteHandle(record, val) {
        this.setState({
            hasVisible: true,
            asvServiceId: record.asvServiceId,
            apiVersion: val,
            apiId: record.apiId,
            itemData: {
                title: '服务停用',
                tbody: '服务停用后,产品将无法关联该服务。若需关联,需重新启用该服务'
            }
        });
    }

    openHandle(item, val) {
        let that = this;

        const data = {
            'serviceOperateType': 1,
            'asvServiceId': item,
            'apiVersion': val,
            'apiId': this.state.apiId
        };

        const apiUrl1 = MI.baseURI + `/ability/start-or-shutdown`;
        fetchPost(apiUrl1, data).then((response) => {
            if (response.success) {
                message.success('启用成功!!');
                that.setState({
                    loadhold: true,
                });
                this.pagesNo(1, 10);

            } else {

                this.setState({
                    loadhold: false,
                });
                message.error(response.msg);
            }
        });
    }

    changeStatus = (status, status_bool) => {
        let that = this;
        this.setState({
            hasVisible: status
        });
        if (status_bool) {

            const data = {
                'serviceOperateType': 0,
                'asvServiceId': this.state.asvServiceId,
                'apiVersion': this.state.apiVersion,
                'apiId': this.state.apiId
            };
            const apiUrl1 = MI.baseURI + `/ability/start-or-shutdown`;
            fetchPost(apiUrl1, data).then((response) => {
                if (response.success) {
                    message.success('停用成功!!');
                    that.setState({
                        loadhold: true,
                    });

                    this.pagesNo(1, 10);
                } else {

                    this.setState({
                        loadhold: false,
                    });
                    message.error(response.msg);
                }
            });
        }
    }
    pagesNo(page, pageSize) {
        this.setState({
            loadhold: true,
        });
        var jjs = (page - 1) * pageSize;
        const data = {
            "pageNum": page,
            "pageSize": '10',
            'asvCallPlateform': this.state.asvCallPlateform == '00' ? '' : this.state.asvCallPlateform,
            'asvServiceName': this.state.asvServiceName,
            'asvStatus': this.state.serviceOperateType == 'jack' ? '' : this.state.serviceOperateType
        };
        const apiUrl1 = MI.baseURI + `/ability/list`;
        fetchPost(apiUrl1, data).then((response) => {
            if (response.success) {
                for (var i = 0; i < response.data.list.length; i++) {
                    response.data.list[i].indexs = i + 1 + jjs;//设置序号
                }
                this.setState({
                    data: response.data.list,
                    loadhold: false,
                    paginationNo: {
                        pageSize: response.data.pageSize,
                        current: response.data.pageNum,
                        total: response.data.totalCount,
                        onChange: this.pagesNo.bind(this),
                        className: 'myPagination',
                        showTotal: showTotal//
 
                         
                showTotal方法  function showTotal(total) {//用于分页器显示总条数的方法
                        return `共${total}条记录`;
                      }
 
  

 


                    }
                });
            } else {
                this.setState({
                    loadhold: false,
                });
                message.error(response.msg);
            }
        });
    }


    //所属系统
    handleChangeS(val) {
        console.log(val);
        this.setState({
            asvCallPlateform: val
        });
    }
    //服务状态
    handleChangeM(val) {
        console.log(Number(val));
        this.setState({
            serviceOperateType: Number(val)
        });
    }
    //服务名称
    sm_servicename_query(e) {
        this.setState({
            asvServiceName: e.target.value
        });
    }
    sm_query() {//通过条件查询查询
        var that = this;
        this.setState({
            loadhold: true,
        });

        this.pagesNo(1, 10);
    }
    //重置
    sm_reset() {
        var that = this;
        this.setState({
            loadhold: true,
            asvCallPlateform: '00',
            asvServiceName: '',
            serviceOperateType: 'jack'

        });
        setTimeout(() => {
            this.pagesNo(1, 10);
        }, 1000);

    }

 

转载于:https://www.cnblogs.com/sunshineForFuture/p/10343047.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用antd table组件进行分页获取数据,需要在表格中设置pagination属性,同时需要在后端接口中实现分页查询功能。 对于跨页多选,antd table提供了rowSelection属性,可以通过设置type为checkbox实现多选,但是默认只能在当前页进行选择,无法跨页选择。 要实现跨页多选,需要自定义rowSelection的selectedRowKeys和onChange方法。具体步骤如下: 1. 在state中定义selectedRowKeys数组,用于保存已选中的行的key值。 2. 在rowSelection属性中设置selectedRowKeys为state中定义的selectedRowKeys数组。 3. 在rowSelection属性中定义onChange方法,用于处理选中行的变化。在该方法中,需要使用setState更新state中的selectedRowKeys数组。 4. 在表格的pagination属性中设置onChange方法,用于处理页码变化时清空已选中的行。 以下是示例代码: ``` import React, { Component } from 'react'; import { Table } from 'antd'; class MyTable extends Component { state = { selectedRowKeys: [], // 保存已选中的行的key值 }; onSelectChange = selectedRowKeys => { // 更新state中的selectedRowKeys数组 this.setState({ selectedRowKeys }); }; onPaginationChange = () => { // 清空已选中的行 this.setState({ selectedRowKeys: [] }); }; render() { const { selectedRowKeys } = this.state; const rowSelection = { type: 'checkbox', selectedRowKeys, onChange: this.onSelectChange, }; return ( <Table rowSelection={rowSelection} columns={columns} dataSource={data} pagination={{ onChange: this.onPaginationChange }} /> ); } } ``` 注意,以上代码仅为示例,具体实现需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值