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

本文介绍了一种在Vue中利用el-pagination组件实现自定义分页布局的方法,通过组合多个el-pagination组件来满足复杂的需求,如首页、尾页、当前页等自定义显示,并提供了完整的组件代码及调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于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()
    },

调用主要是这几处内容

### 关于 Ant-Vue自定义分页组件 在 Ant-Design-Vue 中,可以通过配置 `pagination` 属性来自定义分页行为。如果需要更复杂的自定义逻辑,则可以完全控制分页器的行为并将其与表格或其他组件集成。 #### 使用内置属性进行简单自定义 对于简单的场景,可以直接修改 `Table` 或其他支持分页组件中的 `pagination` 配置项来调整显示效果和交互方式: ```javascript const columns = [ { title: 'Name', dataIndex: 'name' }, // ... other column definitions ... ]; const data = []; // Your table data here. <template> <a-table :columns="columns" :data-source="data" :pagination="{ pageSize: 5, showSizeChanger: true }"> </a-table> </template> ``` 这段代码展示了如何设置每页条目数以及启用大小更改选项[^3]。 #### 完全自定义分页器 当需求超出默认提供的功能时,可以选择禁用内建的分页控件并通过插槽插入自己的分页实现: ```html <a-table :columns="columns" :data-source="dataSource" :pagination="false"> <!-- Custom pagination slot --> <div slot="footer" class="custom-pagination"> <a-pagination :total="totalCount" :current.sync="currentPage" @change="handlePageChange"/> </div> </a-table> <script> export default { data() { return { currentPage: 1, totalCount: 0, dataSource: [] }; }, methods: { handlePageChange(pageNumber) { this.currentPage = pageNumber; // Fetch new page of items based on the selected page number. fetchDataBasedOnCurrentPage(); } } }; </script> ``` 在这个例子中,通过将 `pagination` 设置为 `false` 来关闭默认分页栏,并利用 `<slot>` 插入了一个新的分页区域,在其中放置了 `APagination` 组件以处理翻页事件[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值