当前位置:首页 > VUE

vue搜索过后实现分页

2026-02-25 19:19:43VUE

实现Vue搜索与分页功能

搜索功能实现

在Vue组件中定义搜索方法,通常使用v-model绑定搜索输入框的值。通过计算属性或方法过滤数据列表。

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      allItems: [] // 原始数据
    }
  },
  computed: {
    filteredItems() {
      return this.allItems.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

分页功能实现

基于过滤后的结果实现分页逻辑,需要计算总页数和当前页显示的数据。

vue搜索过后实现分页

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

完整组件示例

<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 
        v-for="page in totalPages" 
        :key="page"
        @click="currentPage = page"
        :class="{ active: currentPage === page }"
      >
        {{ page }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      currentPage: 1,
      itemsPerPage: 10,
      allItems: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' }
        // 更多数据...
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.allItems.filter(item =>
        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)
    }
  }
}
</script>

<style>
.pagination button.active {
  font-weight: bold;
  color: blue;
}
</style>

优化建议

vue搜索过后实现分页

对于大型数据集,考虑使用后端分页和搜索。前端仅处理当前页的数据,减少内存使用。

methods: {
  async fetchData() {
    const response = await axios.get('/api/items', {
      params: {
        search: this.searchQuery,
        page: this.currentPage,
        perPage: this.itemsPerPage
      }
    })
    this.paginatedItems = response.data.items
    this.totalPages = response.data.totalPages
  }
}

注意事项

搜索和分页功能需要密切配合。搜索后应重置当前页码为1,避免显示空页。可以在搜索查询变化时添加监听:

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

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

相关文章

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双…