当前位置: 首页 > news >正文

vue省市区懒加载,用el-cascader 新增和回显

el-cascader对于懒加载有支持方法,小难点在于回显的时候,由于懒加载第一次只有一层,所以要根据选中id数组一层层的加载。

子组件

<template>
  <el-cascader
    ref="cascaderRef"
    v-model="selectedValue"
    :props="CascaderProps"
    :disabled="disabled"
    style="width: 500px"
  />
</template>
  
<script setup>
import { ref, onMounted, defineEmits } from "vue";
import { ElCascader } from "element-plus";
import { listByParent, listParentByChild } from "@/api/common.js";
const cascaderRef = ref();
const props = defineProps({
  modelValue: [String, Number],
  disabled: {
    type: Boolean,
    default: false,
  },
});
const selectedValue = ref([]); // 选中的id数组

const emit = defineEmits(["update"]); // 更新父组件的值
const CascaderProps = {
  lazy: true, // 懒加载
  checkStrictly: true, // 选择任意一项
  lazyLoad(node, resolve) {
    const { level, pathValues } = node;
    listByParent({
      adiId: node.value,
    }).then((res) => {
      const nodes = res.data.map((item) => {
        return {
          ...item,
          label: item.name,
          value: item.adiId,
          leaf:
            (pathValues[0] == "330000000000" && level == 4) ||
            (pathValues[0] != "330000000000" && level == 2)
              ? true
              : false, // 浙江地区层级和别的省层级不一样,true表示最后一层
        };
      });
      resolve(nodes);
    });
  },
};
// 回显逻辑
onMounted(async () => {
  selectedValue.value = []; // 重置
  setTimeout(async () => {
    // 延迟执行
    if (props.modelValue) {
      // 存在值
      let ids = await listParentByChild({
        type: 1,
        childAdiId: props.modelValue,
      }); // 获取父级id数组
      ids = ids.data.map((item) => item.adiId);
      selectedValue.value = ids.reverse(); // 调换顺序,省市区顺序
      await resolveCascaderNodes(selectedValue.value);
    }
  }, 1000);
});

// 递归解析路径
const resolveCascaderNodes = async (ids) => {
  const nodes = [];
  let currentParent = "";
  // 加载子节点路径
  for (const id of ids) {
    const children = await listByParent(currentParent);
    const target = children.data.find((item) => item.id === id);
    if (target) {
      nodes.push(target);
      currentParent = id;
    }
  }
  return nodes;
};
// 监听值变化
watch(selectedValue, (val) => {
  if (val.length) emit("update", val[val.length - 1]);
});
</script>

父组件

                  <AreaCascader
                    :key="new Date().getTime()"
                    :disabled="disabled"
                    :modelValue="form.concatAddress"
                    @update="update"
                  />

key是为了每次使用都刷新,不加不会每次调用onMounted里的方法
form.concatAddress的值是最后一个区划id,在子组件里面要用接口获取到完整的id数组

import AreaCascader from "@/components/Cascader/index.vue";

const update = (val) => {
  form.value.concatAddress = val;
};

记录一下

http://www.dtcms.com/a/108204.html

相关文章:

  • 多模态大模型笔记
  • Compressed串行端口终端应用程序(MAC 、WIN、LINUX)打包下载
  • 高级java每日一道面试题-2025年3月19日-Web篇-防止表单重复提交的方法有哪些?
  • MySQL联合查询
  • vector的学习使用(1)
  • Cjson的创建和解析
  • 【Python】KNN:K-NearestNeighbor 学习指南
  • Vue3+Cesium+vite 入门- 项目搭建
  • HAL库 通过USB Boot进行APP程序升级
  • window11 通过cmd命令行安装 oh my zsh 的教程
  • VMware上的windows虚拟机安装使用Docker方法
  • MySQL篇(二): 核心知识深度聚簇解析:索引、非聚簇索引、回表查询、覆盖索引、超大分页处理、索引创建原则与索引失效场景
  • TDengine 权限管理与安全配置实战(二)
  • Redhat8.10 离线安装Snipe-IT v8.0.4 版本
  • 计算机网络中科大 - 第1章 结构化笔记(详细解析)
  • PostgreSQL pg_repack 重新组织表并释放表空间
  • NumPy的应用
  • 【数据结构】图的基本概念
  • 基于Django框架的基金数据可视化平台(源码+lw+部署文档+讲解),源码可白嫖!
  • 客户机用vscode连接局域网内主机
  • springboot去读yml配置文件中的属性值
  • LLM大模型学习系列——总纲
  • 瑞数信息发布《BOTS自动化威胁报告》,揭示AI时代网络安全新挑战
  • 深入解析 Java 8 Function 接口:函数式编程的核心工具
  • react 15-16-17-18各版本的核心区别、底层原理及演进逻辑的深度解析--react17
  • Windows下部署AgentGPT
  • C/C++与JavaScript的WebAssembly编程(一)
  • RNN模型及NLP应用(5/9)——多层RNN、双向RNN、预训练
  • js防抖函数防抖无效的解决方法
  • 14.网络套接字TCP