element中el-cascader使用小结

表单结构

<el-cascader
	  ref="myCascader"
	  v-model="value"
	  :options="options"
	  placeholder="请选择地区范围"
	  style="width: 100%"
	  :props="{
	    expandTrigger: 'hover',
	    multiple: true,
	    value: 'id'
	  }"
	  filterable
	  clearable
	  collapse-tags
	  @change="handleRegin"
></el-cascader>

options绑定的数据结构

  options: [
    {
      id: "zujian",
      label: "组件",
      children: [
        {
          id: "basic",
          label: "Basic",
          children: [
            {
              id: "layout",
              label: "Layout 布局",
              children: []
            },
            {
              id: "color",
              label: "Color 色彩",
              children: []
            },
            {
              id: "typography",
              label: "Typography 字体",
              children: []
            },
            {
              id: "icon",
              label: "Icon 图标",
              children: []
            },
            {
              id: "button",
              label: "Button 按钮",
              children: []
            }
          ]
        },
        {
          id: "form",
          label: "Form",
          children: [
            {
              id: "radio",
              label: "Radio 单选框",
              children: []
            },
            {
              id: "checkbox",
              label: "Checkbox 多选框",
              children: []
            },
            {
              id: "input",
              label: "Input 输入框",
              children: []
            },
            {
              id: "input-number",
              label: "InputNumber 计数器",
              children: []
            },
            {
              id: "select",
              label: "Select 选择器"
            },
            {
              id: "cascader",
              label: "Cascader 级联选择器"
            }
          ]
        },
        {
          id: "data",
          label: "Data",
          children: [
            {
              id: "table",
              label: "Table 表格"
            },
            {
              id: "tag",
              label: "Tag 标签"
            },
            {
              id: "progress",
              label: "Progress 进度条"
            },
            {
              id: "tree",
              label: "Tree 树形控件"
            },
            {
              id: "pagination",
              label: "Pagination 分页"
            },
            {
              id: "badge",
              label: "Badge 标记"
            }
          ]
        },
      ]
    },
    {
      id: "ziyuan",
      label: "资源",
      children: [
        {
          id: "axure",
          label: "Axure Components"
        },
        {
          id: "sketch",
          label: "Sketch Templates"
        },
        {
          id: "jiaohu",
          label: "组件交互文档"
        }
      ]
    }
  ]

重点地方 必坑点
options结构中子级的children为空时,会发现@change="handleRegin"方法无法触发,选择不上数据值
解决方案
将后台返回的数据进行判断,空数组的children进行删除操作;重新返回组件数据结构

let a = function(child) {
  return child.map(i => {
     if (i.children.length) {
       i.children = a(i.children);
     } else {
       delete i.children;
     }
     return i;
   });
};
this.cascader_options = a(res.data);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-cascader是Element Plus的一个组件,它支持懒加载功能。懒加载是指在用户选择某个节点时,动态加载该节点的子节点,以减少初始加载的数据量,提高页面性能。 要实现el-cascader的懒加载功能,需要满足以下前提条件: 1. 后端需要提供要选的节点在树的完整路径。 2. 后端需要提供根据节点ID查询下一级菜单的懒加载接口。 具体实现步骤如下: 1. 在el-cascader组件,设置lazy属性为true,表示开启懒加载功能。 2. 在el-cascader组件,设置lazyLoad方法,用于根据节点ID加载下一级菜单的数据。 3. 在lazyLoad方法,通过调用后端提供的懒加载接口,传入节点ID,获取该节点的子节点数据。 4. 将获取到的子节点数据返回给el-cascader组件,实现路径菜单的懒加载。 以下是一个示例代码,演示了如何使用el-cascader实现懒加载功能: ```html <template> <el-cascader v-model="selectedNodes" :options="options" :lazy="true" :lazyLoad="lazyLoad" ></el-cascader> </template> <script> export default { data() { return { selectedNodes: [], // 选的节点 options: [] // 树形菜单数据 }; }, methods: { lazyLoad(node, resolve) { // 调用后端懒加载接口,获取子节点数据 // 根据节点ID查询下一级菜单的懒加载接口 // 示例接口:/api/getChildren?id=xxx axios.get(`/api/getChildren?id=${node.id}`).then(response => { // 将获取到的子节点数据返回给el-cascader组件 resolve(response.data); }); } } }; </script> ``` 请注意,上述示例的接口路径和参数需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值