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
| <template>
| <div class="address-cascader">
| <el-cascader
| v-model="adress"
| filterable
| :props="adressProp"
| @change="changeAdress"
| :options="adressArray"
| :disabled ="isDisabled"
| :clearable="clearable"
| ></el-cascader>
| <slot></slot>
| </div>
| </template>
| <script>
| import addressResourcesApi from '@/api/addressResources'
| export default {
| props: {
| isDisabled: {
| type: Boolean,
| default: false
| },
| adressArr: { // 传入绑定值
| type: Array,
| default: function() {
| return []
| }
| },
| adressProp: {
| type: Object,
| default: function() {
| return {}
| }
| },
| /**
| * 是否在子组件内请求接口,默认请求
| */
| isRequest: {
| type: Boolean,
| default: true
| },
| /**
| * 外部传过来的地址数组
| */
| allAdress: { // 传入绑定值
| type: Array,
| default: function() {
| return []
| }
| },
| // 是否可清除数据
| clearable: {
| type: Boolean,
| default: true
| }
| },
| watch: {
| adressArr: {
| handler(newValue, oldValue) {
| this.adress = newValue
| },
| immediate: true
| },
| adress(newValue, oldValue) {
| this.$emit('update:adressArr', newValue)
| },
| allAdress(newValue, oldValue) {
| if (newValue.length) {
| this.adressArray = newValue
| }
| }
| },
| data() {
| return {
| adressArray: [], // 区域数组
| adress: [], // 绑定数据
| adressText: [], // 绑定的中文
| tag: true
| }
| },
| created() {
| if (this.isRequest) {
| this.getAdressArray()
| }
| },
| methods: {
| changeAdress(val) {
| this.adressText = []
| if (val && val.length) {
| if (this.adressProp) {
| val.forEach(item => {
| if (Array.isArray(item)) {
| this.adressText.push(this.getCascaderObj(item, this.adressArray))
| }
| })
| }
| this.$emit('hand-adress-validate', val)
| } else {
| this.adress = []
| this.adressText = []
| }
| },
| /**
| * 获取级联label
| */
| getCascaderObj(val, opt) {
| return val.map(function(value, index, array) {
| for (var itm of opt) {
| if (itm.value === value) { opt = itm.children; return itm.label }
| }
| return null
| })
| },
| /**
| * 获取省市区数据
| */
| getAdressArray() {
| addressResourcesApi.getList().then(res => {
| if (res.data) {
| this.adressArray = res.data
| }
| })
| }
| }
|
| }
| </script>
| <style lang="scss">
| .address-cascader{
| .el-cascader{
| .el-cascader__tags{
| max-height: 200px;
| overflow-y: auto;
| width: 85%;
| padding-right: 3px;
| &::-webkit-scrollbar {display:none}
| }
| }
| }
| </style>
|
|