element ui 用户名远程搜索

用户远程搜索组件

<template>

  <el-select v-model="valueTmp" filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethod" :loading="loading" clearable :disabled="disabled" @change="onchange" @clear="onClear">

    <el-option v-for="item in options" :key="item.id" :label="item.employeeCode+'-'+item.employeeName" :value="item.id">

    </el-option>

  </el-select>

</template>



<script>

import { getEmpSelect } from '@/api/human/employee'

export default {

  props: {

    disabled: {

      type: Boolean,

      default: false

    },

    value: {

      type: Number,

      default: null

    },

    //

    queryType: {

      type: Number,

      default: 0

    }

  },

  data() {

    return {

      valueTmp: null,

      options: [],

      loading: false

    }

  },

  watch: {

    value: {

      handler(val) {

        this.valueTmp = val

        this.$nextTick(()=>{

          if (val) {

            this.remoteMethod(null, val)

          } else {

            this.remoteMethod(null)

          }

        })

      }

    },

    valueTmp(val) {

      this.$emit('input', val)

    }

  },

  mounted() {

    this.remoteMethod()

  },

  methods: {

    remoteMethod(query, id) {

      if (query !== '') {

        this.loading = true;

        let req = {

          id: id,

          employeeName: query,

          queryType: this.queryType,

          pageNum: 1,

          pageSize: 20

        }

        getEmpSelect(req).then((rsp)=>{

          this.options = rsp.data

          this.loading = false;

        })

      } else {

        this.options = [];

      }

    },

    onchange(val) {

      let row = this.options.find(item=>item.id === val)

      this.$emit('change', val, row)

    },

    onClear() {

      this.$emit('input', null)

    }

  }

}

</script>

使用

<select-employee ref="empSelect" v-model="queryForm.maintenanceLeaderId" />

可以使用 Element UI 的 Select 控件结合远程搜索功能来实现。下面是一个简单的示例代码: ```vue <template> <div> <el-select v-model="selectedValue" remote filterable remote-method="remoteSearch" :loading="loading" :remote-method="remoteSearch" :loading-text="'Searching...'" :no-match-text="'No matching results'" :value-key="'value'" > <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" ></el-option> </el-select> </div> </template> <script> export default { data() { return { selectedValue: '', loading: false, options: [] }; }, methods: { remoteSearch(query) { this.loading = true; // 发起远程请求,根据 query 获取匹配的数据 // 假设远程请求返回的数据格式为 [{ value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }] setTimeout(() => { // 模拟异步请求 this.loading = false; this.options = [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' } ]; }, 1000); } } }; </script> ``` 上述代码中,我们使用了 `remote` 属性来开启远程搜索的功能,通过 `remote-method` 属性指定了远程搜索的方法名 `remoteSearch`。在 `remoteSearch` 方法中,你可以发起异步请求,根据用户输入的 `query` 参数获取匹配的数据,并将结果赋值给 `options` 数组。同时,我们使用了 `loading` 属性来展示加载状态。 这样,当用户在 Select 控件中输入内容时,会触发 `remoteSearch` 方法进行远程搜索,并根据搜索结果动态更新下拉选项。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值