当前位置:首页 > VUE

vue实现的表格搜索

2026-01-21 10:10:35VUE

Vue 实现表格搜索功能

基础实现方法

数据绑定与搜索输入 在 Vue 中创建一个响应式的搜索输入框,绑定到 data 中的 searchQuery 属性:

<input v-model="searchQuery" placeholder="搜索..." />

表格数据过滤 使用 computed 属性对表格数据进行实时过滤:

computed: {
  filteredItems() {
    return this.items.filter(item => 
      item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
    );
  }
}

表格渲染 在模板中使用 filteredItems 代替原始数据:

<tr v-for="item in filteredItems" :key="item.id">
  <td>{{ item.name }}</td>
  <td>{{ item.age }}</td>
</tr>

多字段搜索实现

扩展搜索功能 修改过滤逻辑以支持多字段搜索:

computed: {
  filteredItems() {
    const query = this.searchQuery.toLowerCase();
    return this.items.filter(item => 
      item.name.toLowerCase().includes(query) ||
      item.email.toLowerCase().includes(query) ||
      item.department.toLowerCase().includes(query)
    );
  }
}

高级搜索实现

分页集成 当搜索结果需要分页时,可以结合分页组件:

computed: {
  paginatedItems() {
    const start = (this.currentPage - 1) * this.itemsPerPage;
    return this.filteredItems.slice(start, start + this.itemsPerPage);
  },
  totalPages() {
    return Math.ceil(this.filteredItems.length / this.itemsPerPage);
  }
}

延迟搜索优化 对于大数据量,可以使用防抖技术减少计算频率:

data() {
  return {
    searchQuery: '',
    debouncedSearch: ''
  };
},
created() {
  this.debounceSearch = _.debounce(() => {
    this.debouncedSearch = this.searchQuery;
  }, 300);
},
watch: {
  searchQuery() {
    this.debounceSearch();
  }
}

服务器端搜索

API 请求实现 对于大量数据,建议使用服务器端搜索:

methods: {
  searchItems() {
    axios.get('/api/items', {
      params: {
        search: this.searchQuery
      }
    }).then(response => {
      this.items = response.data;
    });
  }
}

搜索触发方式 可以监听搜索输入变化或添加搜索按钮:

<input v-model="searchQuery" @input="searchItems" />
<!-- 或 -->
<button @click="searchItems">搜索</button>

UI 增强

加载状态显示 在等待搜索结果时显示加载状态:

data() {
  return {
    isLoading: false
  };
},
methods: {
  async searchItems() {
    this.isLoading = true;
    try {
      const response = await axios.get('/api/items', {
        params: { search: this.searchQuery }
      });
      this.items = response.data;
    } finally {
      this.isLoading = false;
    }
  }
}

空结果提示 当没有搜索结果时显示提示信息:

vue实现的表格搜索

<tr v-if="filteredItems.length === 0">
  <td colspan="100%">没有找到匹配的结果</td>
</tr>

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

相关文章

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contai…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterva…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…