根据提供的代码,可以使用Vue的computed属性和watch属性结合的方式来实现查询功能。首先,在data属性中添加一个新的属性filteredVoucherGroupList,用于存储过滤后的列表数据。然后,根据搜索框的输入值,在querySearch方法中调用接口listVoucherGroup获取列表数据,并进行过滤。最后,在模板中显示filteredVoucherGroupList

以下是实现查询功能的代码示例:

<template>
  <div>
    <el-autocomplete
      class="inline-input"
      v-model="searchvalue"
      :fetch-suggestions="querySearch"
      placeholder="输入抽样模板编码或名称"
      :trigger-on-focus="false"
      @select="handleQuery"
    ></el-autocomplete>

    <ul>
      <li v-for="voucherGroup in filteredVoucherGroupList" :key="voucherGroup.voucherGroupId">
        {{ voucherGroup.name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchvalue: "",
      voucherGroupList: [], // 列表数据
      filteredVoucherGroupList: [], // 过滤后的列表数据
    };
  },
  methods: {
    querySearch(queryString, cb) {
      // 调用接口listVoucherGroup获取列表数据
      listVoucherGroup({ ...this.queryParams, searchString: queryString }).then((response) => {
        const filteredList = this.handleTree(response.data, "voucherGroupId", "parentId");
        cb(filteredList);
        this.filteredVoucherGroupList = filteredList;
      });
    },
    handleQuery(selectedItem) {
      // 处理查询结果
      console.log(selectedItem);
    },
  },
  computed: {
    queryParams() {
      return {
        accountsId: this.accountsId,
        // 其他查询参数
      };
    },
  },
  watch: {
    queryParams: {
      handler() {
        // 当查询参数改变时,重新获取列表数据
        this.getList();
      },
      deep: true,
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

在上述代码中,filteredVoucherGroupList用于存储过滤后的列表数据。在querySearch方法中,调用listVoucherGroup接口并传入搜索框的输入值,获取列表数据,并将数据赋值给filteredVoucherGroupList。在模板中使用v-for指令遍历filteredVoucherGroupList显示列表项。

另外,为了实现实时搜索,可以在fetch-suggestions属性中设置一个延迟执行的方法,例如使用lodash库的debounce方法对querySearch方法进行包装,以实现延迟搜索的效果

getList thisloading = true; thisqueryParamsaccountsId = thisaccountsId; listVoucherGroupthisqueryParamsthenresponse = consolelogresponse response; thisvoucherGroupL

原文地址: https://www.cveoy.top/t/topic/iQD4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录