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

实战 element-plus 级联选择器(Cascader)+企微部门架构

先看效果 :

1、部门架构数据来源于企业微信;

2、部门层级关系的展现和勾选;

具体实现:

1、组件官方文档

http://element-plus.org/zh-CN/component/cascader.html

级联选择器组件要求的数据格式:

 

重点:了解清楚数据格式

 2、后端写法(php)

   // 递归查询负责的子部门(给 elementplus 组件用的)public static function getSubdepartments2($sub_id, $all_departments) {$output = array();foreach ($all_departments as $department) {if ($department['parent_id'] == $sub_id) {$arr = ['value'=>$department['id'],'label'=>$department['name']];// 继续判断是否有下一级$subdepartments = self::getSubdepartments2($department['id'], $all_departments);if (!empty($subdepartments)) {// 返回所有子部门// $arr['children'] = $subdepartments;// 只返回两级$arr[] = $subdepartments;}$output[] = $arr;}}return $output;}public function department($id=194){$row = Department::where('parent_id',$id)->field('id as value,name as label')->select();$alldeptlist = Department::select();foreach($row as &$val){// 查询是否有子部门$children = self::getSubdepartments2($val['value'],$alldeptlist);if(!empty($children)){$val['children'] = $children;}}if($row){$this->success('查询成功',$row);}else{$this->error('找不到数据!');}}

递归方法说明

另外一个注意点:

递归有可能返回空数组,可能会出现“空级联”的 bug,如下面这位老铁的文章提及到的:

那么我们在调用递归结果时,只需要判断是否为空(如上图 348 行代码),空就不要添加 children 对象了。


http://www.mrgr.cn/news/14338.html

相关文章:

  • 从json字符串中获取指定值
  • Windows 平台编译openssl3.3
  • 全自动饲料机整套设备:养殖业生产线利器
  • 深入理解Spring Security
  • 数学建模学习(128):使用Python结合CILOS与熵法的多准则决策权重确定
  • pytorch 数据处理
  • pytorch中的__init__()与super__init__()方法
  • 文件包含漏洞(1)
  • PostgreSQL:后端开发者的瑞士军刀
  • 计算机毕业设计选题推荐-在线音乐网站-音乐专辑商城-Java/Python项目实战
  • 这些持续高额派息的公司,都做对了什么?
  • 微信小程序:点击事件(bindtap)传递参数
  • 云端Ruby:将应用无缝融入云服务的艺术
  • Ubuntu 24.04部署Wordpress
  • 大模型之二十八-语音识别Whisper进阶
  • python常见报错总结
  • shutil库学习之copy和copy2函数
  • Docker常见命令和参数
  • oracle日常训练
  • 什么是激光测距仪