当前位置:首页 > VUE

vue搜索过后实现分页

2026-01-12 01:18:05VUE

实现 Vue 搜索后分页的方法

数据绑定与搜索逻辑

在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数:

data() {
  return {
    originalList: [], // 原始数据
    filteredList: [], // 过滤后的数据
    searchQuery: '',  // 搜索关键词
    currentPage: 1,    // 当前页码
    itemsPerPage: 10   // 每页显示数量
  }
}

计算属性处理过滤与分页

使用计算属性实现搜索过滤和分页逻辑:

computed: {
  filteredItems() {
    return this.originalList.filter(item => {
      return item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    })
  },
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    return this.filteredItems.slice(start, start + this.itemsPerPage)
  },
  totalPages() {
    return Math.ceil(this.filteredItems.length / this.itemsPerPage)
  }
}

模板部分实现

在模板中绑定搜索输入和分页控件:

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">

    <ul>
      <li v-for="item in paginatedItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>

    <div class="pagination">
      <button 
        @click="currentPage--" 
        :disabled="currentPage === 1">
        上一页
      </button>
      <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>
      <button 
        @click="currentPage++" 
        :disabled="currentPage >= totalPages">
        下一页
      </button>
    </div>
  </div>
</template>

重置页码处理

当搜索条件变化时,需要重置当前页码到第一页:

watch: {
  searchQuery() {
    this.currentPage = 1
  }
}

样式优化

添加基础样式提升用户体验:

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

异步数据加载

如果数据需要从API获取,可以在created或mounted钩子中加载:

async created() {
  const response = await fetch('/api/items')
  this.originalList = await response.json()
}

vue搜索过后实现分页

标签: 分页vue
分享给朋友:

相关文章

vue实现共享屏幕

vue实现共享屏幕

共享屏幕的基本实现 在Vue中实现共享屏幕功能通常需要结合浏览器的getDisplayMedia API。以下是一个基础实现示例: <template> <div>…

vue循环数组实现

vue循环数组实现

vue循环数组的实现方法 在Vue中循环数组主要通过v-for指令实现,以下是几种常见用法: 基本数组循环 使用v-for指令遍历数组,可以通过(item, index)的语法获取当前项和索引值:…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue如何实现注册

vue如何实现注册

实现用户注册功能 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是完整的实现方案: 创建注册表单组件 注册表单需要包含用户名、邮箱、密码等基本字段,并进行表单验证: <te…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…