当前位置:首页 > 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 Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListe…