用js写一个组织架构选择人和选择部门的组件

时间: 2024-03-05 16:53:05 浏览: 14
好的,下面是一个用纯 JavaScript 实现的组织架构选择人和选择部门的组件: ```html <!-- HTML 结构 --> <div id="org-tree"></div> ``` ```javascript // JavaScript 代码 var orgTree = { // 组织架构数据 data: [ { id: "1", name: "总经理办公室", type: "department", children: [ { id: "11", name: "财务部", type: "department", children: [ { id: "111", name: "会计组", type: "department", children: [ { id: "1111", name: "张三", type: "user" }, { id: "1112", name: "李四", type: "user" } ] }, { id: "112", name: "出纳组", type: "department", children: [ { id: "1121", name: "王五", type: "user" } ] } ] }, { id: "12", name: "市场部", type: "department", children: [ { id: "121", name: "市场组", type: "department", children: [ { id: "1211", name: "赵六", type: "user" } ] } ] } ] } ], // 初始化组织架构树 init: function(element, onSelect) { // 保存当前选择的节点 this.currentNode = null; // 创建根节点 var rootNode = document.createElement("div"); rootNode.className = "org-tree-node root-node"; rootNode.innerHTML = '<span>组织架构</span>'; // 创建树容器 var treeContainer = document.createElement("div"); treeContainer.className = "org-tree"; treeContainer.appendChild(rootNode); // 递归创建子节点 this.createNode(this.data, rootNode, onSelect); // 将树容器插入到指定元素中 element.appendChild(treeContainer); }, // 创建节点 createNode: function(data, parentNode, onSelect) { for (var i = 0; i < data.length; i++) { var node = data[i]; // 创建节点元素 var nodeElement = document.createElement("div"); nodeElement.className = "org-tree-node"; nodeElement.setAttribute("data-id", node.id); nodeElement.setAttribute("data-type", node.type); nodeElement.innerHTML = "<span>" + node.name + "</span>"; // 添加点击事件 nodeElement.onclick = function() { // 获取节点 ID 和类型 var id = this.getAttribute("data-id"); var type = this.getAttribute("data-type"); // 如果当前节点已被选中,则取消选中 if (this.classList.contains("selected")) { this.classList.remove("selected"); orgTree.currentNode = null; onSelect && onSelect(null, null); } else { // 否则选中该节点,并将当前节点设置为选中节点 if (orgTree.currentNode) { orgTree.currentNode.classList.remove("selected"); } this.classList.add("selected"); orgTree.currentNode = this; onSelect && onSelect(id, type); } }; // 将节点元素插入到父节点中 parentNode.appendChild(nodeElement); // 如果节点有子节点,则递归创建子节点 if (node.children && node.children.length > 0) { var childrenNode = document.createElement("div"); childrenNode.className = "org-tree-children"; nodeElement.appendChild(childrenNode); this.createNode(node.children, childrenNode, onSelect); } } } }; ``` 使用方法: ```javascript // 选择部门 orgTree.init(document.getElementById("org-tree"), function(id, type) { if (type === "department") { console.log("选择了部门:" + id); } }); // 选择人员 orgTree.init(document.getElementById("org-tree"), function(id, type) { if (type === "user") { console.log("选择了人员:" + id); } }); ``` 这个组件会在指定的元素中创建一个组织架构树,支持选择部门和人员。当选择部门时,会返回部门的 ID;当选择人员时,会返回人员的 ID。您可以根据需要自行修改代码。

相关推荐

最新推荐

recommend-type

Vue.js递归组件实现组织架构树和选人功能

主要介绍了Vue.js递归组件实现组织架构树和选人功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

JS拖动选择table里的单元格完整实例【基于jQuery】

主要介绍了JS拖动选择table里的单元格,结合完整实例形式分析了基于jQuery的table表格动态操作相关实现技巧,涉及事件响应及页面元素属性动态操作使用方法,需要的朋友可以参考下
recommend-type

JS实现单击输入框弹出选择框效果完整实例

本文实例讲述了JS实现单击输入框弹出选择框效果的方法。分享给大家供大家参考,具体如下: 运行效果截图如下: 完整实例代码如下: &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
recommend-type

js判断选择的时间是否大于今天的代码

判断选择的时间是否大于今天还是蛮实用的,下面与大家分享下具体的实现,感兴趣的朋友可以参考下
recommend-type

js给网页加上背景音乐及选择音效的方法

主要介绍了js给网页加上背景音乐及选择音效的方法,涉及javascript操作音频的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。