当前位置:首页 > VUE

vue实现列表查询

2026-01-19 05:09:11VUE

Vue 实现列表查询功能

在 Vue 中实现列表查询功能通常需要结合数据绑定、事件处理和过滤逻辑。以下是几种常见的实现方式:

使用计算属性过滤列表

计算属性是 Vue 中实现列表查询的高效方式,它会根据依赖自动缓存结果。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <ul>
      <li v-for="item in filteredList" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    }
  },
  computed: {
    filteredList() {
      return this.items.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 和 methods 实现

对于更复杂的查询逻辑,可以结合 watch 和 methods 实现。

<template>
  <div>
    <input v-model="searchQuery" @input="filterItems">
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ],
      filteredItems: []
    }
  },
  created() {
    this.filteredItems = [...this.items]
  },
  methods: {
    filterItems() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用第三方库实现高级搜索

对于更复杂的搜索需求,可以考虑使用 Fuse.js 等模糊搜索库。

import Fuse from 'fuse.js'

export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果', category: '水果' },
        { id: 2, name: '胡萝卜', category: '蔬菜' },
        { id: 3, name: '牛肉', category: '肉类' }
      ],
      fuse: null,
      searchResults: []
    }
  },
  created() {
    this.fuse = new Fuse(this.items, {
      keys: ['name', 'category'],
      threshold: 0.4
    })
  },
  watch: {
    searchQuery(newVal) {
      this.searchResults = newVal ? 
        this.fuse.search(newVal).map(r => r.item) : 
        [...this.items]
    }
  }
}

结合分页实现查询

对于大数据量的列表,可以结合分页实现查询功能。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <ul>
      <li v-for="item in paginatedData" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页</span>
    <button @click="nextPage" :disabled="currentPage >= pageCount">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      currentPage: 1,
      pageSize: 5,
      items: [
        // 大数组数据
      ]
    }
  },
  computed: {
    filteredList() {
      return this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    },
    pageCount() {
      return Math.ceil(this.filteredList.length / this.pageSize)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.filteredList.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用 Vuex 管理搜索状态

在大型应用中,可以使用 Vuex 集中管理搜索状态。

// store.js
export default new Vuex.Store({
  state: {
    searchQuery: '',
    items: [
      { id: 1, name: '苹果' },
      { id: 2, name: '香蕉' },
      { id: 3, name: '橙子' }
    ]
  },
  getters: {
    filteredItems: state => {
      return state.items.filter(item =>
        item.name.toLowerCase().includes(state.searchQuery.toLowerCase())
      )
    }
  },
  mutations: {
    setSearchQuery(state, query) {
      state.searchQuery = query
    }
  }
})

// 组件中
<template>
  <div>
    <input 
      :value="searchQuery" 
      @input="updateSearchQuery($event.target.value)"
      placeholder="搜索..."
    >
    <ul>
      <li v-for="item in filteredItems" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex'

export default {
  computed: {
    ...mapState(['searchQuery']),
    ...mapGetters(['filteredItems'])
  },
  methods: {
    ...mapMutations(['setSearchQuery']),
    updateSearchQuery(query) {
      this.setSearchQuery(query)
    }
  }
}
</script>

以上方法可以根据具体需求选择或组合使用,实现不同复杂度的列表查询功能。

vue实现列表查询

标签: 列表vue
分享给朋友:

相关文章

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue实现route

vue实现route

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

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue环境实现

vue环境实现

Vue 环境搭建 安装 Node.js 和 npm Node.js 是运行 Vue 的基础环境,npm 是包管理工具。从 Node.js 官网 下载并安装最新稳定版本。安装完成后,在终端运行以下命令验…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…