级联下拉多选,只展示路径名称,数据处理

文章描述了一个JavaScript函数`getSelectedNames`,用于在给定的数据源选项中,根据用户选择的ID路径获取所有匹配的完整层级名称。它通过递归遍历一个多级菜单结构,实现了根据选择的值查找并返回相应的全路径名称。
摘要由CSDN通过智能技术生成

ids:多选每项最后一级的id
options:数据源
curId:全路径的id
curAllName:全路径名称
pid:父级id

getSelectedNames = (ids, options) => {
   const names = []
   const findNames = (ids, options, pid = '', curId = '', curAllName = '') => {
	   options.forEach(node => {
		   node.pid = pid
		   node.curId = curId + (curId ? '/' : '') + node.value
		   node.curAllName = curAllName + (curAllName ? '/' : '') + node.label
		   if (ids.includes(node.value)) {
		     names.push(node.curAllName)
		   }
		   if (node.children && node.children.length) {
		     findNames(ids, node.children, node.value, node.curId, node.curAllName);
		   }
		});
	};
	findNames(ids, options);
	return names;
}

数据源

options: [{
  value: 'zhinan',
  label: '指南',
  children: [{
    value: 'shejiyuanze',
    label: '设计原则',
    children: [{
      value: 'yizhi',
      label: '一致'
    }, {
      value: 'kekong',
      label: '可控'
    }]
  }, {
    value: 'daohang',
    label: '导航',
    children: [{
      value: 'cexiangdaohang',
      label: '侧向导航'
    }, {
      value: 'dingbudaohang',
      label: '顶部导航'
    }]
  }]
}, {
  value: 'zujian',
  label: '组件',
  children: [{
    value: 'basic',
    label: 'Basic',
    children: [{
      value: 'layout',
      label: 'Layout 布局'
    }, {
      value: 'color',
      label: 'Color 色彩'
    }, {
      value: 'button',
      label: 'Button 按钮'
    }]
  }, {
    value: 'form',
    label: 'Form',
    children: [{
      value: 'radio',
      label: 'Radio 单选框'
    }, {
      value: 'checkbox',
      label: 'Checkbox 多选框'
    },{
      value: 'form',
      label: 'Form 表单'
    }]
  }, {
    value: 'data',
    label: 'Data',
    children: [{
      value: 'table',
      label: 'Table 表格'
    }, {
      value: 'tag',
      label: 'Tag 标签'
    }, {
      value: 'badge',
      label: 'Badge 标记'
    }]
  }, {
    value: 'notice',
    label: 'Notice',
    children: [{
      value: 'alert',
      label: 'Alert 警告'
    }, {
      value: 'loading',
      label: 'Loading 加载'
    },{
      value: 'notification',
      label: 'Notification 通知'
    }]
  }, {
    value: 'navigation',
    label: 'Navigation',
    children: [{
      value: 'menu',
      label: 'NavMenu 导航菜单'
    }, {
      value: 'steps',
      label: 'Steps 步骤条'
    }]
  }, {
    value: 'others',
    label: 'Others',
    children: [{
      value: 'dialog',
      label: 'Dialog 对话框'
    },{
      value: 'carousel',
      label: 'Carousel 走马灯'
    }, {
      value: 'collapse',
      label: 'Collapse 折叠面板'
    }]
  }]
}, {
  value: 'ziyuan',
  label: '资源',
  children: [{
    value: 'axure',
    label: 'Axure Components'
  }, {
    value: 'jiaohu',
    label: '组件交互文档'
  }]
}],
value: ['kekong','radio','dialog','loading'],

调用

const val = getSelectedNames(this.options, this.value)
console.log(val)
//'指南/设计原则/可控,组件/Form/Radio 单选框,组件/Others/Dialog 对话框,组件/Notice/Loading 加载'
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值