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
| <template>
| <el-select :disabled="disabled" :clearable="clearable" v-model="selectValue" :value-key="idField" :multiple="multiple" :placeholder="placeholder" @change="onSelected" style="width: 100%">
| <el-option v-for="data in dataList" :key="data[idField]" :label="data[textField]" :value="data[idField]"></el-option>
| </el-select>
| </template>
| <script>
|
| export default {
| name: 'ZtSelect',
| components: {},
| props: {
| value: [String, Array],
| idField: {
| type: String,
| default: 'id'
| },
| textField: {
| type: String,
| default: 'name'
| },
| url: String,
| datas: Array,
| multiple: {
| type: Boolean,
| default: false
| },
| clearable: {
| type: Boolean,
| default: false
| },
| disabled: {
| type: Boolean,
| default: false
| },
| disabledFilter: Function, // 可选的节点
| placeholder: String
| },
| data() {
| return {
| dataList: [],
| selectValue: this.getValue(this.value)
| }
| },
| watch: {
| value(val, oldval) {
| this.selectValue = this.getValue(val)
| },
| url(val, oldval) {
| this.queryDataList(val)
| },
| datas(val, oldval) {
| this.setDataList(val)
| }
| },
| mounted() {
| // console.log(this.datas, 'this.datas in zt-select')
| if (this.datas !== undefined && this.datas.length > 0) {
| // console.log('use datas')
| this.setDataList(this.datas)
| } else if (this.url !== undefined) { // 指定了url
| // console.log('use url')
| this.queryDataList(this.url)
| }
| },
| methods: {
| getValue(val) {
| if (this.multiple) { // 多选
| if (val) {
| return Array.isArray(val) ? val : val.split(',')
| } else {
| return []
| }
| } else { // 单选
| return typeof val === 'undefined' ? '' : (val + '')
| }
| },
| async queryDataList(url) {
| let res = await this.$http.get(url, null)
| this.setDataList(res.data)
| },
| setDataList(data) { // 设置树的值
| if (Array.isArray(data)) {
| this.dataList = data
| } else {
| this.dataList = data.rows
| }
| },
| onSelected(data) {
| if (this.multiple) { // 多选
| this.$emit('input', this.selectValue)
|
| let selected = this.dataList.filter(d => this.selectValue.indexOf(d[this.idField] + '') >= 0)
| this.$emit('change', selected)
| } else { // 单选
| this.$emit('input', this.selectValue)
| let selected = this.dataList.filter(d => this.$equalsIgnoreType(data, d[this.idField]))
| if (selected.length > 0) {
| this.$emit('change', selected[0])
| }
| }
| }
| }
| }
| </script>
| <style lang="scss">
|
| </style>
|
|