当前位置:首页 > VUE

vue实现列表查询

2026-02-19 21:30:41VUE

Vue 实现列表查询的方法

基础列表渲染

使用 v-for 指令渲染数组数据,配合 key 属性提升性能:

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' }
      ]
    }
  }
}
</script>

动态过滤查询

通过计算属性实现实时搜索过滤:

vue实现列表查询

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

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

分页功能实现

结合分页参数进行数据切片:

vue实现列表查询

<template>
  <ul>
    <li v-for="item in paginatedItems" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
  <button @click="prevPage">Previous</button>
  <span>Page {{ currentPage }}</span>
  <button @click="nextPage">Next</button>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 5,
      items: [...] // 假设有大量数据
    }
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      return this.items.slice(start, start + this.itemsPerPage)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage * this.itemsPerPage < this.items.length) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

异步数据加载

通过 API 获取远程数据:

<template>
  <ul v-if="!loading">
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
  <div v-else>Loading...</div>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      items: []
    }
  },
  async created() {
    try {
      const response = await fetch('https://api.example.com/items')
      this.items = await response.json()
    } catch (error) {
      console.error(error)
    } finally {
      this.loading = false
    }
  }
}
</script>

复合查询功能

结合多个过滤条件和排序:

<template>
  <input v-model="filters.name" placeholder="Name">
  <select v-model="filters.category">
    <option value="">All</option>
    <option value="electronics">Electronics</option>
  </select>

  <ul>
    <li v-for="item in processedItems" :key="item.id">
      {{ item.name }} - {{ item.price }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        name: '',
        category: ''
      },
      items: [...]
    }
  },
  computed: {
    processedItems() {
      return this.items
        .filter(item => {
          const nameMatch = item.name.toLowerCase()
            .includes(this.filters.name.toLowerCase())
          const categoryMatch = this.filters.category === '' 
            || item.category === this.filters.category
          return nameMatch && categoryMatch
        })
        .sort((a, b) => a.price - b.price)
    }
  }
}
</script>

使用第三方库

对于复杂场景可考虑使用专门库如 vue-infinite-loading 实现无限滚动,或 vue-tables-2 实现高级表格功能。安装后按文档配置即可快速实现复杂列表功能。

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

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现表白

vue实现表白

Vue 实现表白页面 使用 Vue 可以快速创建一个动态、交互式的表白页面。以下是一个简单的实现方案: 基础结构 创建一个 Vue 项目或单文件组件,包含以下核心部分: <template&…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…