• element + vue3,级联选择器实现省市区


     由于es6支持哈希,所以数据量只要不太大,就不需要对el-cascader进行点击后在调接口出现下一级,很简单的就是直接获取所有数据。

    1. <script lang="ts" setup>
    2. import { ref, reactive, toRaw, onMounted, watch, onBeforeUnmount } from 'vue'
    3. import type { CascaderProps } from 'element-plus'
    4. import * as api from "./api.js";
    5. const props = defineProps({
    6. modelValue: {
    7. type: Array,
    8. default: []
    9. },
    10. // 是否支持清空选项
    11. clearable: {
    12. type: Boolean,
    13. default: true,
    14. },
    15. // 是否多选
    16. // multiple: {
    17. // type: Boolean,
    18. // default: false,
    19. // },
    20. // 指定某省,传入该自治区的areacode
    21. designation: {
    22. type: String,
    23. default: '',
    24. },
    25. // 仅显示最后一级
    26. showAllLevels: {
    27. type: Boolean,
    28. default: true,
    29. },
    30. // 是否禁用
    31. disabled: {
    32. type: Boolean,
    33. default: false,
    34. },
    35. // 输入框占位文本
    36. placeholder: {
    37. type: String,
    38. default: '',
    39. },
    40. // 是否选择任意一级选项,还是最后叶子结点
    41. checkStrictly: {
    42. type: Boolean,
    43. default: true,
    44. },
    45. // 次级菜单的展开方式,click和hover
    46. expandTrigger: {
    47. type: String,
    48. default: 'click',
    49. },
    50. // 多选个数限制selectNum
    51. multipleSelectNum: {
    52. type: Number,
    53. default: 1,
    54. },
    55. // 省市县乡镇,显示层级,默认为2,到区县。0为省,1为市,2为区县,3为乡镇
    56. level: {
    57. type: Number,
    58. default: 2,
    59. },
    60. })
    61. let options: any = ref([]) //树
    62. const watchArr = ref([])
    63. onMounted(() => {
    64. // 获取全量数据
    65. getAllData()
    66. })
    67. //重新定义一个值来接受prop
    68. const isMultiple = ref(false)
    69. //因为prop中的值非动态响应,所以需要通过watch监听,immediate 初始化时接收父组件中的传值
    70. watch(() => props.multipleSelectNum, () => {
    71. // isMultiple.value = props.multipleSelectNum;
    72. if(props.multipleSelectNum > 1) {
    73. isMultiple.value = true
    74. }
    75. console.log(isMultiple.value,props.multipleSelectNum,'---props.multipleSelectNum;');
    76. }, {
    77. immediate: true
    78. })
    79. onBeforeUnmount(() => {})
    80. const emit = defineEmits(['update:modelValue', 'change'])
    81. const handleChange = (e: any, e2: any) => {
    82. watchArr.value = e
    83. emit('change', e)
    84. emit('update:modelValue', e)
    85. }
    86. async function getAllData() {
    87. let res = await api.SearchAdministrativeDivisions({
    88. queryArea: props.designation,
    89. level: props.level
    90. })
    91. options.value = res.data
    92. }
    93. function onFocus(e: any) { }
    94. async function onBeforeFilter(e: any) {
    95. let res = await api.SearchAdministrativeDivisions({
    96. queryArea: e
    97. })
    98. const fn = (arr: any[]) => {
    99. arr.forEach((item, index) => {
    100. if (item.children && item.children.length) {
    101. item.children = fn(item.children)
    102. } else {
    103. if (item.children) delete item.children
    104. }
    105. })
    106. return arr
    107. }
    108. // console.log(options, '---options');
    109. options.value = fn(res.data)
    110. }
    111. // const isMultiple = ref(false)
    112. const innerProps: CascaderProps = {
    113. checkStrictly: props.checkStrictly,
    114. // lazy: true,
    115. value: 'areaCode',
    116. label: 'name',
    117. multiple: isMultiple.value, //modify
    118. expandTrigger: props.expandTrigger,
    119. }
    120. const handleClose = () => { }
    121. const onBlur = () => {
    122. // console.log('---onBlur');
    123. }
    124. const onVisibleChange = (e: any) => {
    125. // if (props.modelValue.length < props.multipleSelectNum && props.multiple == true) {
    126. if (props.modelValue.length < props.multipleSelectNum && isMultiple.value == true) { //modify
    127. function onRabbit(list: any) {
    128. list.forEach((el: any) => {
    129. if (el.children && el.children.length > 0) {
    130. el.children = onRabbit(el.children);
    131. }
    132. el.disabled = false;
    133. el.isFlag = false
    134. });
    135. return list;
    136. }
    137. const newList = onRabbit(options.value);
    138. options.value = newList;
    139. }
    140. }
    141. // 监听
    142. watch(
    143. () => watchArr.value,
    144. (newVal, oldVal) => {
    145. // if (props.multiple == true) {
    146. if (isMultiple.value == true) { //modify
    147. // console.log(oldVal,`watch监听变化前的数`)
    148. // console.log(newVal, `watch监听变化后的数据`)
    149. if (newVal.length >= props.multipleSelectNum) {
    150. function onRabbit(list: any) {
    151. list.forEach((el: any) => {
    152. if (el.children && el.children.length > 0) {
    153. el.children = onRabbit(el.children);
    154. }
    155. newVal.forEach((v: any) => {
    156. if (v[v.length - 1] != el.areaCode) {
    157. if (el.isFlag) {
    158. } else {
    159. el.disabled = true
    160. el.isFlag = true
    161. }
    162. } else {
    163. el.disabled = false
    164. el.isFlag = true
    165. }
    166. })
    167. });
    168. return list;
    169. }
    170. const newList = onRabbit(options.value);
    171. options.value = newList;
    172. } else {
    173. function onRabbit(list: any) {
    174. list.forEach((el: any) => {
    175. if (el.children && el.children.length > 0) {
    176. el.children = onRabbit(el.children);
    177. }
    178. el.disabled = false;
    179. el.isFlag = false
    180. });
    181. return list;
    182. }
    183. const newList = onRabbit(options.value);
    184. options.value = newList;
    185. }
    186. }
    187. },
    188. {
    189. immediate: true, // 立即执行
    190. deep: true // 深度监听
    191. }
    192. )
    193. script>
    194. <style>style>

  • 相关阅读:
    【考研数据结构代码题】day11-day20
    python+vue+elementui高校学生成绩补考通知系统django
    uniAPP小程序 子组件使用watch不生效,H5正常,小程序不正常(其实是子组件model选项的问题)
    Python(一)关键字、内置函数
    基于HTML美食餐饮文化项目的设计与实现 HTML学生网页设计作业 计算机毕业设计 HTML+CSS+JavaScript
    WorkPlus AI助理,基于ChatGPT的企业级知识问答机器人
    JDK8 ThreadPoolExecutor 线程池源码深度解析(附几种线程池的扩展方式)
    java计算机毕业设计ssm智慧农贸信息化管理平台(源码+系统+mysql数据库+Lw文档)
    【JavaEE重点知识归纳】第5节:方法
    QGIS编译(跨平台编译)之五十:Linux环境下安装Python、pyqt5、pyqt5-tools等
  • 原文地址:https://blog.csdn.net/irisMoon06/article/details/134511792