当前位置:首页 > VUE

vue搜索过后实现分页

2026-01-07 02:15:47VUE

Vue 实现搜索后分页功能

数据绑定与搜索逻辑

在 Vue 组件中定义必要的数据属性:

data() {
  return {
    searchQuery: '',
    currentPage: 1,
    itemsPerPage: 10,
    allItems: [], // 原始数据
    filteredItems: [] // 搜索结果
  }
}

实现搜索方法:

methods: {
  performSearch() {
    this.filteredItems = this.allItems.filter(item => 
      item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    )
    this.currentPage = 1 // 搜索后重置页码
  }
}

计算属性处理分页

创建计算属性计算分页数据:

computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage
    const end = start + this.itemsPerPage
    return this.filteredItems.slice(start, end)
  },
  totalPages() {
    return Math.ceil(this.filteredItems.length / this.itemsPerPage)
  }
}

模板部分实现

搜索输入框和分页控制:

<input v-model="searchQuery" @input="performSearch" placeholder="搜索...">

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

<div class="pagination">
  <button 
    @click="currentPage--" 
    :disabled="currentPage === 1"
  >上一页</button>

  <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span>

  <button 
    @click="currentPage++" 
    :disabled="currentPage >= totalPages"
  >下一页</button>
</div>

样式优化

添加基础样式改善用户体验:

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

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

高级实现方案

对于大型数据集考虑使用服务器端分页:

async fetchPaginatedData() {
  const response = await axios.get('/api/items', {
    params: {
      search: this.searchQuery,
      page: this.currentPage,
      limit: this.itemsPerPage
    }
  })
  this.paginatedItems = response.data.items
  this.totalItems = response.data.total
}

使用第三方库

考虑使用成熟的分页组件如 vuejs-paginate

import Paginate from 'vuejs-paginate-next'

export default {
  components: { Paginate }
}

模板中使用:

<paginate
  v-model="currentPage"
  :page-count="totalPages"
  :click-handler="fetchPaginatedData"
  :prev-text="'<'"
  :next-text="'>'"
>
</paginate>

vue搜索过后实现分页

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

相关文章

vue实现分支

vue实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容…

订单 vue实现

订单 vue实现

Vue 实现订单功能 数据模型设计 订单功能通常需要设计以下数据结构: // 订单数据结构 const order = { id: '', // 订单ID userId…

vue实现评论编辑

vue实现评论编辑

Vue 实现评论编辑功能 数据绑定与表单结构 使用 v-model 双向绑定评论内容到表单输入框,确保编辑时能显示原内容并同步修改。示例模板结构: <template> <di…

vue实现图片分析

vue实现图片分析

Vue 实现图片分析的方法 在 Vue 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas 进行…

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install a…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…