codeSelect.vue
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* 使用方式 <code-select v-model="params.enableFlag" dictname="ENABLE_FLAG" @changed="handler" placeholder="启用状态" style="width: 120px;"/>
* 根据/data/basecode.js中字典值,生成下拉列表
* @property dictname ENABLE_FLAG
* @property placeholder
* @property style
*/
<template>
<el-select :name="inputName" v-model="selectValue" :disabled="disabled" :placeholder="placeholder" :style="mystyle" clearable class="filter-item code-selected" @change="selectChange" @visible-change="drowShow">
<el-option v-for="(item,index) in optionList" :key="index" :label="item[label]" :value="item[valWord]" />
</el-select>
</template>
<script>
import request from '@/api/axios'
import { getStorageItem } from '@/utils/storage'
export default {
props: {
value: {
type: [String, Number],
default: ''
},
//用localStrage中的basecode生成下拉
dictname: {
type: String,
default: ''
},
//远程请求,生成下拉
remoteurl: {
type: String,
default: ''
},
remoteParams: {
type: Object,
default: ()=>{}
},
inputName: {
type: String,
default: ''
},
placeholder: {
type: String,
default: '请选择'
},
mystyle: {
type: String,
default: 'width: 100px'
},
disabled: {
type: Boolean,
default: false
},
label:{
type: String,
default: 'label'
},
valWord:{
type: String,
default: 'value'
},
},
data () {
return {
selectValue: '',
optionList: [],
dictList: [],
}
},
watch: {
value:{
handler(newValue, oldValue){
if (typeof newValue === 'string') {
this.selectValue = newValue
} else {
this.selectValue = this.parseString(newValue);
}
},
immediate: true
},
},
computed: {},
created () {
if (this.dictname !== '') {
this.optionList = this.getListFromBaseCode()
}
if (this.remoteurl !== '') {
this.getListFromAjax()
}
},
mounted () {},
methods: {
getListFromBaseCode() {
let basecode = getStorageItem('queryForCodeSelect')
let list = []
if (!basecode.hasOwnProperty(this.dictname)) {
return []
}
this.dictList = basecode[this.dictname]
for (let i = 0; i < this.dictList.length; i++) {
var codeItem = this.dictList[i]
list.push({ 'value': codeItem.value.toString(), 'label': codeItem.label })
}
return list
},
getListFromAjax() {
//将url中的参数转换成json,提交
var reqData = {};
/*var params = this.remoteurl.slice(this.remoteurl.indexOf('?') + 1).split('&');
for (var i = 0; i < params.length; i++) {
var map = params[i].split('=');
var key = map[0];
var val = map[1];
if(','.indexOf(val)>0 && val.split(',').length>1){
val = val.split(',');
}
reqData[map[0]] = val;
}*/
if(this.remoteParams != null){
reqData = this.remoteParams;
}
request({
url: this.remoteurl,
method: 'post',
data: reqData
}).then(response => {
if (response.repCode == '0000') {
this.optionList = response.repData
}
})
},
selectChange (val) {
this.$emit('input', val)
var list = this.optionList
for (var i in list) {
var item = list[i]
if (item[this.valWord] === val) {
this.$emit('changed', item)
break
}
}
},
drowShow (val) {
this.$emit('show', val)
this.$emit('click', val)
}
}
}
</script>
<!--<style rel="stylesheet/scss" lang="less" scoped>-->
<!--</style>-->