当前位置:首页 > VUE

vue列表查询实现

2026-01-14 01:58:32VUE

实现Vue列表查询功能

基本数据绑定与渲染

在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。

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

多条件复合查询

对于更复杂的查询需求,可以扩展查询条件和过滤逻辑。

<template>
  <div>
    <input v-model="queryParams.name" placeholder="名称">
    <input v-model="queryParams.category" placeholder="类别">
    <select v-model="queryParams.status">
      <option value="">全部</option>
      <option value="1">启用</option>
      <option value="0">禁用</option>
    </select>

    <table>
      <tr v-for="item in filteredItems" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.category }}</td>
        <td>{{ item.status ? '启用' : '禁用' }}</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryParams: {
        name: '',
        category: '',
        status: ''
      },
      items: [
        { id: 1, name: '产品A', category: '电子', status: 1 },
        { id: 2, name: '产品B', category: '家居', status: 0 }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        const nameMatch = item.name.includes(this.queryParams.name)
        const categoryMatch = this.queryParams.category ? 
          item.category === this.queryParams.category : true
        const statusMatch = this.queryParams.status ? 
          item.status.toString() === this.queryParams.status : true
        return nameMatch && categoryMatch && statusMatch
      })
    }
  }
}
</script>

分页处理

对于大数据量列表,需要结合分页功能实现高效查询。

<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 >= totalPages">下一页</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())
      )
    },
    totalPages() {
      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.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用第三方库优化查询

对于更复杂的查询需求,可以考虑使用lodash等工具库的debounce函数优化性能。

import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      filteredItems: []
    }
  },
  created() {
    this.debouncedFilter = debounce(this.filterItems, 300)
  },
  watch: {
    searchQuery() {
      this.debouncedFilter()
    }
  },
  methods: {
    filterItems() {
      this.filteredItems = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}

服务端查询

当数据量很大时,应该将查询逻辑放到服务端,前端只负责发送请求和显示结果。

vue列表查询实现

export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      loading: false
    }
  },
  watch: {
    searchQuery() {
      this.fetchItems()
    }
  },
  methods: {
    async fetchItems() {
      this.loading = true
      try {
        const response = await axios.get('/api/items', {
          params: {
            q: this.searchQuery
          }
        })
        this.items = response.data
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}

这些方法涵盖了从基础到高级的Vue列表查询实现方案,可以根据实际需求选择合适的实现方式。

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

相关文章

vue实现多层评论回复

vue实现多层评论回复

Vue 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现横滚

vue实现横滚

Vue 实现横滚效果 横滚效果通常指水平滚动内容,常见于轮播图、横向导航栏等场景。以下是几种实现方式: 使用 CSS 和 Vue 结合 通过 CSS 的 overflow-x 和 white-spa…

vue实现im通信

vue实现im通信

Vue 实现 IM 通信的方法 选择通信协议 WebSocket 是实现实时 IM 通信的常用协议,相比 HTTP 更适合双向通信。可使用原生 WebSocket 或第三方库如 Socket.IO。…

vue实现销售程序

vue实现销售程序

Vue实现销售程序的基本步骤 搭建Vue项目基础结构 使用Vue CLI或Vite初始化项目,安装必要依赖(如Vue Router、Vuex/Pinia、Element UI/Ant Design V…

vue如何实现tap

vue如何实现tap

实现Tap事件的方法 在Vue中实现类似移动端的tap事件(轻触)可以通过以下几种方式完成。原生移动端浏览器通常通过touchstart和touchend事件组合模拟tap,而Vue中可以封装自定义指…