ant design的Table分页

页面Welcome.jsx

错误代码显示

import React from 'react';

import {  Table, Input } from 'antd';
const { Search } = Input;
import moment from 'moment'
export default class Welcome extends React.Component{
  constructor(props){
    super(props);
    let format = 'YYYY-MM-DD HH:mm:ss';
    this.state = {
      columns : [
        {title:'id',dataIndex:'id',key:'id'},
        {title: '红包钱数',dataIndex: 'giftMoney',key:'giftMoney'},
        {title:'用户id',dataIndex:'userId',key:'userId'},
        {title:'姓名',dataIndex:'userName',key:'userName'},
        {title:'状态',dataIndex:'status',key:'status'},
        {title:'发红包时间',dataIndex:'giftTime',key:'giftTime',render: record => {
            return moment(record).format(format);
          },}
      ],
      page:1,
      total:0,
      size:5,
      value:"",
      columnData:[]
    }
  }

  onSerchUserName(value,page,size){
    this.setState({value:value})
    console.log(this.state.page+"this.state.page")
    let _fetchUrl = "/data/money/selectGiftByUserName?userName=" + value+"&page="+page+"&size="+size;
    fetch(_fetchUrl, {
      method: 'GET',
    })
      .catch(error => {
      })
      .then(res => res.json())
      .then(data => {
        if (data.success) {
          if (data.obj != null) {
            console.log(data,"data")
            this.setState({
              columnData: data.obj.giftWrapperList,
              total:data.obj.count
            });
          }
        } else {
          message.warn(data.msg)
        }
      });
  }

  render() {
    let list = this.state.columnData;
    //演示错误写法,处理表格的数据
    list.map(item =>{
      item.giftMoney = item.giftMoney+"元";
      if (item.status == 1){
        item.status = "成功";
      } else {
        item.status = "失败";
      }
    })
    
    const  pagination={
      total:this.state.total,
      // current:this.state.page,
      pageSize:this.state.size,
      onChange: (page, size) => {
        this.setState({
          page: page,
        });
        console.log(size,"size")
        console.log(this.state.size,"this.state.size")
        console.log(this.state.page,"this.state.page")
        console.log(page,"page")
        this.onSerchUserName(this.state.value,this.state.page,this.state.size);
      },
    }
    return (
      <div>
        <Search
          placeholder="input search text"
          enterButton="Search"
          size="large"
          style={{ width: 400,marginBottom:10}}
          onSearch={value => this.onSerchUserName(value,1,this.state.size)}
        />
        <Table
          columns={this.state.columns}
          dataSource={list}
          pagination={pagination}
        />
      </div>
    );
  }
}

错误代码原因:根本原因setState就相当于是一个异步操作,不能立即被修改。onSerchUserName参数传值使用了this.state.page,应该使用page,当页面点击第2页,this.state.page的值是1,page的值是2;当页面点击第3页时,this.state.page的值是2,page的值是3;所以page才是需要的值。

  pageSize:this.state.size,
      onChange: (page, size) => {
        this.setState({
          page: page,
        });
        console.log(size,"size")
        console.log(this.state.size,"this.state.size")
        console.log(this.state.page,"this.state.page")
        console.log(page,"page")
        this.onSerchUserName(this.state.value,this.state.page,this.state.size);//错误原因

打印结果:
在这里插入图片描述
正确改法:

import React from 'react';

import {  Table, Input,Pagination } from 'antd';
const { Search } = Input;
import moment from 'moment'
export default class Welcome extends React.Component{
  constructor(props){
    super(props);
    let format = 'YYYY-MM-DD HH:mm:ss';
    this.state = {
      columns : [
        {title:'id',dataIndex:'id',key:'id'},
        {title: '红包钱数',dataIndex: 'giftMoney',key:'giftMoney'},
        {title:'用户id',dataIndex:'userId',key:'userId'},
        {title:'姓名',dataIndex:'userName',key:'userName'},
        {title:'状态',dataIndex:'status',key:'status'},
        {title:'发红包时间',dataIndex:'giftTime',key:'giftTime',render: record => {
            return moment(record).format(format);
          },}
      ],
      page:1,
      total:0,
      size:5,
      value:"",
      columnData:[]
    }
  }

  onSerchUserName(value,page,size){
    this.setState({value:value})
    console.log(this.state.page+"this.state.page")
    let _fetchUrl = "/data/money/selectGiftByUserName?userName=" + value+"&page="+page+"&size="+size;
    fetch(_fetchUrl, {
      method: 'GET',
    })
      .catch(error => {
      })
      .then(res => res.json())
      .then(data => {
        if (data.success) {
          if (data.obj != null) {
            console.log(data,"data")
            this.setState({
              columnData: data.obj.giftWrapperList,
              total:data.obj.count
            });
          }
        } else {
          message.warn(data.msg)
        }
      });
  }

  render() {
    let list = this.state.columnData;
    //演示错误写法,处理表格的数据
    list.map(item =>{
      item.giftMoney = item.giftMoney+"元";
      if (item.status == 1){
        item.status = "成功";
      } else {
        item.status = "失败";
      }
    })
    const  pagination={
      total:this.state.total,
      pageSize:this.state.size, //必须加上
      onChange: (page, size) => {
        console.log(size,"size")
        console.log(this.state.total,"total")
        this.onSerchUserName(this.state.value,page,size);
      },

    }
    return (
      <div>
        <Search
          placeholder="input search text"
          enterButton="Search"
          size="large"
          style={{ width: 400,marginBottom:10}}
          onSearch={value => this.onSerchUserName(value,1,this.state.size)}
        />
        <Table
          columns={this.state.columns}
          dataSource={list}
          pagination={pagination}
        />
      </div>
    );
  }
}

Table默认pageSize是10,total会根据pageSize自动算出页数page

坑:不加pageSize:this.state.size,只有是10页的时候会分页成功;不是10页,比如size:5在state初始化时,分页不好用。

此代码还有一个不妥之处和分页无关
在这里插入图片描述
处理Table数据不应该在这数据,如果在这里处理,每次查询页面,页面刷新一次就会抖动,正确代码应该放在设置table列的时候在render处理

import React from 'react';

import {  Table, Input,Pagination } from 'antd';
const { Search } = Input;
import moment from 'moment'
export default class Welcome extends React.Component{
  constructor(props){
    super(props);
    let format = 'YYYY-MM-DD HH:mm:ss';
    this.state = {
      columns : [
        {title:'id',dataIndex:'id',key:'id'},
        {title: '红包钱数',dataIndex: 'giftMoney',key:'giftMoney',render:record =>{
          return record+"元"
          }},
        {title:'微信昵称',dataIndex:'userName',key:'userName'},
        {title:'状态',dataIndex:'status',key:'status',render:record =>{
          return record==1?"成功":"失敗";
          }},
        {title:'更新时间',dataIndex:'giftTime',key:'giftTime',render: record => {
            return moment(record).format(format);
          },}
      ],
      page:1,
      total:0,
      size:10,
      value:"",
      columnData:[]
    };
    console.log("这里是构造函数内部:"+this)
    console.log(this)
  }


  componentDidMount() {
    this.onSerchUserName(this.state.value,this.state.page,this.state.size);
  }

  onSerchUserName(value,page,size){
    this.setState({value:value})
    console.log(this.state.page+"this.state.page")
    let _fetchUrl = "/data/money/selectGiftByUserName?userName=" + value+"&page="+page+"&size="+size;
    fetch(_fetchUrl, {
      method: 'GET',
    })
      .catch(error => {
      })
      .then(res => res.json())
      .then(data => {
        if (data.success) {
          if (data.obj != null) {
            console.log(data.obj.giftWrapperList,"data.obj.giftWrapperList")
            this.setState({
              columnData: data.obj.giftWrapperList,
              total:data.obj.count
            });

          }
        } else {
          message.warn(data.msg)
        }
      });
    console.log("这里是onSerchUserName:"+this)
    console.log(this)
  }

  render() {
    const  pagination={
      total:this.state.total,
      pageSize:this.state.size, //必须加上
      onChange: (page, size) => {
        console.log(size,"size")
        console.log(this.state.total,"total")
        this.onSerchUserName(this.state.value,page,size);
      },

    }
    return (
      <div>
        <Search
          placeholder="请输入微信名称"
          enterButton="查询"
          size="large"
          style={{ width: 400,marginBottom:10}}
          onSearch={value => this.onSerchUserName(value,1,this.state.size)}
        />
        <Table
          columns={this.state.columns}
          dataSource={this.state.columnData}
          pagination={pagination}
        />
      </div>
    );
  }
}

来源项目答题小程序后台管理页面

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 Ant DesignTable 组件进行合并单元格操作后,会出现分页的问题。这是因为 Table 组件在合并单元格时,对于被合并的单元格只会显示一个单元格内容,但是在分页时,每一页都是按照原始的数据进行分页的,因此会出现某些行被分到不同的页的情况。 解决这个问题的方法是使用 `rowSpan` 和 `colSpan` 属性,而不是合并单元格。具体地,可以在需要合并的单元格上设置 `rowSpan` 或 `colSpan` 属性,这样单元格会占用多行或多列,但是在 Table 组件内仍然算作多个单元格。这样在分页时,每一页都会按照实际的单元格数进行分页,不会出现某些行被分到不同的页的情况。 示例代码如下: ```jsx <Table dataSource={dataSource} pagination={pagination} rowKey="id"> <Column title="Name" dataIndex="name" key="name" /> <Column title="Age" dataIndex="age" key="age" /> <Column title="Address" dataIndex="address" key="address" render={(text, record, index) => { if (index === 0) { return { children: text, props: { rowSpan: 2, // 合并两行单元格 }, }; } if (index === 1) { return null; // 第二行单元格被合并,不需要渲染 } return { children: text, }; }} /> </Table> ``` 在上面的代码中,第三列的单元格需要合并前两行,因此在 `render` 函数中判断行索引,对第一行渲染单元格内容,并设置 `rowSpan` 属性为 2,对第二行返回 `null`,表示该行单元格被合并,不需要渲染。对于其他行,直接渲染单元格内容。这样在分页时,每一页都会按照实际的单元格数进行分页,不会出现某些行被分到不同的页的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值