vue实现前端分页完整代码

yizhihongxing

下面是“Vue实现前端分页完整代码”的详细讲解攻略,包括代码示例。

什么是前端分页

前端分页是指在浏览器端进行数据分页处理,采用JavaScript实现。该技术可以减轻服务器的负担,提高网站性能,给用户带来更流畅、更友好的交互体验。

基于Vue的前端分页实现

Vue是一款流行的JavaScript框架,为前端开发提供了快速、简便的构建SPA(单页应用)的方式。下面是基于Vue实现前端分页的完整代码示例:

<div id="app">
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in paginatedData" :key="index">
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
  <div>
    <button :disabled="currentPage === 1" @click="currentPage--">上一页</button>
    <span>{{ currentPage }}</span>
    <button :disabled="currentPage === totalPages" @click="currentPage++">下一页</button>
  </div>
</div>
new Vue({
  el: '#app',
  data: {
    data: [
      { id: 1, name: 'Alice', age: 18 },
      { id: 2, name: 'Bob', age: 20 },
      { id: 3, name: 'Charlie', age: 25 },
      { id: 4, name: 'David', age: 30 },
      { id: 5, name: 'Eva', age: 35 },
      { id: 6, name: 'Frank', age: 40 },
      { id: 7, name: 'Gina', age: 45 },
      { id: 8, name: 'Harry', age: 50 }
    ],
    itemsPerPage: 3, // 每页显示的数据量
    currentPage: 1 // 当前页码
  },
  computed: {
    // 获取当前页应该显示的数据
    paginatedData () {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.data.slice(startIndex, endIndex);
    },
    // 获取总页数
    totalPages () {
      return Math.ceil(this.data.length / this.itemsPerPage);
    }
  }
})

上述代码使用Vue框架实现了前端分页功能。其中,data数组存储了原始数据,itemsPerPage定义了每页显示的数据量,currentPage表示当前页码。通过计算属性paginatedData获取当前页应该显示的数据,通过计算属性totalPages获取总页数。

页面中,使用v-for指令遍历每一页数据,currentPagetotalPages控制页面呈现的页码。点击“上一页”和“下一页”按钮,可以切换到前一页和后一页数据。

示例说明

示例1:基于axios异步获取数据

new Vue({
  el: '#app',
  data () {
    return {
      data: [],
      itemsPerPage: 10,
      currentPage: 1
    }
  },
  mounted () {
    axios.get('/api/data')
      .then(response => {
        this.data = response.data;
      })
      .catch(error => {
        console.log(error);
      });
  },
  computed: {
    paginatedData () {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.data.slice(startIndex, endIndex);
    },
    totalPages () {
      return Math.ceil(this.data.length / this.itemsPerPage);
    }
  }
})

该示例中,使用axios库异步获取数据,然后实现分页展示。其中,mounted生命周期函数在组件挂载时触发,异步获取数据后将其保存到data中。由于数据通常很大,可能需要分页展示,页面中的其它部分可以按照前端分页的方式显示数据。

示例2:通过搜索实现过滤

<div id="app">
  <input type="text" v-model="searchKeyword" placeholder="Search...">
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in paginatedData" :key="index">
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
  <div>
    <button :disabled="currentPage === 1" @click="currentPage--">上一页</button>
    <span>{{ currentPage }}</span>
    <button :disabled="currentPage === totalPages" @click="currentPage++">下一页</button>
  </div>
</div>
new Vue({
  el: '#app',
  data () {
    return {
      data: [
        { id: 1, name: 'Alice', age: 18 },
        { id: 2, name: 'Bob', age: 20 },
        { id: 3, name: 'Charlie', age: 25 },
        { id: 4, name: 'David', age: 30 },
        { id: 5, name: 'Eva', age: 35 },
        { id: 6, name: 'Frank', age: 40 },
        { id: 7, name: 'Gina', age: 45 },
        { id: 8, name: 'Harry', age: 50 }
      ],
      itemsPerPage: 3,
      currentPage: 1,
      searchKeyword: ''
    }
  },
  computed: {
    // 获取满足搜索条件的数据
    filteredData () {
      const result = this.data.filter(item => {
        return item.name.indexOf(this.searchKeyword) !== -1 ||
          item.age.toString().indexOf(this.searchKeyword) !== -1;
      })
      return result;
    },
    // 获取当前页应该显示的数据
    paginatedData () {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.filteredData.slice(startIndex, endIndex);
    },
    // 获取总页数
    totalPages () {
      return Math.ceil(this.filteredData.length / this.itemsPerPage);
    }
  }
})

该示例实现了一个搜索框,可以按照姓名和年龄进行搜索,然后按照前端分页的方式展示搜索结果。通过计算属性filteredData过滤不满足搜索条件的数据,然后在paginatedData中进行分页展示。

以上就是基于Vue实现前端分页完整代码的攻略,希望对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue实现前端分页完整代码 - Python技术站

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

相关文章

  • Django之无名分组和有名分组的实现

    Django之无名分组和有名分组的实现 在Django的url路由中,我们可以通过使用正则表达式来匹配不同的url地址,并且通过分组的方式将匹配到的信息提取出来,这就是Django的分组功能,分组的方式可以分为无名分组和有名分组。 无名分组 无名分组即为不特别指定分组名称的分组方式,使用()来进行分组,$1、$2等都是分组的引用,这种引用方式不直观,难以辨别…

    人工智能概论 2023年5月25日
    00
  • python 实现文件的递归拷贝实现代码

    下面是详细讲解如何使用 Python 实现文件递归拷贝的攻略: 1. 确定拷贝源和目标 在开始编写拷贝代码之前,首先需要明确需要拷贝哪些文件,以及拷贝到哪个目标路径。可以使用 Python 的 os 模块 来获取文件列表,并使用 shutil 模块 来完成文件拷贝的操作。具体代码如下: import os import shutil src_path = ‘…

    人工智能概论 2023年5月25日
    00
  • 云原生技术持久化存储PV与PVC

    当今云计算领域中,云原生技术已经成为了业界的一个热门话题。云原生技术的一个核心特点就是它能够对应用进行拆分,将应用在各个层面上进行最大化的优化,从而达到整个应用的高效运行。其中,持久化存储就是云原生架构下的一个重要话题,今天我们就来详细讲解一下云原生技术中持久化存储的相关知识。 1. 什么是PV和PVC 在云原生技术中,PV是指持久卷(Persistent …

    人工智能概览 2023年5月25日
    00
  • 解析Tars-Java客户端源码

    解析Tars-Java客户端源码的完整攻略 Tars-Java客户端是基于Tars框架的Java版本实现的一种提供远程服务的客户端。在理解Tars-Java客户端源码时,我们可以从以下几个方面入手: 1. 主要依赖的引入 在使用Tars-Java客户端时,我们需要在pom.xml文件中引入以下依赖: <dependency> <groupI…

    人工智能概览 2023年5月25日
    00
  • Android开发图片水平旋转180度方法

    当需要在Android应用程序中进行图片操作时,图片的旋转可能是一个常用的操作。如果需要将图片旋转180度,可以使用以下步骤: 读取图片文件:首先需要读取需要旋转的图片文件。 Bitmap bmp = BitmapFactory.decodeFile(“/sdcard/image.jpg”); 创建Matrix对象:创建一个新的Matrix对象,用于执行图像…

    人工智能概览 2023年5月25日
    00
  • pytorch加载自己的数据集源码分享

    下面是关于pytorch加载自己的数据集的完整攻略。 1. 准备数据集 在使用pytorch训练模型需要一个自己的数据集,这里以图像分类任务为例,准备一个包含训练集和测试集的数据集,其中每个图像都分好了类别并放在对应的文件夹中,例如: dataset ├── train │ ├── cat │ │ ├── cat1.jpg │ │ ├── cat2.jpg …

    人工智能概论 2023年5月25日
    00
  • 常用的Spring Boot调用外部接口方式实现数据交互

    Spring Boot是一款十分流行的Java框架,使用Spring Boot开发应用程序常遇到的问题之一就是需要调用外部接口实现数据交互。本篇文章将详细讲解常用的Spring Boot调用外部接口方式实现数据交互的完整攻略,主要包括以下几点。 1. 实现数据交互的方式 在前期规划时,我们需要明确如何实现数据交互。通常有以下几种方式。 RestTemplat…

    人工智能概览 2023年5月25日
    00
  • OpenCV实现图像腐蚀

    让我们来详细讲解一下“OpenCV实现图像腐蚀”的完整攻略。 什么是图像腐蚀? 图像腐蚀是一种基本图像处理操作,它可以去除图像中小的不连续三角形、孤点等噪声,同时也可以缩小物体边界。它是一种由于对象形态在变化的过程中对象的边界产生的变化,与平滑操作(如图像模糊化)相反。在数字图像处理中,腐蚀操作是一种基本的形态学处理操作,可以用来消除图像中的小的独立的物体。…

    人工智能概论 2023年5月24日
    00
合作推广
合作推广
分享本页
返回顶部