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,湖北分公司宜昌中心"]
}}