Vue使用element-ul输入框input模糊查询加多选功能

1、我这里的功能要求为:可以一次性多新增多个人员并且需要有模糊查询功能。

            <el-select
                v-model="form.userId"
                multiple
                filterable
                remote
                reserve-keyword
                placeholder="请输入关键词"
                :remote-method="remoteMethod"
                :loading="loading"
                @change="handleSelectBranchCom"
              >
                <el-option
                  v-for="item in staffUserList"
                  :key="item.value"
                  :label="item.label"
                  style="height: 30px"
                  :value="item.value"
                >
                </el-option>
              </el-select>
data() {
    return{
        staffUserList: [],
        states: "",
        list: "",   
    }
}

2、我这里点击新增按钮的时候会一开始查询所有。

    // 点击新增查询一次所有人员
    handleAdd() {
      listStaffUser().then((res)=> {
        this.states = res.rows;
        this.list = this.states.map((item) => {
            return { value: item.userId, label: item.nickName };
        });
        this.staffUserList = this.list;
      })
      this.open = true;
      this.title = "添加巡检人员";
      this.reset();
    },
    
    // 参数发生变化调用模糊查询
    remoteMethod(query) {
      var that = this;
      listStaffUser({
        params: {
          comName: query, //请求的参数  输入的值
        },
      })
        .then(function (res) {
          //将取到的值进行遍历
          that.states = res.rows;
          that.list = that.states.map((item) => {
            // item.userId item.nickName 根据接口里的关键字
            return { value: item.userId, label: item.nickName };
          });
        })
        .catch(function (err) {
          console.log(err);
        });
      setTimeout(() => {
        if (query !== "") {
          that.loading = true;
          setTimeout(() => {
            that.loading = false;
            that.staffUserList = that.list.filter((item) => {
              //这里的label值对应着 上面遍历 return  返回的label:item.nickName 
              return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
            });
          }, 200);
        } else {
          that.staffUserList = that.list;
        }
      }, 100);
    },
    // 拿到Id
    handleSelectBranchCom(item) {
      this.userId = item;
    },

3、最后实现的功能效果如下

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值