xianniu-ui 0.3.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xianniu-ui",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "private": false,
5
5
  "main": "lib/xianniu-ui.umd.min.js",
6
6
  "scripts": {
@@ -1,17 +1,15 @@
1
1
  <template>
2
2
  <!-- 城市联动 -->
3
3
  <div class="xn-city">
4
- <span v-if="showType === 'text'">{{ cityLabel }}</span>
5
4
  <el-cascader
6
- v-else-if="showType === 'form'"
7
5
  ref="xnCity"
8
6
  v-model="cityValue"
9
7
  placeholder="请选择城市"
10
8
  filterable
9
+ v-bind="propsConf()"
10
+ v-on="$listeners"
11
11
  style="width: 100%"
12
12
  :options="cityList"
13
- :props="cityProps"
14
- :disabled="disabled"
15
13
  clearable
16
14
  @change="handleChange"
17
15
  />
@@ -19,19 +17,15 @@
19
17
  </template>
20
18
 
21
19
  <script>
22
- import citys from 'xn-ui/src/area/index.js'
23
- // import tools from '../../../utils/index'
20
+ const ZXCITY = ['北京市','天津市','上海市','重庆市']
21
+ import citys from "xn-ui/src/area/index.js";
24
22
  export default {
25
- name: 'XnCity',
23
+ name: "XnCity",
26
24
  model: {
27
- prop: 'value',
28
- event: 'on-change'
25
+ prop: "value",
26
+ event: "on-change",
29
27
  },
30
28
  props: {
31
- disabled: {
32
- type: Boolean,
33
- default: false
34
- },
35
29
  /**
36
30
  * 传入对应的城市code
37
31
  * 区级 -> 省|市|区
@@ -40,196 +34,180 @@ export default {
40
34
  */
41
35
  value: {
42
36
  type: [String, Number, Object],
43
- default: ''
37
+ default: "",
44
38
  },
45
39
  valueKey: {
46
40
  type: String,
47
- default: ''
48
- },
49
- /**
50
- * 显示级别
51
- * 3 -> 省|市|区
52
- * 2 -> 省|市
53
- */
54
- showLevel: {
55
- type: Number,
56
- default: 3
41
+ default: "",
57
42
  },
58
43
  /**
59
44
  * 组件类型
60
45
  * 静态显示 -> text
61
46
  * 表单类型 -> form
62
47
  */
63
- showType: {
64
- type: String,
65
- default: 'form'
66
- },
67
48
  dataLevel: {
68
49
  type: Number,
69
- default: 3
50
+ default: 3,
70
51
  },
71
52
  keyOptions: {
72
53
  type: Object,
73
54
  default: () => {
74
55
  return {
75
- codeKey: 'code',
76
- labelKey: 'label'
77
- }
78
- }
79
- }
56
+ codeKey: "code",
57
+ labelKey: "label",
58
+ };
59
+ },
60
+ },
61
+ propConfig: {
62
+ type: Object,
63
+ default: () => {
64
+ return {};
65
+ },
66
+ },
80
67
  },
81
68
  data() {
82
69
  return {
83
70
  // "430000", "430200", "430203"
84
71
  val: [],
85
72
  cityList: [],
86
- cityProps: {
87
- label: 'cityName',
88
- value: 'cityCode',
89
- children: 'subCitys'
73
+ flattenResult: [],
74
+ propKey: {
75
+ props: {
76
+ label: "cityName",
77
+ value: "cityCode",
78
+ children: "subCitys",
79
+ },
90
80
  },
91
- flattenResult: []
92
- }
81
+ };
93
82
  },
94
83
  computed: {
95
- // 根据code 获取对应的父级省市区label
96
- cityLabel() {
97
- const { value, showLevel, showType, valueKey } = this
98
- let res = '--'
99
- let _value = ''
100
- valueKey ? _value = value[valueKey] : _value
101
- if (showType === 'text' && value !== '') {
102
- // if (!value) throw new Error("静态模式下请传入cityCode!");
103
- const level = showLevel > 3 ? 3 : showLevel
104
- res = this.findParent(_value, this.flattenResult)
105
- .slice(0, level)
106
- .map((item) => item.cityName)
107
- .join(` | `)
108
- }
109
- return res
110
- },
111
84
  cityValue: {
112
- set: function(n) {
113
- this.val = n
85
+ set: function (n) {
86
+ this.val = n;
114
87
  },
115
- get: function() {
116
- const { value, valueKey } = this
117
- const _value = valueKey ? value[valueKey] : value
118
- const res = this.findParent(
119
- _value,
120
- this.flattenResult
121
- )
122
- .map((item) => item.cityCode)
123
-
124
- return res
125
- }
126
- }
88
+ get: function () {
89
+ const { value, valueKey } = this;
90
+ const _value = valueKey ? value[valueKey] : value;
91
+ const res = this.findParent(_value, this.flattenResult).map(
92
+ (item) => item.cityCode
93
+ );
94
+ return res;
95
+ },
96
+ },
127
97
  },
128
98
  created() {
129
- this.fnGetCitys()
99
+ this.fnGetCitys();
130
100
  },
131
101
  methods: {
102
+ propsConf() {
103
+ let obj = this.propKey;
104
+ if (this.$attrs.props) {
105
+ obj.props = Object.assign(this.$attrs.props, this.propKey.props);
106
+ }
107
+ return Object.assign(this.$attrs, obj);
108
+ },
132
109
  // 为所有数据添加父级pid
133
110
  addParentKey(tree) {
134
- const data = JSON.parse(JSON.stringify(tree)) // deepClone
111
+ const data = JSON.parse(JSON.stringify(tree)); // deepClone
135
112
  function addParentKey(data, parentKey) {
136
113
  data.forEach((item) => {
137
- const { subCitys, cityCode } = item
138
- item.parent = parentKey
114
+ const { subCitys, cityCode } = item;
115
+ item.parent = parentKey;
139
116
  if (subCitys) {
140
- addParentKey(subCitys, cityCode)
117
+ addParentKey(subCitys, cityCode);
141
118
  }
142
- })
119
+ });
143
120
  }
144
- addParentKey(data, null) // 一开始的时候是null
145
- return data
121
+ addParentKey(data, null); // 一开始的时候是null
122
+ return data;
146
123
  },
147
124
  /**
148
125
  * @param {array} data 城市联动数据(带pid)
149
126
  */
150
127
  flattenTreeData(data) {
151
- const treeData = JSON.parse(JSON.stringify(data))
152
- const flattenData = []
128
+ const treeData = JSON.parse(JSON.stringify(data));
129
+ const flattenData = [];
153
130
  function flattenTree(data, parentKey) {
154
131
  data.forEach((item) => {
155
- const { cityName, cityCode, subCitys } = item
156
- flattenData.push({ cityName, cityCode, parentKey })
132
+ const { cityName, cityCode, subCitys } = item;
133
+ flattenData.push({ cityName, cityCode, parentKey });
157
134
  if (subCitys) {
158
- flattenTree(subCitys, cityCode)
135
+ flattenTree(subCitys, cityCode);
159
136
  }
160
- })
137
+ });
161
138
  }
162
- flattenTree(treeData, null)
163
- return flattenData
139
+ flattenTree(treeData, null);
140
+ return flattenData;
164
141
  },
165
142
  /**
166
143
  * @param {string,number} cityCode 区县对应的code
167
144
  * @param {array} flattenTree 扁平化后的城市数据
168
145
  */
169
- findParent(cityCode, flattenTree) {
170
- const parentArr = []
171
- function find(cityCode, flattenTree) {
146
+ findParent(cityCode, flattenTree, typeKey = "cityCode") {
147
+ const parentArr = [];
148
+ function find(cityCode, flattenTree, typeKey) {
172
149
  flattenTree.forEach((item) => {
173
150
  // eslint-disable-next-line eqeqeq
174
- if (item.cityCode == cityCode) {
175
- parentArr.unshift(item)
176
- find(item.parentKey, flattenTree)
151
+ if (item[typeKey] == cityCode) {
152
+ parentArr.unshift(item);
153
+ find(item.parentKey, flattenTree, typeKey);
177
154
  }
178
- })
155
+ });
179
156
  }
180
- find(cityCode, flattenTree)
181
- return parentArr
157
+ find(cityCode, flattenTree, typeKey);
158
+
159
+ return parentArr;
182
160
  },
183
161
  // 接口获取城市联动数据
184
162
  fnGetCitys() {
185
- const _citys = this.$utils.deepClone(citys)
186
- this.cityList = this.toTreeDataLevel(_citys)
163
+ const _citys = this.$utils.deepClone(citys);
164
+ this.cityList = this.toTreeDataLevel(_citys);
187
165
  this.flattenResult = this.flattenTreeData(
188
166
  this.addParentKey(this.cityList)
189
- )
167
+ );
190
168
  },
191
169
  // 给数据加上level并且去除最后一层空数据
192
170
  toTreeDataLevel(data) {
193
- if (!Array.isArray(data)) return []
171
+ if (!Array.isArray(data)) return [];
194
172
  const recursive = (data, level = 0) => {
195
- level++
173
+ level++;
196
174
  return data.map((item) => {
197
- item.level = level
198
- const child = item.subCitys
175
+ item.level = level;
176
+ const child = item.subCitys;
199
177
  if (level >= 2 && this.dataLevel === 2) {
200
- delete item.subCitys
178
+ delete item.subCitys;
201
179
  }
202
180
  if (child && child.length) {
203
- recursive(child, level)
181
+ recursive(child, level);
204
182
  } else {
205
- delete item.subCitys
183
+ delete item.subCitys;
206
184
  }
207
- return item
208
- })
209
- }
210
- return recursive(data)
185
+ return item;
186
+ });
187
+ };
188
+ return recursive(data);
211
189
  },
212
190
  // 获取选中
213
191
  handleChange(cityCode) {
214
192
  // 返回数组形式 code和label
215
- const cityName = this.handleTreeLabel(cityCode, this.cityList)
216
- const city = this.handleTreeLabel(cityCode, this.cityList, 2)
217
- const cityCodeLast = cityCode[cityCode.length - 1]
218
- const cityNameLast = cityName[cityName.length - 1]
219
- let value = ''
193
+ const cityName = this.handleTreeLabel(cityCode, this.cityList);
194
+ const city = this.handleTreeLabel(cityCode, this.cityList, 2);
195
+ const cityCodeLast = cityCode[cityCode.length - 1];
196
+ const cityNameLast = cityName[cityName.length - 1];
197
+ let value = "";
220
198
  if (this.valueKey) {
221
- value = city[city.length - 1]
199
+ value = city[city.length - 1];
222
200
  } else {
223
- value = cityCodeLast
201
+ value = cityCodeLast;
224
202
  }
225
- this.$emit('on-change', value)
226
- this.$emit('on-city', {
203
+ this.$emit("on-change", value);
204
+ this.$emit("on-city", {
227
205
  city,
228
206
  cityCode,
229
207
  cityName,
230
208
  cityCodeLast,
231
- cityNameLast
232
- })
209
+ cityNameLast,
210
+ });
233
211
  },
234
212
  /**
235
213
  * 根据城市code 获取对应的城市
@@ -241,18 +219,54 @@ export default {
241
219
  for (var itm of data) {
242
220
  // eslint-disable-next-line eqeqeq
243
221
  if (itm.cityCode == item) {
244
- data = itm.subCitys
222
+ data = itm.subCitys;
245
223
  return type === 1
246
224
  ? itm.cityName
247
225
  : {
248
- [this.keyOptions.codeKey]: itm.cityCode,
249
- [this.keyOptions.labelKey]: itm.cityName
250
- }
226
+ [this.keyOptions.codeKey]: itm.cityCode,
227
+ [this.keyOptions.labelKey]: itm.cityName,
228
+ };
251
229
  }
252
230
  }
253
- return null
254
- })
255
- }
256
- }
257
- }
231
+ return null;
232
+ });
233
+ },
234
+
235
+ // 方法二
236
+ // getCity(arr, data, city = []) {
237
+ // if (typeof data === "object") {
238
+ // for (let i = 0; arr[i] !== undefined; i++) {
239
+ // for (let j = 0; data[j] !== undefined; j++) {
240
+ // if (arr[i] === data[j].cityName) {
241
+ // city.push(data[j]);
242
+ // }
243
+ // }
244
+ // }
245
+ // for (let i = 0; data[i] !== undefined; i++) {
246
+ // this.getCity(arr, data[i].subCitys, city);
247
+ // }
248
+ // }
249
+ // return city;
250
+ // },
251
+ getCity(data, nameList) {
252
+ if (nameList.length === 0) return [];
253
+ const [cityName, ...rest] = nameList;
254
+ const item = data.find((i) => i.cityName === cityName || i.cityName.indexOf(cityName.substring(0,2)) > -1);
255
+ if(item){
256
+ return [item.cityCode, ...this.getCity(item.subCitys, rest)];
257
+ }else{
258
+ return [...this.getCity([], rest)]
259
+ }
260
+ },
261
+ str2Code(val) {
262
+ if (!val) return;
263
+ const cityArr = val.match(this.$reg.getCity) || [];
264
+ const newarr = cityArr.length&&cityArr.map((item,idx,arr)=>{
265
+ return ZXCITY.includes(item) && idx === 0 ? [item,...arr] : arr
266
+ })[0]
267
+ const arr = this.getCity(this.cityList, newarr);
268
+ return arr[arr.length - 1];
269
+ },
270
+ },
271
+ };
258
272
  </script>
@@ -108,7 +108,7 @@
108
108
  ></el-table-column>
109
109
  <el-table-column v-bind="$attrs" v-if="radio" width="40px" align="center">
110
110
  <template slot-scope="{ row }">
111
- <el-radio v-model="radioSelected" :label="row.id">&nbsp;</el-radio>
111
+ <el-radio v-model="radioSelected" :label="row[idKey]">&nbsp;</el-radio>
112
112
  </template>
113
113
  </el-table-column>
114
114
  <el-table-column
@@ -174,6 +174,10 @@ export default {
174
174
  showColumn: Boolean,
175
175
  expand: Boolean,
176
176
  isTools: Boolean,
177
+ idKey: {
178
+ type: String,
179
+ default: "id",
180
+ },
177
181
  },
178
182
  data() {
179
183
  return {
@@ -194,8 +198,9 @@ export default {
194
198
  },
195
199
  singleElection(val) {
196
200
  if (!this.radio) return;
197
- this.radioSelected = val.id;
198
- const res = this.data.filter((item) => item.id === val.id);
201
+ const { idKey } = this;
202
+ this.radioSelected = val[idKey];
203
+ const res = this.data.filter((item) => item[idKey] === val[idKey]);
199
204
  this.$emit("on-single", res);
200
205
  },
201
206
  handleToolsItem(row, index) {
@@ -214,9 +219,9 @@ export default {
214
219
  this.$refs.table.toggleRowSelection(row, status);
215
220
  },
216
221
  clearSelection() {
217
- if(this.radio){
218
- this.radioSelected = ''
219
- return
222
+ if (this.radio) {
223
+ this.radioSelected = "";
224
+ return;
220
225
  }
221
226
  this.$refs.table.clearSelection();
222
227
  },
package/src/utils/reg.js CHANGED
@@ -8,7 +8,8 @@ const Reg = {
8
8
  checkFullName: /^[\u4E00-\u9FA5A-Za-z0-9_]+$/, //中文、英文、数字包括下划线
9
9
  positiveInteger: /^[1-9]\d*$/, // 正整数
10
10
  twoDecimal: /(^[0-9]{1,20}$)|(^[0-9]{1,20}[.][0-9]{1,2}$)/, // 2位小数
11
- text: /^[a-z\d\u4E00-\u9FA5]+$/i // 不含特殊字符和标点
11
+ text: /^[a-z\d\u4E00-\u9FA5]+$/i, // 不含特殊字符和标点
12
+ getCity: /.+?(省|市|自治区|自治州|县|区)/g //获取地址中的省市区[省,市,区]
12
13
  }
13
14
 
14
15
  export default Reg