当前位置:首页 > VUE

vue列表查询实现

2026-01-08 04:40:52VUE

实现 Vue 列表查询功能

基础列表渲染 在 Vue 中可以通过 v-for 指令实现列表渲染,结合计算属性动态过滤数据:

<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: '',
      originalList: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' }
      ]
    }
  },
  computed: {
    filteredList() {
      return this.originalList.filter(item => 
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

使用 watch 实现异步查询 当需要从 API 获取数据时,可以使用 watch 监听查询条件变化:

export default {
  data() {
    return {
      searchQuery: '',
      loading: false,
      resultList: []
    }
  },
  watch: {
    searchQuery(newVal) {
      this.debouncedSearch(newVal)
    }
  },
  created() {
    this.debouncedSearch = _.debounce(this.fetchData, 500)
  },
  methods: {
    async fetchData(query) {
      this.loading = true
      try {
        const res = await axios.get('/api/items', { params: { q: query } })
        this.resultList = res.data
      } finally {
        this.loading = false
      }
    }
  }
}

分页查询实现 结合分页组件实现完整查询功能:

export default {
  data() {
    return {
      queryParams: {
        keyword: '',
        page: 1,
        pageSize: 10
      },
      total: 0,
      dataList: []
    }
  },
  methods: {
    async loadData() {
      const res = await api.getItems(this.queryParams)
      this.dataList = res.data.list
      this.total = res.data.total
    },
    handleSearch() {
      this.queryParams.page = 1
      this.loadData()
    }
  },
  mounted() {
    this.loadData()
  }
}

高级过滤功能 实现多条件复合查询:

computed: {
  filteredItems() {
    return this.items.filter(item => {
      return (
        (!this.filters.category || item.category === this.filters.category) &&
        (!this.filters.priceMin || item.price >= this.filters.priceMin) &&
        (!this.filters.priceMax || item.price <= this.filters.priceMax)
      )
    })
  }
}

性能优化建议

  • 对频繁触发的搜索使用防抖(debounce)
  • 大数据量时采用虚拟滚动(vue-virtual-scroller)
  • 复杂查询条件可考虑使用 Web Worker
  • 服务端查询时确保有数据库索引支持

vue列表查询实现

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

相关文章

vue 实现手册

vue 实现手册

以下是关于 Vue.js 实现手册的整理内容,涵盖核心概念、实践方法和常见场景的解决方案: 核心概念与基础用法 Vue.js 是一个渐进式 JavaScript 框架,核心特点包括数据驱动视图和组…

vue实现前端分离

vue实现前端分离

Vue 实现前后端分离 前后端分离是一种架构模式,前端负责页面渲染和交互逻辑,后端提供数据接口。Vue.js 作为前端框架,非常适合实现前后端分离的开发模式。 核心实现步骤 创建 Vue 项目 使用…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…