配合element-ui的分页和表格控件实现本地数据分页效果

也不多说了直接上代码

<template>
  <div>
    <div>表格本地分页</div>
    <el-table :data="tableData" border>
      <el-table-column prop="id" label="序号"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="index" :page-sizes="[2, 4, 6, 8,10]" :page-size="size" layout="total, sizes, prev, pager, next, jumper" :total="tableCopyTableList.length">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: "start", age: 18 },
        { id: 2, name: "Rory", age: 18 },
        { id: 3, name: "Rosa", age: 18 },
        { id: 4, name: "Rosalia", age: 18 },
        { id: 5, name: "Rosalind", age: 18 },
        { id: 6, name: "Karin", age: 18 },
        { id: 7, name: "Kate", age: 18 },
        { id: 8, name: "Roseanne", age: 18 },
        { id: 9, name: "Rosemarie", age: 18 },
        { id: 10, name: "Rosemary", age: 18 },
        { id: 11, name: "Rosetta", age: 18 },
        { id: 12, name: "Arden", age: 18 },
        { id: 13, name: "Arendt", age: 18 },
        { id: 14, name: "Ares", age: 18 },
        { id: 15, name: "Penn", age: 18 },
        { id: 16, name: "Penney", age: 18 },
        { id: 17, name: "Pennie", age: 18 },
        { id: 18, name: "Pennington", age: 18 },
        { id: 19, name: "Bobette", age: 18 },
        { id: 20, name: "end", age: 18 }
      ],
      tableCopyTableList: [],
      index: 1,
      size: 10
    };
  },
  methods: {
    // 页数改变事件
    handleSizeChange(size) {
      this.size = size;
      this.tableData = this.paging(size, this.index);
    },
    // 页码改变事件
    handleCurrentChange(current) {
      this.index = current;
      this.tableData = this.paging(this.size, current);
    },
    // 本地分页的方法
    paging(size, current) {
      const tableList = JSON.parse(JSON.stringify(this.tableCopyTableList));
      const tablePush = [];
      tableList.forEach((item, index) => {
        if (size * (current - 1) <= index && index <= size * current - 1) {
          tablePush.push(item);
        }
      });
      return tablePush;
    }
  },
  created() {
    // 初始化数据
    this.tableCopyTableList = JSON.parse(JSON.stringify(this.tableData));
    this.tableData = this.paging(this.size, this.index);
  }
};
</script>

<style lang="scss" scoped>
</style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,038评论 3 119
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,132评论 25 709
  • 可莱丝卡通line系列面膜 现货哦,风靡韩国街头巷尾的一款面膜 蓝色情侣熊兔:加强版·补水保湿 黄色布朗熊:细致毛...
    张柠阅读 2,900评论 0 0
  • 鱼鱼在大叔手背上舔了一小口,砸吧嘴。 大叔:“咸不咸?” 鱼鱼:“闲?不闲。” 大叔:“你是不是傻?我刚跑完步。”...
    鱼墨9898阅读 1,111评论 0 0
  • 2013年11月5日,北京,阴天,轻度雾霾,我,28岁了。 除了父母和一个巴掌就能数过来的几个朋友发来生日祝福,手...
    离碎阅读 2,662评论 0 2