当前位置:首页 > VUE

vue实现搜索分页

2026-01-17 05:14:18VUE

实现搜索分页的基本思路

在Vue中实现搜索分页功能通常需要结合以下几个关键点:数据绑定、搜索过滤、分页逻辑处理。以下是一个完整的实现方案。

数据准备与绑定

定义数据列表和分页相关变量,通常包括当前页、每页条数、总条数等。在Vue的data中初始化这些变量:

data() {
  return {
    searchQuery: '',       // 搜索关键词
    items: [],             // 原始数据列表
    filteredItems: [],     // 过滤后的数据列表
    currentPage: 1,        // 当前页码
    itemsPerPage: 10,      // 每页显示条数
    totalItems: 0          // 总条数
  }
}

搜索过滤逻辑

通过计算属性或方法实现搜索过滤。计算属性会根据搜索关键词动态过滤数据:

computed: {
  filteredItems() {
    return this.items.filter(item => {
      return item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    })
  }
}

分页计算逻辑

计算当前页显示的数据切片,通常结合slice方法实现:

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)
  }
}

分页控件实现

在模板中添加分页控件,通常包括页码按钮和上一页/下一页按钮:

vue实现搜索分页

<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="prevPage" :disabled="currentPage === 1">上一页</button>
    <span v-for="page in totalPages" :key="page">
      <button @click="currentPage = page" :class="{ active: currentPage === page }">{{ page }}</button>
    </span>
    <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
  </div>
</div>

分页方法实现

添加分页导航的方法:

methods: {
  prevPage() {
    if (this.currentPage > 1) {
      this.currentPage--
    }
  },
  nextPage() {
    if (this.currentPage < this.totalPages) {
      this.currentPage++
    }
  }
}

样式优化

为分页控件添加基本样式:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}

完整组件示例

将上述代码整合成一个完整的Vue组件:

vue实现搜索分页

<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="prevPage" :disabled="currentPage === 1">上一页</button>
      <span v-for="page in totalPages" :key="page">
        <button @click="currentPage = page" :class="{ active: currentPage === page }">{{ page }}</button>
      </span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' },
        // 更多数据...
      ],
      currentPage: 1,
      itemsPerPage: 2
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        return item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      })
    },
    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)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    }
  }
}
</script>

<style>
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
}
.pagination button.active {
  background-color: #42b983;
  color: white;
}
</style>

后端API集成方案

如果需要从后端API获取分页数据,可以修改为以下实现方式:

methods: {
  async fetchData() {
    const params = {
      page: this.currentPage,
      pageSize: this.itemsPerPage,
      keyword: this.searchQuery
    }
    const response = await axios.get('/api/items', { params })
    this.items = response.data.items
    this.totalItems = response.data.total
  },
  handleSearch() {
    this.currentPage = 1
    this.fetchData()
  }
}

在模板中添加搜索按钮或使用watch监听搜索词变化:

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

性能优化建议

对于大数据量情况,考虑以下优化措施:

  • 使用防抖处理搜索输入,避免频繁触发搜索
  • 实现虚拟滚动代替完整列表渲染
  • 在后端实现搜索和分页逻辑,减少前端计算压力

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

相关文章

简易实现vue

简易实现vue

简易 Vue 实现思路 核心是数据劫持结合发布-订阅模式,通过 Object.defineProperty 或 Proxy 监听数据变化,触发视图更新。 数据劫持实现 class Observer…

vue实现aop切面

vue实现aop切面

Vue 实现 AOP 切面编程的方法 在 Vue 中实现 AOP(面向切面编程)可以通过多种方式,以下是几种常见的实现方法: 使用 Vue 混入(Mixins) Vue 的混入机制可以用于实现横切…

拖拽式编程vue实现

拖拽式编程vue实现

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

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…