使用vue的分页实现自定义分页、多个slot的处理方案

由于vue的标签el-pagination当中的layout只能存在一个自定义slot,所以当需要自定义多个slot时,就考虑用多个el-pagination拼接到一起,比如要实现如下需求
类似需求样式
从组件库中的分页改造在这里插入图片描述
其中当前第几页共几页 、 首页(可点击)、尾页(可点击)三个地方是自定义的slot,所以拆成三个独立的el-pagination,layout分别为(total,slot,sizes)(slot)(prev, pager, next, slot, jumper)对应下图红色方框的三个部分在这里插入图片描述

由于系统多处使用分页所以此处将他抽取为组件以供方便使用。组件完整代码如下

<template>
  <div class="app-container calendar-list-container">
   <div class="pagination-container" style="float: left;margin-bottom: 30px">
      <el-pagination background @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-sizes="pageSizes" :page-size="limit"
                     layout="total, slot, sizes" :total="total" style="float: left;">
        <span>{{'当前第' + page + '页' + ',   ' + '共' + this.lastPage + '页'}}</span>
      </el-pagination>
      <el-pagination background @size-change="handleSizeChange" :firstPage="firstPage"
                     @current-change="handleCurrentChange"
                     :page-sizes="[20,100,300, 500]" :page-size="limit"
                     layout="slot" :total="total" style="float: left;">
        <el-button
          :disabled="page === firstPage"
          class="first-pager"
          @click="toFirstPage"
        >首页</el-button>
      </el-pagination>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="page" :page-sizes="pageSizes"
                     :page-size="limit" :lastPage="lastPage"
                     layout="prev, pager, next, slot, jumper" :total="total"style="float: left;margin-left: -10px;">
        <el-button
          :disabled="page === lastPage"
          class="first-pager"
          @click="toLastPage"
        >尾页</el-button>
      </el-pagination>
      <el-button circle @click="handleGo">{{'go'}}</el-button>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'pagination',
    props: { // 父页面传值
      page: {
        type: Number
      },
      limit: {
        type: Number
      },
      total: {
        type: Number
      },
      pageSizes: {
        type: String
      }
    },
    data() {
      return {
        firstPage: 1,
        lastPage: 1
      }
    },
    created() {
    },
    computed: {
      lastPage: function() { return Math.ceil(this.total / this.limit) }
    },
    methods: {
      // 首页
      toFirstPage() {
        this.$emit('handleChildGetList', this.firstPage, this.limit)
      },
      // 尾页
      toLastPage() {
        this.$emit('handleChildGetList', this.lastPage, this.limit)
      },
      // 响应数据列表选择页面显示记录数事件
      handleSizeChange(val) {
        this.$emit('handleChildGetList', 1, val)
      },
      // 响应数据列表选择页码事件
      handleCurrentChange(val) {
        this.$emit('handleChildGetList', val, this.limit)
      },
      // 响应go事件
      handleGo() {
        this.$emit('handleChildGetList', this.page, this.limit)
      }
    }
  }
</script>

要调用时需要传(page、limit、total、pageSizes)给子组件,从子组件传回handleChildGetList

    <pagination :page="antiWaterListQuery.page" :limit="antiWaterListQuery.limit" :total="total"
                :pageSizes="[20,100,300, 500]" v-on:handleChildGetList="handleParentGetList">	</pagination>
 // 引入
    import pagination from '../common/components/pagination'
  components: {
    pagination
  },
      // 响应分页组件查询事件
    handleParentGetList(page, limit) {
      this.antiWaterListQuery.page = page
      this.antiWaterListQuery.limit = limit
      // 调用查询方法
      this.getConfigList()
    },

调用主要是这几处内容

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-awesome-swiper中,要自定义分页器,可以使用pagination组件来进行自定义。以下是一个简单的示例: ```vue <template> <div> <swiper :options="swiperOptions"> <swiper-slide v-for="(slide, index) in slides" :key="index"> <!-- 内容 --> </swiper-slide> <!-- 自定义分页器 --> <div class="custom-pagination" slot="pagination"> <span v-for="(slide, index) in slides" :key="index" :class="{ active: index === activeIndex }" @click="goToSlide(index)" > {{ index + 1 }} </span> </div> </swiper> </div> </template> <script> import { swiper, swiperSlide } from "vue-awesome-swiper"; export default { components: { swiper, swiperSlide, }, data() { return { swiperOptions: { // 设置其他选项 }, slides: [ // 设置轮播项数据 ], activeIndex: 0, }; }, methods: { goToSlide(index) { this.activeIndex = index; }, }, }; </script> <style> .custom-pagination { /* 样式自定义 */ } .custom-pagination span { /* 样式自定义 */ } .custom-pagination span.active { /* 样式自定义 */ } </style> ``` 在这个示例中,我们首先导入`vue-awesome-swiper`的`swiper`和`swiperSlide`组件,然后在模板中使用`swiper`组件包裹轮播项,并设置`options`属性来配置其他选项。 接下来,在`swiper`组件内部,我们使用`slot`属性将自定义分页器的内容放在名为"pagination"的插槽中。我们使用一个`v-for`循环来渲染分页器中的每个页码,并在点击时调用`goToSlide`方法来切换轮播项。 最后,我们可以通过样式来自定义分页器的外观,通过修改`.custom-pagination`和`.custom-pagination span`的样式来实现自定义效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值