当前位置:   article > 正文

elementui 级联选择--全国区域选择_elementui地区级联选择

elementui地区级联选择
  1. <template>
  2. <el-cascader
  3. v-model="areaList"
  4. style="width:100%"
  5. ref="areaTree"
  6. :props="areaProps"
  7. clearable
  8. :options="data"
  9. @change="changeArea"
  10. />
  11. </template>
  12. <script>
  13. import pcasCode from "@/assets/json/pcas-code.json" // 非全国
  14. import pcasCode2 from "@/assets/json/pcas-code2.json" // 全国
  15. import axios from "axios";
  16. export default {
  17. props: {
  18. value: [Array, Object],
  19. //是否可选择任意节点
  20. checkStrictly: {
  21. type: Boolean,
  22. default: false
  23. },
  24. noRoot: {
  25. type: Boolean,
  26. default: false
  27. },
  28. //最多查询层级 4可到街道 和noRoot相关
  29. levelLimit: {
  30. type: Number,
  31. default: 4
  32. }
  33. },
  34. watch: {
  35. value: {
  36. immediate: true,
  37. handler (val) {
  38. if (val) {
  39. let list = []
  40. console.log(val);
  41. val.forEach(item => {
  42. let arr = []
  43. let { nationwide, provinceCode, cityCode, areaCode, streetCode } = item
  44. if (!this.noRoot && nationwide) {
  45. arr.push(nationwide)
  46. }
  47. if (provinceCode) {
  48. arr.push(provinceCode)
  49. }
  50. if (cityCode) {
  51. arr.push(cityCode)
  52. }
  53. if (areaCode) {
  54. arr.push(areaCode)
  55. }
  56. if (streetCode) {
  57. arr.push(streetCode)
  58. }
  59. list.push(arr)
  60. })
  61. this.areaList = list;
  62. this.$forceUpdate();
  63. } else {
  64. this.areaList = []
  65. this.$forceUpdate();
  66. }
  67. },
  68. deep: true
  69. },
  70. },
  71. data () {
  72. return {
  73. data: this.noRoot ? pcasCode : pcasCode2,
  74. areaList: [],
  75. areaProps: {
  76. value: "code",
  77. label: "name",
  78. expandTrigger: "click",
  79. lazy: false,
  80. multiple: true,
  81. checkStrictly: this.checkStrictly,
  82. lazyLoad: (node, resolve) => {
  83. const { level } = node;
  84. let parentArea = "";
  85. if (this.noRoot || level !== 0) {
  86. parentArea = node.value;
  87. this.getArea(parentArea, level === this.levelLimit).then((res) => {
  88. resolve(res);
  89. });
  90. } else {
  91. let r = {
  92. areaCode: "410000",
  93. areaName: '全国',
  94. leaf: level === this.levelLimit,
  95. }
  96. //必须异步 不然无法触发多级请求 恶心死我
  97. setTimeout(() => {
  98. resolve([r]);
  99. })
  100. }
  101. },
  102. },
  103. }
  104. },
  105. methods: {
  106. getArea (code, isLeaf) {
  107. return new Promise((resolve, reject) => {
  108. axios
  109. .get("/amap/v3/config/district", {
  110. params: {
  111. keywords: "650000",
  112. subdistrict: 3,
  113. key: "aff1e25d9a37616eff95250ce75a48f9",
  114. },
  115. })
  116. .then((res) => {
  117. let data = res.data.districts[0].districts.map((item) => {
  118. let adcode
  119. if (this.levelLimit === (this.noRoot ? 3 : 4)) {
  120. adcode = item.level === "street" ? item.name : item.adcode
  121. } else {
  122. adcode = item.adcode
  123. }
  124. return {
  125. areaCode: adcode,
  126. areaName: item.name,
  127. leaf: isLeaf,
  128. };
  129. });
  130. resolve(data);
  131. });
  132. });
  133. },
  134. changeArea (val) {
  135. if (!val.length) {
  136. this.$emit("change", [])
  137. return
  138. }
  139. if (this.noRoot) {
  140. this.$nextTick(() => {
  141. let list = []
  142. val.forEach((item, index) => {
  143. let [provinceCode, cityCode, areaCode, streetCode] = item
  144. let obj = {
  145. provinceCode,
  146. cityCode,
  147. areaCode,
  148. streetCode,
  149. fullAreaText: this.getfullAreaText(index)
  150. }
  151. list.push(obj)
  152. });
  153. this.$emit("change", list)
  154. });
  155. } else {
  156. this.$nextTick(() => {
  157. let list = []
  158. val.forEach((item, index) => {
  159. let [nationwide, provinceCode, cityCode, areaCode, streetCode] = item
  160. let obj = {
  161. nationwide,
  162. provinceCode,
  163. cityCode,
  164. areaCode,
  165. streetCode,
  166. fullAreaText: this.getfullAreaText(index)
  167. }
  168. list.push(obj)
  169. });
  170. this.$emit("change", list)
  171. });
  172. }
  173. },
  174. //返回地区文本
  175. getfullAreaText (index) {
  176. // this.$nextTick(() => {
  177. let str = this.$refs.areaTree.presentTags[index].text;
  178. if (str == "全国") {
  179. return str.replaceAll(" / ", "")
  180. } else {
  181. return str.replaceAll(" / ", "").replace("全国", "") || ""
  182. }
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .el-cascader {
  189. max-height: 300px;
  190. overflow: scroll;
  191. &::-webkit-scrollbar {
  192. width: 0;
  193. }
  194. }
  195. ::v-deep .el-cascader__tags {
  196. top: 0;
  197. transform: translateY(0);
  198. }
  199. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/107491
推荐阅读
相关标签
  

闽ICP备14008679号