当前位置:首页 > 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 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…