You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Vuetify Data Table支持数组形式多条件搜索?

Vuetify Data Table 多条件搜索实现方案

问题描述

我正在使用Vuetify Data Table实现搜索功能,但官方文档里的search参数是字符串类型。能不能把search参数设为数组,实现多条件同时搜索?比如同时搜索x和y,把它们放进search: []然后绑定到v-data-table的search属性?

组件代码

<v-data-table v-show="!map"
    :custom-filter="customSearch"
    id="mytable"
    :search="search"
    :headers="headers"
    :items="getcartables.data"
    style="white-space: nowrap; cursor: pointer"
    @dblclick:row="showDetails"
    v-model="selectedRows"
    class="box-shadow mx-5 border-radius-2"
  >
    <template v-slot:item="{ item, index }">
      <tr
        :class="selectedRows.indexOf(item.refrenceEid) > -1 ? 'active' : ''"
        @click="rowClicked(item)"
        @dblclick="showDetails($event, item)"
      >
        <td>
          {{ index + 1 }}
        </td>
        <td>{{ item.demandNumber }}</td>
        <td>{{ item.folderNumber }}</td>
        <td>{{ item.workflowTitle }}</td>
     
      </tr>
    </template>
  
  </v-data-table>

自定义过滤函数(暂未启用)

customSearch(value, search, item) {
  return Object.values(item).some(
      (v) => v && v.toString().toLowerCase().includes(search)
  );
},

多条件相关代码

async renewWallPorMishe() {
  let area = this.mantaghe ? this.mantaghe : 0
  let local = this.hoze ? this.hoze : 0
  let block = this.boluk ? this.boluk : 0
  let property = this.melk ? this.melk : 0
  let building = this.sakhteman ? this.sakhteman : 0
  let apartment = this.apartman ? this.apartman : 0
  let guild = this.senf ? this.senf : 0
  this.renewcode =
      area +
      "-" +
      local +
      "-" +
      block +
      "-" +
      property +
      "-" +
      building +
      "-" +
      apartment +
      "-" +
      guild;
  this.search = this.renewcode
  this.renew = true
},

解决方案

Vuetify的v-data-table默认只支持字符串类型的search参数,但可以通过自定义过滤函数实现多条件搜索,既可以把search设为数组适配,也可以直接在过滤函数中整合多个变量。

方案1:将search设为数组,修改自定义过滤函数

把search定义为数组(比如search: []),然后修改customSearch函数,根据需求实现AND/OR逻辑的多条件匹配:

customSearch(value, searchArray, item) {
  // 空搜索数组时显示所有数据
  if (!searchArray || searchArray.length === 0) return true;
  
  // AND逻辑:所有搜索条件都要匹配
  return searchArray.every(searchTerm => {
    if (!searchTerm) return true; // 跳过空条件
    return Object.values(item).some(
      v => v && v.toString().toLowerCase().includes(searchTerm.toLowerCase())
    );
  });

  // OR逻辑:任意条件匹配即可,替换成下面的代码
  // return searchArray.some(searchTerm => {
  //   if (!searchTerm) return false;
  //   return Object.values(item).some(
  //     v => v && v.toString().toLowerCase().includes(searchTerm.toLowerCase())
  //   );
  // });
}

然后在renewWallPorMishe方法中,把多个条件存入search数组:

async renewWallPorMishe() {
  // 生成renewcode逻辑不变
  let area = this.mantaghe ? this.mantaghe : 0
  let local = this.hoze ? this.hoze : 0
  let block = this.boluk ? this.boluk : 0
  let property = this.melk ? this.melk : 0
  let building = this.sakhteman ? this.sakhteman : 0
  let apartment = this.apartman ? this.apartman : 0
  let guild = this.senf ? this.senf : 0
  this.renewcode =
      area +
      "-" +
      local +
      "-" +
      block +
      "-" +
      property +
      "-" +
      building +
      "-" +
      apartment +
      "-" +
      guild;
  
  // 将renewcode和其他搜索条件放入数组
  this.search = [this.renewcode, this.otherSearchTerm]; // otherSearchTerm为你的其他v-model值
  this.renew = true
},

方案2:直接在过滤函数中使用多个变量(无需修改search参数)

如果不想改变search的类型,可以直接在customSearch里引用组件的多个搜索变量,比如this.renewcode:

customSearch(value, search, item) {
  // 原字符串search的匹配逻辑
  const baseMatch = !search || Object.values(item).some(
    v => v && v.toString().toLowerCase().includes(search.toLowerCase())
  );

  // renewcode的匹配逻辑
  const renewcodeMatch = !this.renewcode || Object.values(item).some(
    v => v && v.toString().toLowerCase().includes(this.renewcode.toLowerCase())
  );

  // 可添加更多条件,比如anotherTerm
  const anotherMatch = !this.anotherTerm || Object.values(item).some(
    v => v && v.toString().toLowerCase().includes(this.anotherTerm.toLowerCase())
  );

  // 所有条件需同时满足(AND逻辑)
  return baseMatch && renewcodeMatch && anotherMatch;
}

这种方式不需要调整search的使用方式,直接在过滤函数中整合多条件即可。

注意事项

  • 确保v-data-table上已绑定:custom-filter="customSearch",启用自定义过滤。
  • 根据业务需求选择AND/OR逻辑,处理空条件避免误过滤数据。

内容的提问来源于stack exchange,提问作者sanaz baghban

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 14:16:47