当前位置:首页 > 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)
  }
}

分页控件实现

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

<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组件:

<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 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现抽奖

vue实现抽奖

Vue 实现抽奖功能 基本抽奖逻辑 创建一个Vue组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…