当前位置:首页 > 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组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock-…

vue 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕包…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…