首页 > 其他分享 >封装分页组件

封装分页组件

时间:2022-09-21 16:48:10浏览次数:55  
标签:pagination 封装 分页 val type 组件 total true page

效果图

 

 

 主要框架:vue2+element

一:pagination组件代码

<template>
  <!-- 原理:
  分页中有三个地方需要使用插槽(首页,末页,确定),一个分页模块中只能使用一个插槽,所以将分页分成三段 -->
  <!-- 第一段:【total,slot(首页)】  -->
  <!-- 第二段:【prev, pager, next, slot(末页), sizes, jumper】 -->
  <!-- 第三段:【slot(确定)】 -->
  <div class="containerPag" :class="{ hidden: hidden }">
    <el-pagination
      ref="pagination"
      class="ahead-page"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      layout="total,slot"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
      <button
        class="firstPage"
        :disabled="firstPageDisabled"
        @click="toFirstPage"
      >
        首页
      </button>
    </el-pagination>
    <el-pagination
      ref="pagination"
      class="rear-page"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      layout="prev, pager, next, slot, sizes, jumper "
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
      <button class="lastPage" :disabled="lastPageDisabled" @click="toLastPage">
        末页
      </button>
    </el-pagination>
    <el-pagination
      ref="pagination"
      class="ahead-page"
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      layout="slot"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
      <el-button
        type="primary"
        size="mini"
        class="btndif"
        @click="handleCurrentChange"
        >确定</el-button
      >
    </el-pagination>
  </div>
</template>

<script>
import { scrollTo } from "@/utils/scroll-to";

export default {
  name: "Pagination",
  props: {
    total: {
      required: true,
      type: Number,
    },
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 20,
    },
    pageSizes: {
      type: Array,
      default() {
        return [2, 20, 30, 50];
      },
    },
    layout: {
      type: String,
      default: "total, sizes, prev, pager, next, jumper",
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      firstPageDisabled: false, //  首页
      lastPageDisabled: true, //  末页
      childTotal: "",
    };
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    pageSize: {
      get() {
        return this.limit;
      },
      set(val) {
        this.$emit("update:limit", val);
      },
    },
  },
  watch: {
    // 注意一:
    // 子组件中的数据通过props来接受,
    // 子组件的methods中想要取到props中的值,直接使用this.total即可
    // 你的total里面的值并不是固定的,而是动态获取的,
    // 这种情况下,你会发现methods中是取不到你的total的,或者取到的一直是默认值
    // 解决办法用watch解决(获取不到total,刚进页面【末页】按钮是点击不了的)
    total: {
      handler(newval) {
        this.childTotal = newval;
      },
      immediate: true,
      deep: true,
    },
    // 分页 计算首页和末页
    currentPage: {
      handler(newVal) {
        setTimeout((_) => {
          let pages = Math.ceil(this.childTotal / this.pageSize);
          if (pages === 0) {
            // 数据(totalResult)为0
            this.firstPageDisabled = true; // 首页按钮是否禁用
            this.lastPageDisabled = true; // 末页按钮是否禁用
          } else {
            this.firstPageDisabled = this.currentPage === 1;
            this.lastPageDisabled = this.currentPage === pages;
          }
        }, 500);
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("pagination", { page: this.currentPage, limit: val });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { page: val, limit: this.pageSize });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    //   前往首页
    toFirstPage() {
      this.$refs.pagination.handleCurrentChange(1);
      this.$emit("handleCurrentChange", 1);
    },
    //  前往末页
    toLastPage() {
      // 注意二:
      // 因为组件中的this指向组件实例,
      // this.$refs.pagination指向要操作的元素
      let font = this.$refs.pagination;
      let cpage = Math.ceil(font.total / font.pageSize);
      font.handleCurrentChange(cpage);
    },
  },
};
</script>

<style scoped>
.containerPag {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  justify-content: end;
}
.el-pagination {
  float: left;
  margin-top: 10px;
}
.el-pagination.ahead-page {
  padding-right: 0px;
}
.el-pagination.rear-page {
  padding-left: 0px;
}
.firstPage,
.lastPage {
  background-color: white;
  cursor: pointer;
}
.totalData {
  margin-left: 24px;
  font-weight: normal;
  color: #303133;
  display: inline-block;
  font-size: 13px;
  min-width: 35.5px;
  height: 28px;
  line-height: 28px;
  vertical-align: top;
  box-sizing: border-box;
}
.el-pagination .btndif {
  background: #1890ff;
}
.el-pagination .btndif:hover {
  color: #fff;
}
::v-deep .el-pagination__jump {
  margin-left: 0px;
}
</style>

二:引入全局

在main.js中导入,可全局使用

import Pagination from "@/components/Pagination";

三:使用(页面中直接使用)

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

 

标签:pagination,封装,分页,val,type,组件,total,true,page
From: https://www.cnblogs.com/guohanting/p/16716115.html

相关文章

  • JS 动态获取 Url 参数(封装函数)
     话不多说直接上代码:封装函数如下:(如果urls固定,可以只写一个name变量)getQueryString(name,urls){varurl=urls;//获取URL......
  • MySQL学习——分页查询
    在Orcale中我们也介绍过了使用内建视图和rownum来实现分页的操作(Oracle学习——视图、序列、索引),而MySQL有更加方便的实现分页查询的操作。1、LIMIT语法格式SELECT......
  • react hooks组件父组件调用子组件方法
    函数组件父组件调用子组件方法需要使用useImperativeHandle和forwardRef两个方法1.子组件    2.父组件 注意:一定要使用ref来接从子组件传过来的实例值,用......
  • 本周四晚19:00知识赋能第八期第2课丨ArkUI自定义组件
     9月21日19:00~20:00,第八期知识赋能第2节直播就要开始啦!本次直播将为同学们带来涂鸦小游戏的趣味体验,让大家全面了解ArkUI框架的应用,帮助你们在自己已有专业的基础上拓宽知......
  • vue富文本(5版本)组件
    <template><div><divstyle="border:1pxsolid#ccc;width:500px"><!--工具栏--><Toolbarstyle="border-bottom:1pxsolid#ccc":editor="......
  • 请求封装
    //constapp=getApp()letisDev=true;//是否开发环境lethost='https://xs2b.kktijian.com/api'letfileHost='https://xs2b.kktijian.com'letversions=__wxC......
  • React 面向组件编程 之 函数式组件
    简单组件和复杂组件的概念如果组件有state(状态)属性,就是复杂组件。如果没有state(状态)属性,就是简单组件。state、props、refs是组件实例的三大核心属性,在之后会逐一进......
  • 即用型UI组件Kendo UI,助力惠普缩短40%的应用开发时长
    惠普解决方案架构师BenjaminThatcher:"我们HP有自己的设备策略,内部使用的应用程序需要支持几乎所有主要的移动操作系统,包括WindowsPhone、Android、iOS系统及这些平台的......
  • 小程序自定义组件
    1.创建自定义组件一个组件也类似于一个页面,由json wxml wxss js 4个文件组成可以在一个新文件夹上右键新建component,可以直接生成这四个文件其次在json配置文件中......
  • 来看界面组件DevExpress WinForm是如何实现地图搜索的
    DevExpressWinForm拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......