element UI-远程搜索(el-select)

15 篇文章 0 订阅
12 篇文章 0 订阅

1.需求

表单项需具有搜索功能,并以下拉选的形式展现

2.实现

<template>
  <div>
    <div>
      <div class="content-top">
        <el-form style="width: 100%" :inline="true" :model="formResult" ref="formResult" hide-required-asterisk class="form-box" :rules="formRules">
          <ul class="flex-space-between" style="width: 100%">
            <li style="width: 322px; margin-left: 3px !important;">
              <el-form-item label="机构:" label-width="82px">
                <el-select
                  v-model="formResult.branchCom"
                  filterable
                  remote
                  clearable
                  reserve-keyword
                  placeholder="请输入关键词"
                  :remote-method="remoteMethod"
                  :loading="remoteLoading"
                  :disabled="isRemote"
                  @focus="repeatList"
                  @change="handleSelectBranchCom"
                >
                  <el-option
                    v-for="item in remoteOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </li>
          </ul>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'taskManage',
  data () {
    return {
      formResult: {
        branchCom: '', // 机构
        branchComCode: '' // 机构编码
      },
      formRules: {},
      remoteOptions: [], // 远程搜索列表
      remoteList: [], // 返回的远程搜索列表
      remoteLoading: false, // 是否正在从远程获取数据
      isRemote: false // 是否为远程搜索
    }
  },
  created () {
    this.getBranchCom() // 获取机构列表
  },
  methods: {
    // 获取机构列表
    getBranchCom () {
      console.log('获取机构,入参;this.branchComCode', this.formResult.branchComCode)
      this.$axios({
        url: '/branchcom/query',
        method: 'post',
        data: {
          'tradeMap': {
            'manageCom': '',
            'branchCom': '%'
          }
        }
      }).then(res => {
        console.log(res)
        let data = res.data
        if (data.tradeMap.resultCode === '00') {
          this.remoteList = data.tradeMap.comCode.map(item => {
            return {
              value: item,
              label: item.split(',')[1]
            }
          })
          console.log('this.remoteList')
          console.log(this.remoteList)
        }
      }).catch(() => {
        this.$message.error('系统异常')
      })
    },
    // 清空选择项
    repeatList () {
      this.remoteOptions = this.remoteList.filter(item => {
        return item.label
      })
    },
    // 选中机构时触发
    handleSelectBranchCom (item) {
      console.log('远程搜索选中后返回的item:::::即value的值')
      console.log(item)
      this.formResult.branchComCode = item.split(',')[0]
    },
    // 机构触发的远程搜索方法
    remoteMethod (query) {
      console.log(this.remoteList)
      if (query !== '') {
        this.remoteLoading = true
        setTimeout(() => {
          this.remoteLoading = false
          this.remoteOptions = this.remoteList.filter(item => {
            return item.value
              .indexOf(query) > -1
          })
          console.log('this.remoteOptions')
          console.log(this.remoteOptions)
        }, 200)
      } else {
        this.remoteOptions = []
      }
    }
  }
}
</script>

3.后端返回结果示例  

{errorList:[]
serviceNode:""
shardFlag:false
storageMap:{}
tradeMap:{
    message:"查询成功"
    resultCode:"00"
    comCode:["01,北京分公司", "02,湖北分公司", "0201,湖北分公司宜昌中心"]
}}

4.效果展示  

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值