当前位置:首页 > 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>

多条件复合查询

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

vue列表查询实现

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

分页处理

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

vue列表查询实现

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

服务端查询

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

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 grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现监控

vue实现监控

Vue 实现监控功能的方法 在 Vue 中实现监控功能通常涉及数据变化监听、事件监听或第三方监控工具集成。以下是几种常见实现方式: 使用 Vue 的 watch 监听数据变化 通过 watch 可以…

vue实现spa

vue实现spa

Vue 实现 SPA(单页应用)的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保安装了 Vue Router 作为路由管理工具。运行以下命令创建项目: vue c…

vue怎么实现直播

vue怎么实现直播

Vue实现直播的方法 使用Vue实现直播功能通常需要结合第三方直播服务或WebRTC技术。以下是几种常见的实现方式: 使用第三方直播服务 集成如腾讯云、阿里云或七牛云等提供的直播SDK,通过他们的A…

vue实现拦截登录

vue实现拦截登录

Vue 实现登录拦截的方法 使用路由守卫进行拦截 Vue Router 提供了全局前置守卫 beforeEach,可以在路由跳转前进行验证。在路由配置文件中添加以下代码: import router…