Vue分页组件实现过程详解

yizhihongxing

下面是详细讲解“Vue分页组件实现过程详解”的完整攻略。

1. 分页组件概述

分页组件是网页中常用的组件之一。它用于在一页内容过多的情况下,将内容分为多页展示,便于用户浏览和操作。Vue.js在处理分页组件时,提供了一种简单且高度可定制的方式。

2. 分页组件实现的基本步骤

2.1 设计组件结构:包括需求分析,页面布局设计,功能需求设计。

2.2 实现组件结构:分别实现分页栏,页码等子组件。

2.3 完善功能:组件常见功能包括点击页码跳转到指定页面,改变每页显示的数据条数,更新分页状态等。

3.分页组件示例1

以下示例中,我们将实现一个简单的分页组件,该组件可以根据用户点击页码或点击“下一页”和“上一页”按钮,让用户浏览大量数据。

3.1 实现组件结构

我们先在Vue中定义一个名为page的组件。在组件中,我们使用props传递需要渲染的数据和每页显示的数据条数:

<template>
  <div>
    <!-- 待渲染数据 -->
    <ul>
      <li v-for="item in data">{{ item }}</li>
    </ul>

    <!-- 分页效果 -->
    <div>
      <button :disabled="currentPage === 1" @click="gotoPage(currentPage - 1)">上一页</button>
      <span v-for="index in pages" :key="index" @click="gotoPage(index)" :class="{'active': currentPage === index}">
        {{ index }}
      </span>
      <button :disabled="currentPage === totalPages" @click="gotoPage(currentPage + 1)">下一页</button>

      <select v-model="pageSize" @change="changePageSize">
        <option v-for="size in pageSizes">{{ size }}条/页</option>
      </select>
      <span>{{ currentPage }} / {{ totalPages }}页</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'page',
  props: {
    data: {
      type: Array,
      default: []
    },
    count: {
      type: Number,
      default: 0
    },
    pageSize: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default: () => [10, 20, 30, 40]
    }
  },
  data() {
    return {
      currentPage: 1
    }
  },
  computed: {
    // the total number of pages
    totalPages() {
      return Math.ceil(this.count / this.pageSize)
    },
    // the page list to display in the pagination bar
    pages() {
      const pageCount = 7
      const currentPage = this.currentPage
      const totalPages = this.totalPages
      const half = Math.floor(pageCount / 2)

      let start = Math.max(currentPage - half, 1)
      let end = Math.min(start + pageCount - 1, totalPages)

      if (end - start < pageCount) {
        start = end - pageCount + 1
      }

      const pagesList = []
      for (let i = start; i <= end; i++) {
        pagesList.push(i)
      }

      return pagesList
    }
  },
  methods: {
    gotoPage(pageNumber) {
      if (pageNumber < 1) {
        this.currentPage = 1
      } else if (pageNumber > this.totalPages) {
        this.currentPage = this.totalPages
      } else {
        this.currentPage = pageNumber
      }
      // emit out the page change event
      this.$emit('page-changed', { page: this.currentPage, size: this.pageSize })
    },
    changePageSize(event) {
      const newSize = parseInt(event.target.value, 10)
      if (this.pageSize !== newSize) {
        this.currentPage = 1
        this.$emit('page-changed', { page: this.currentPage, size: newSize })
      }
    }
  }
}
</script>

3.2 完善功能

以上代码中的功能实现时针对示例1所实现的分页组件,包括基本的页码、上一页、下一页、数据条数的显示和控制,用户可以根据自己的需求进行二次开发。

4. 分页组件示例2

以下示例中,我们将实现一个复杂一些的分页组件,该组件可以根据用户输入的搜索条件,从服务器请求数据,并可以支持跳转到指定的页码。

4.1 实现组件结构

我们同样先在Vue中定义一个名为page的组件,但与上一个示例不同的是,该组件的数据来源于服务器端,我们需要通过ajax等方式从服务器获取数据。

<template>
  <div>
    <input type="text" v-model="searchText" placeholder="请输入搜索关键词" @keyup.enter="search"/>
    <button @click="search">搜索</button>

    <table>
      <thead>
        <tr>
          <th>商品名称</th>
          <th>商品价格</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in list" :key="item.id">
          <td>{{ item.name }}</td>
          <td>{{ item.price }}</td>
        </tr>
      </tbody>
    </table>

    <!-- 分页效果 -->
    <div>
      <button :disabled="currentPage === 1" @click="gotoPage(currentPage - 1)">上一页</button>
      <span v-for="index in pages" :key="index" @click="gotoPage(index)" :class="{'active': currentPage === index}">
        {{ index }}
      </span>
      <button :disabled="currentPage === totalPages" @click="gotoPage(currentPage + 1)">下一页</button>
      <input type="text" v-model="jumpPage" placeholder="跳转到第几页" @keyup.enter="gotoPage(jumpPage)">

      <span>{{ currentPage }} / {{ totalPages }}页</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'page',
  data() {
    return {
      list: [],
      count: 0,
      currentPage: 1,
      pageSize: 10,
      keywords: '',
      jumpPage: ''
    }
  },
  computed: {
    // the total number of pages
    totalPages() {
      return Math.ceil(this.count / this.pageSize)
    },
    // the page list to display in the pagination bar
    pages() {
      if (this.totalPages <= 9) {
        return Array.from({ length: this.totalPages }, (_, index) => index + 1)
      } else if (this.currentPage <= 5) {
        return Array.from({ length: 6 }, (_, index) => index + 1).concat(['...'], [this.totalPages])
      } else if (this.currentPage + 4 >= this.totalPages) {
        return [1, '...', ...Array.from({ length: 6 }, (_, index) => this.totalPages - 5 + index)]
      } else {
        return [
          1,
          '...', 
          ...Array.from({ length: 5 }, (_, index) => this.currentPage - 2 + index),
          '...',
          this.totalPages
        ]
      }
    }
  },
  methods: {
    search() {
      this.getList()
    },
    getList() {
      //ajax调用 获取数据
    },
    gotoPage(pageNumber) {
      if (!pageNumber || pageNumber < 1) {
        this.currentPage = 1
      } else if (pageNumber > this.totalPages) {
        this.currentPage = this.totalPages
      } else {
        this.currentPage = pageNumber
      }

      this.getList()
    },
  }
}
</script>

4.2 完善功能

通过上述代码实现了使用复杂搜索条件获取服务器端数据,并实现跳转到指定页码的功能。但该示例中页面渲染、分页组件的表现形式可以根据自己的需求进行二次开发。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Vue分页组件实现过程详解 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • 解决idea中debug工具栏消失后如何显示的问题

    当我们使用IntelliJ IDEA进行调试时,有时会遇到调试工具栏消失的问题,这样我们就无法查看和控制调试过程中的变量值、控制程序运行等操作。在这里,我将介绍一些方法来解决debug工具栏消失的问题。 方法一:检查工具栏是否被隐藏 有时候,我们可能会意外地将debug工具栏隐藏起来了,所以第一步是确认一下工具栏是否被隐藏。你可以使用以下步骤来检查: 点击”…

    Vue 2023年5月28日
    00
  • vue监听对象及对象属性问题

    以下是关于“Vue监听对象及对象属性问题”的完整攻略。 问题背景 在使用Vue时,我们经常需要监听对象的变化并在响应时更新相应的视图。Vue提供了一些方法供我们监听对象及其属性的变化。 监听对象 可以使用$watch方法监听一个对象的变化,基本语法为: vm.$watch(‘对象名’,callback(newVal,oldVal){ //处理逻辑 }) 其中…

    Vue 2023年5月28日
    00
  • vue 动态样式绑定 class/style的写法小结

    那我来详细讲解“Vue 动态样式绑定 class/style 的写法小结”。 1. 绑定 class Vue.js 提供了一种叫做:class的指令,可以通过数据绑定的方式动态地设置一个 HTML 元素的类名。语法为: <div :class="{ className: condition }"></div> 其中…

    Vue 2023年5月27日
    00
  • vue.js的状态管理vuex中store的使用详解

    Vue.js的状态管理:Vuex中store的使用详解 在大型Vue.js应用程序中,管理组件之间的状态可能会变得异常困难。为了解决这个问题,Vue.js提供了一个专门的状态管理库——Vuex。 在Vuex中,store是状态集合,包含了你的应用程序中所有组件的状态。store使用一个单一的数据源来管理组件之间的通讯,其中的数据可以在整个应用程序中被访问和修…

    Vue 2023年5月27日
    00
  • Vue+Element UI+vue-quill-editor富文本编辑器及插入图片自定义

    下面给你详细讲解一下怎样使用Vue、Element UI和vue-quill-editor富文本编辑器实现插入图片自定义功能。 环境与依赖 首先,我们需要创建一个Vue项目,在命令行中输入以下命令: vue create my-project 然后,进入到项目目录下,安装以下依赖: npm install element-ui vue-quill-edito…

    Vue 2023年5月28日
    00
  • Vue3 中路由Vue Router 的使用实例详解

    Vue3 中路由Vue Router 的使用实例详解 介绍 Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,可以很方便的用于构建单页应用程序。Vue Router 2.x 版本为 Vue 2.x 版本提供路由功能,而 Vue Router 3.x 版本为 Vue 3.x 版本提供路由功能,并在性能和体积方面得到了…

    Vue 2023年5月28日
    00
  • vue.js移动数组位置,同时更新视图的方法

    要移动 Vue 中的数组位置并更新视图,常用的方法是: 使用 splice 方法直接修改数组,再触发更新操作; 使用 Vue.set 或 this.$set 方法,更新数组指定索引值的数据并触发更新操作。 下面分别介绍这两种方法的使用。 1. 使用 splice 方法直接修改数组 最常见的移动数组位置操作就是将某个元素往前或往后移动,以 arr[i] 为例,…

    Vue 2023年5月29日
    00
  • VUE的tab页面切换的四种方法

    VUE 是一款流行的 JavaScript 前端框架,用于构建交互式用户界面。在 VUE 中,tab 页面切换是常见的功能,以下是 VUE 中实现 tab 切换的四种方法的完整攻略。 1. 标准的 VUE 动态组件方法 我们可以使用标准的 VUE 动态组件方法,在模板中设置一个变量,然后根据变量的值确定要显示的组件。这种方法需要我们在 VUE 组件中定义所有…

    Vue 2023年5月27日
    00
合作推广
合作推广
分享本页
返回顶部