一、OrgChart下载地址:
https://github.com/dabeng/OrgChart
二、组织架构的表设计
CREATE TABLE `org_info` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '组织ID',`name` varchar(36) NOT NULL DEFAULT '' COMMENT '组织名称',`content` varchar(256) NOT NULL DEFAULT '' COMMENT '组织内容',`pid` int(11) NOT NULL DEFAULT '0' COMMENT '父级ID',`path` varchar(256) NOT NULL DEFAULT '' COMMENT '父级路径(逗号分割)',PRIMARY KEY (`id`),KEY `path` (`path`)) ENGINE=MyISAM AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 COMMENT='组织信息表';
三、index.html代码如下:
四、data.php代码如下:
name = 'xxx有限公司'; $obj->children = $data; echo json_encode($obj);} else if($act == 'orgFrm') { //处理组织表单 $addOrgAct = !empty($_POST['addOrgAct']) ? trim($_POST['addOrgAct']) : ''; //判断表单方法是否为空 if(empty($addOrgAct)) { exit; } //当前节点名 $curNodeName = !empty($_POST['curNodeName']) ? trim($_POST['curNodeName']) : ''; //当前节点ID $curNodeId = !empty($_POST['curNodeId']) ? trim($_POST['curNodeId']) : 0; //组织名称 $orgName = !empty($_POST['orgName']) ? trim($_POST['orgName']) : ''; if($addOrgAct == 'add') { $addOrgType = !empty($_POST['addOrgType']) ? trim($_POST['addOrgType']) : ''; if(empty($orgName)) { exit; } if($addOrgType == 'sib') { //添加兄弟节点 if(empty($curNodeId)) { exit; } //获取兄弟节点的pid和path $sql = "SELECT `pid`,`path` FROM `org_info` WHERE `id`={$curNodeId}"; $ret = mysql_query($sql); $row = mysql_fetch_assoc($ret); $path = $row['path']; $pid = $row['pid']; //添加节点 $sql = "INSERT INTO `org_info`(`name`, `pid`, `path`) VALUES('{$orgName}', {$pid}, '{$path}')"; mysql_query($sql); $nodeId = mysql_insert_id(); if(mysql_affected_rows()) { $obj = new StdClass(); $obj->name = $orgName; $obj->id = $nodeId; echo json_encode($obj); exit; } } else { //添加子节点 //获取当前节点的path $sql = "SELECT `path` FROM `org_info` WHERE `id`={$curNodeId}"; $ret = mysql_query($sql); $row = mysql_fetch_assoc($ret); $path = $row['path']; $path = !empty($path) ? (rtrim($path, ',') . ',' . $curNodeId) : $curNodeId; $pid = $curNodeId; //添加节点 $sql = "INSERT INTO `org_info`(`name`, `pid`, `path`) VALUES('{$orgName}', {$pid}, '{$path}')"; mysql_query($sql); $nodeId = mysql_insert_id(); if(mysql_affected_rows()) { $obj = new StdClass(); $obj->name = $orgName; $obj->id = $nodeId; echo json_encode($obj); exit; } } } else if($addOrgAct == 'del') { //删除所有有父节点中有该节点的子节点 $sql = "DELETE FROM `org_info` WHERE FIND_IN_SET('{$curNodeId}',`path`) OR `id`={$curNodeId}"; mysql_query($sql); if(mysql_affected_rows()) { $obj = new StdClass(); $obj->name = $orgName; $obj->id = $curNodeId; echo json_encode($obj); exit; } } else if($addOrgAct == 'edt') { if(empty($orgName)) { exit; } //更新节点 $sql = "UPDATE `org_info` SET `name`='{$orgName}' WHERE `id`={$curNodeId}"; mysql_query($sql); if(mysql_affected_rows()) { $obj = new StdClass(); $obj->name = $orgName; $obj->id = $curNodeId; echo json_encode($obj); exit; } }}
五、效果如下: