当前位置:首页 > VUE

vue实现的表格搜索

2026-02-22 01:37:16VUE

vue实现的表格搜索

Vue 表格搜索实现方法

基础实现(基于本地数据过滤)

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <table>
      <tr v-for="item in filteredItems" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.email }}</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '张三', email: 'zhang@example.com' },
        { id: 2, name: '李四', email: 'li@example.com' }
      ]
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        return item.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
               item.email.toLowerCase().includes(this.searchQuery.toLowerCase())
      })
    }
  }
}
</script>

使用 Lodash 进行防抖优化

<script>
import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      items: [],
      allItems: [] // 原始数据
    }
  },
  created() {
    this.debouncedFilter = debounce(this.filterItems, 300)
  },
  watch: {
    searchQuery() {
      this.debouncedFilter()
    }
  },
  methods: {
    filterItems() {
      if (!this.searchQuery) {
        this.items = [...this.allItems]
        return
      }
      this.items = this.allItems.filter(item => 
        Object.values(item).some(val =>
          String(val).toLowerCase().includes(this.searchQuery.toLowerCase())
        )
      )
    }
  }
}
</script>

服务器端搜索实现

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

多条件高级搜索实现

<template>
  <div>
    <input v-model="searchParams.name" placeholder="姓名">
    <input v-model="searchParams.email" placeholder="邮箱">
    <select v-model="searchParams.status">
      <option value="">所有状态</option>
      <option value="active">活跃</option>
      <option value="inactive">非活跃</option>
    </select>

    <table>
      <tr v-for="item in filteredItems" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.email }}</td>
        <td>{{ item.status }}</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchParams: {
        name: '',
        email: '',
        status: ''
      },
      items: []
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => {
        return (
          (this.searchParams.name === '' || 
           item.name.toLowerCase().includes(this.searchParams.name.toLowerCase())) &&
          (this.searchParams.email === '' || 
           item.email.toLowerCase().includes(this.searchParams.email.toLowerCase())) &&
          (this.searchParams.status === '' || 
           item.status === this.searchParams.status)
        )
      })
    }
  }
}
</script>

使用第三方表格组件(如 Element UI)

<template>
  <el-input v-model="searchQuery" placeholder="搜索"></el-input>
  <el-table :data="filteredItems">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="email" label="邮箱"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: []
    }
  },
  computed: {
    filteredItems() {
      return this.items.filter(item => 
        !this.searchQuery ||
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
        item.email.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

实现要点

  • 使用 v-model 绑定搜索输入框
  • 通过计算属性 computed 实现实时过滤
  • 对于大数据集考虑使用防抖/节流优化性能
  • 服务器端搜索需要处理异步请求和加载状态
  • 多条件搜索需要构建更复杂的过滤逻辑
  • 第三方表格组件通常提供内置搜索功能

vue实现的表格搜索

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

相关文章

vue实现tab换行

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示与隐…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…