当前位置:   article > 正文

ant-design-vue cascader级联选择省、市、区_ant vue design 选择省市区

ant vue design 选择省市区

默认返回值格式:all:code、name都返回 name:只返回name code:只返回code,level:可设置显示层级 1: 省  2: 省、市 3: 省、市、区

v-model 默认值 可以是 name: [ "天津市", "天津市", "和平区" ] code: [ "120000", "120100", "120101" ]

all:[ { "code": "120000", "name": "天津市" }, { "code": "120100", "name": "天津市" }, { "code": "120101", "name": "和平区" } ]

1、数据来源  area-data

npm i -s area-data

area-data 直辖市默认下级是市辖区 如:北京市->市辖区 若显示 北京市->北京市 需找到安装的包将数据里的市辖区改为相应的直辖市名称

2、自定义级联组件 al-cascader.vue

  1. <template>
  2. <div>
  3. <a-cascader
  4. v-model="select"
  5. :options="data"
  6. :load-data="loadData"
  7. change-on-select
  8. :disabled="disabled"
  9. :size="size"
  10. :placeholder="placeholder"
  11. :display-render="renderFormat"
  12. @change="handleChange"
  13. />
  14. </div>
  15. </template>
  16. <script>
  17. import { pcaa } from 'area-data'
  18. import util from '@/util'
  19. export default {
  20. name: 'AlCascader',
  21. props: {
  22. value: {
  23. type: Array,
  24. default: () => [],
  25. },
  26. level: {
  27. type: [Number, String],
  28. default: 3,
  29. validator: (val) => {
  30. return util.checkLevel(parseInt(val))
  31. },
  32. },
  33. dataType: {
  34. type: String,
  35. default: 'all',
  36. validator: (val) => {
  37. return util.oneOf(val, util.dataType)
  38. },
  39. },
  40. disabled: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. size: {
  45. type: String,
  46. default: 'default',
  47. },
  48. placeholder: {
  49. type: String,
  50. default: '请选择',
  51. },
  52. renderFormat: {
  53. type: Function,
  54. default: ({ labels, selectedOptions }) => labels.join(' / '),
  55. },
  56. },
  57. data() {
  58. return {
  59. data: [],
  60. select: [],
  61. oldData: [],
  62. }
  63. },
  64. computed: {
  65. showLevel() {
  66. return parseInt(this.level)
  67. },
  68. },
  69. watch: {
  70. value(val) {
  71. if (this.canEmit(this.value)) this.setDefaultValue()
  72. },
  73. showLevel() {
  74. this.init()
  75. this.setDefaultValue()
  76. },
  77. dataType() {
  78. this.setDefaultValue()
  79. },
  80. },
  81. mounted() {
  82. this.init()
  83. if (this.canEmit(this.value)) this.setDefaultValue()
  84. },
  85. methods: {
  86. init() {
  87. const { showLevel } = this
  88. const proData = []
  89. for (const p in pcaa['86']) {
  90. const children = []
  91. if (showLevel > 1)
  92. for (const c in pcaa[p]) {
  93. const _children = []
  94. if (showLevel > 2)
  95. for (const co in pcaa[c]) {
  96. const v = { value: co, label: pcaa[c][co] } // 县区级
  97. _children.push(v)
  98. }
  99. const v = { value: c, label: pcaa[p][c] } // 市级
  100. if (_children.length > 0) v.children = _children
  101. children.push(v)
  102. }
  103. const proitem = { value: p, label: pcaa['86'][p] }
  104. if (children.length > 0) proitem.children = children
  105. proData.push(proitem)
  106. }
  107. this.data = proData
  108. },
  109. setDefaultValue() {
  110. const { value, showLevel } = this
  111. if (value[0]) {
  112. let select = []
  113. if (isNaN(parseInt(value[0]))) {
  114. let i = 0
  115. let code = '' // 编码
  116. while (value[i] && i <= this.showLevel) {
  117. if (i === 0)
  118. code = util.getIndex(pcaa['86'], value[0].name || value[0])
  119. else code = util.getIndex(pcaa[code], value[i].name || value[i])
  120. select.push(code)
  121. i++
  122. }
  123. } else select = value
  124. select = select.slice(0, showLevel)
  125. this.select = select
  126. const res = this.procesValue(this.select)
  127. this.oldData = res
  128. this.$emit('input', res)
  129. this.$emit('on-change', res)
  130. }
  131. },
  132. canEmit(res) {
  133. let ifEmit = false
  134. const { value, oldData } = this
  135. if (value && value.length !== 0) {
  136. const v = value[value.length - 1]
  137. const o = oldData[oldData.length - 1]
  138. if (typeof res[0] === 'string') ifEmit = v !== o
  139. else if (oldData.length === 0 || v.code !== o.code) ifEmit = true
  140. } else ifEmit = true
  141. return ifEmit
  142. },
  143. handleChange(resArr) {
  144. const res = this.procesValue(resArr)
  145. this.oldData = res
  146. this.$emit('input', res)
  147. this.$emit('on-change', res)
  148. },
  149. // 遗弃
  150. loadData(select) {
  151. const childData = []
  152. const target = select[select.length - 1]
  153. const currentLevel = select.length
  154. target.loading = true
  155. const key = target.value
  156. const childs = pcaa[key]
  157. setTimeout(() => {
  158. for (const c in childs) {
  159. const isLeaf = currentLevel >= this.showLevel - 1
  160. const childitem = {
  161. value: c,
  162. label: pcaa[key][c],
  163. isLeaf,
  164. loading: false,
  165. }
  166. childData.push(childitem)
  167. }
  168. target.children = childData
  169. target.loading = false
  170. this.data = [...this.data]
  171. }, 100)
  172. },
  173. procesValue(arr) {
  174. let i = 0
  175. const res = []
  176. while (arr[i]) {
  177. const name = i === 0 ? pcaa['86'][arr[i]] : pcaa[arr[i - 1]][arr[i]]
  178. let item
  179. switch (this.dataType) {
  180. case 'all':
  181. item = { code: arr[i], name }
  182. break
  183. case 'name':
  184. item = name
  185. break
  186. default:
  187. item = arr[i]
  188. break
  189. }
  190. res.push(item)
  191. i++
  192. }
  193. return res
  194. },
  195. },
  196. }
  197. </script>

util

  1. const util = {}
  2. util.levelArr = [1, 2, 3]
  3. util.oneOf = (item, arr) => {
  4. return arr.some((i) => {
  5. return i === item
  6. })
  7. }
  8. util.getIndex = (list, name) => {
  9. for (const i in list) {
  10. if (list[i] === name) return i
  11. }
  12. }
  13. util.dataType = ['all', 'code', 'name']
  14. util.checkLevel = (val) => {
  15. return util.oneOf(val, util.levelArr)
  16. }
  17. export default util
  1. import AlCascader from '@/components/select-area/al-cascader'
  2. //在需要使用的地方引入组件
  3. <al-cascader v-model="resArr" level="2" data-type="all" />

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号