el-select远程搜索

在项目中经常会用到el-select远程所搜,今天记录一下
1.首先将组件引入到项目页面中

 <el-select
            v-model="ruleForm.newCertificateName"
            filterable
            remote
            reserve-keyword
            :remote-method="remoteMethod"
            clearable
            placeholder="请选择新证书名称"
            @change="changeVal"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item"
            >
            </el-option>
          </el-select>

filterable:是否可搜索
remote:是否远程搜索
remote-method:远程搜索的方法
change:选中值发生变化触发
options:下拉框内容
2.在data中定义options和list

data() {
    return {
      list: [],//输入值过滤的内容
      options: [],//下拉框内容
  },
}

3.调用后端接口获取值

 getGood() {
      getAllGoodsApi().then((res) => {
        if (res.code == 0) {
          // console.log(res);
          // 将返回结果的data字段proId,proName映射到数组list中
          this.list = res.data.map((item) => {
            return { value: `${item.proId}`, label: `${item.proName}` };
          });
        }
      });
    },

4.根据输入的值过滤

 // 根据输入的值过滤
    remoteMethod(query) {
      // console.log(query);
      // console.log(this.list, 1);
      if (query !== "") {
        //根据输入的值过滤list数组,并将结果赋值给options,用于展示下拉选型
        this.options = this.list.filter((item) => {
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
      } else {
        // 当query为空时option为空数组
        this.options = [];
      }
    },

5.选择的项

 changeVal(val) {
      // console.log(val);
      this.ruleForm.newCertificateName = val.label;
    },
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-select 远程搜索回显的实现需要以下步骤: 1. 使用 el-select 的 remote 方法来实现远程搜索功能; 2. 在 remote 方法中,将搜索关键字作为参数传递给后端接口,并获取接口返回的搜索结果; 3. 在 el-select 的 filterable 属性中设置为 true,开启搜索功能; 4. 在 el-select 的 remote-method 属性中设置一个函数,用于在搜索时调用 remote 方法并更新 el-select 的下拉列表的选项; 5. 在 remote 方法中,将后端接口返回的搜索结果映射为 el-select 的下拉列表选项,并将这些选项作为参数传递给 remote 方法的回调函数; 6. 在回调函数中,更新 el-select 的下拉列表的选项。 以下是一个示例代码: ```html <el-select v-model="value" remote filterable :remote-method="getOptions" :loading="loading" > <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value" /> </el-select> ``` ```javascript export default { data() { return { value: '', options: [], loading: false, }; }, methods: { getOptions(query) { if (query !== '') { this.loading = true; // 调用后端接口获取搜索结果 this.$http.get('/api/search', { params: { q: query } }).then(res => { this.loading = false; // 将搜索结果映射为 el-select 的下拉列表选项 const options = res.data.results.map(result => ({ value: result.id, label: result.name, })); // 调用回调函数更新 el-select 的下拉列表的选项 this.options = options; }); } else { this.options = []; } }, }, }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值