vue的表格和分页

前面有提到在vue里面,对于表格的使用:vue2.0 + element-ui 实战项目-渲染表格(四)https://www.jianshu.com/p/fea8cacc04b9,在实战的过程中,往往还要选择一个合适的分页,搭配着一起使用,尤其是数据比较多的时候,必然会做出分页效果。

关于一些经常用到的参考文档:这里都罗列一下,查找起来比较方便

Element UI手册:https://cloud.tencent.com/developer/doc/1270
github地址:https://github.com/ElemeFE/element

vue2.0官方网站:http://caibaojian.com/vue/guide/installation.html
element-ui官方网站:https://element.eleme.cn/#/zh-CN


1:在组件里面找一个自己比较喜欢的分页的样式https://element.eleme.cn/#/zh-CN/component/pagination

其实我们可以看到,文档里面的样式非常的简单
复制一下这段代码

<el-pagination
  background
  layout="prev, pager, next"
  :total="1000">
</el-pagination>

就可以在页面上看到一个静态的分页的效果了

2:最重要的一个步骤就是点击分页的办法

// 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },

3:对表格中获取到的json数据进行处理

:data="pvData.slice((currentPage - 1) * pagesize, currentPage * pagesize)"

4:将前面的静态分页也进行修改一下,加上方法和变量

<el-pagination
      style="margin: 12px 0px"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pvData.length"
    >
    </el-pagination>

5:写一个完整的实例:

json

{"msg":"success","total":0,"code":1,"data":[{"id":6,"userOrganId":null,"userName":"super","sex":1,"realName":"super","birthday":null,"password":"202cb962ac59075b964b07152d234b70","roleId":1,"roleName":"角色1","organId":1,"organName":"部门1","authority":0,"companyId":1,"phone":null,"organIds":null,"isPagination":false,"page":1,"rows":1},{"id":13,"userOrganId":null,"userName":"super2","sex":1,"realName":"super","birthday":null,"password":"202cb962ac59075b964b07152d234b70","roleId":1,"roleName":"角色1","organId":1,"organName":"部门1","authority":0,"companyId":1,"phone":null,"organIds":null,"isPagination":false,"page":1,"rows":1},{"id":14,"userOrganId":null,"userName":"999","sex":1,"realName":"999","birthday":null,"password":"d41d8cd98f00b204e9800998ecf8427e","roleId":1,"roleName":"1","organId":1,"organName":"1","authority":1,"companyId":1,"phone":null,"organIds":null,"isPagination":false,"page":1,"rows":1},{"id":27,"userOrganId":null,"userName":"21","sex":null,"realName":"21","birthday":null,"password":"d41d8cd98f00b204e9800998ecf8427e","roleId":1,"roleName":"","organId":1,"organName":"","authority":1,"companyId":1,"phone":null,"organIds":null,"isPagination":false,"page":1,"rows":1},{"id":28,"userOrganId":null,"userName":"111","sex":1,"realName":"111","birthday":null,"password":"202cb962ac59075b964b07152d234b70","roleId":1,"roleName":"1","organId":1,"organName":"14","authority":1,"companyId":1,"phone":null,"organIds":null,"isPagination":false,"page":1,"rows":1},{"id":29,"userOrganId":null,"userName":"212","sex":0,"realName":"121","birthday":null,"password":"d41d8cd98f00b204e9800998ecf8427e","roleId":1,"roleName":"1","organId":1,"organName":"13","authority":1,"companyId":1,"phone":null,"organIds":null,"isPagination":false,"page":1,"rows":1}]}

实例代码

<template>
  <div class="tab-container">
    <el-table
      :data="pvData.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        prop="userName"
        label="用户名"
        width="180"
      ></el-table-column>

      <el-table-column prop="realName" label="姓名"></el-table-column>
      <el-table-column prop="sex" label="性别"  :formatter="formatSex"></el-table-column>
      <el-table-column prop="organName" label="所属部门"></el-table-column>
      <el-table-column prop="authority" label="权限"></el-table-column>
      <el-table-column prop="roleName" label="角色"></el-table-column>
      
    </el-table>

    <el-pagination
      style="margin: 12px 0px"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pvData.length"
    >
    </el-pagination>
  

  </div>
</template>
<script>
//调用接口
import {getQuerycheckList} from "@/api/permission/user";

export default {
  data() {
    return {
      // 分页
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      total: 0,
      pvData: [],
      
    };
  },

  created() {
    this.getQuerycheckList();
  },
  methods: {
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },

    //查询题目列表信息接口
    getQuerycheckList() {
      const params = {
        organId: 1,
        userOrganId: 1,
        authority: 0,
        page: 1,
        rows: 5,
        isPagination: false,
      };
      getQuerycheckList(params).then((res) => {
        console.log("查询题目列表信息", res);
        this.pvData = res.data;
      });
    },
    //格式化性别
    formatSex(row, column) {
    return row.sex === 1? "男" : "女";
    }
  },
};
</script>
<style scoped>
.tab-container {
  margin: 30px;
}
</style>

效果:

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Ant Design Vue 表格组件提供了分页功能,但默认情况下只显示一页数据。如果你想要实现滚动分页,可以通过以下步骤来实现: 1. 在表格组件中添加 `scroll` 属性来启用滚动条。 2. 将 `pagination` 属性设置为 `false`,以禁用默认的分页。 3. 使用 `@scroll` 事件监听表格滚动事件。 4. 在滚动事件处理函数中,根据滚动条位置和表格高度计算出当前页码。 5. 根据当前页码,使用 `dataSource` 属性来更新表格数据。 具体实现可参考以下代码示例: ```html <template> <a-table :columns="columns" :dataSource="dataSource" :scroll="{ y: 400 }" :pagination="false" @scroll="handleScroll"> <template slot="name" slot-scope="text">{{ text }}</template> </a-table> </template> <script> import { Table } from 'ant-design-vue'; export default { components: { 'a-table': Table, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name', width: 150, }, { title: 'Age', dataIndex: 'age', key: 'age', width: 70, }, { title: 'Address', dataIndex: 'address', key: 'address', width: 200, }, ], dataSource: [], currentPage: 1, }; }, mounted() { this.loadData(); }, methods: { loadData() { // 根据当前页码加载数据 const pageSize = 10; const start = (this.currentPage - 1) * pageSize; const end = start + pageSize; const data = []; for (let i = start; i < end; i++) { data.push({ key: i, name: `User ${i}`, age: Math.floor(Math.random() * 30) + 20, address: `Street ${i}`, }); } this.dataSource = data; }, handleScroll(e) { const tableBody = e.target.querySelector('.ant-table-body'); // 计算当前页码 const currentPage = Math.floor(tableBody.scrollTop / tableBody.clientHeight) + 1; if (currentPage !== this.currentPage) { this.currentPage = currentPage; this.loadData(); } }, }, }; </script> ``` 在以上代码中,我们设置表格高度为 400px,并禁用了默认的分页。当表格滚动时,会触发 `handleScroll` 方法来更新当前页码并加载数据。注意,为了方便起见,这里的数据是随机生成的,实际应用中需要根据实际情况来获取数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值