想用react实现一个递归树,但一些框架里面的有些不符合需求,于是自己写了个,功能比较简单,欢迎批评指正。。

react实现这样一个组织架构递归树,下级部门的收起和展开,点击部门名称时请求接口获取下级部门以及员工等。效果如下:

首先封装左边的组织架构组件organize-tree

import React, { Component } from 'react';
export default class OrganizeTree extends React.Component { render(){
let self = this;
return (
<div className="OrganizeTree">
<div className="left">
{
this.props.treedata.map(function(item,index){
return <div className="row-li" key={index}>
<span className={`${item.show?'':'rotate90'}`}>
<i className={`iconfont arrow ${item.departs?'icon-arrowdropdown':''}`} onClick={self.toggle.bind(self,item,index)}></i>
</span>
<span className={`depart ${item.departs?'':'departmentname'}`} onClick={self.showdepart.bind(self,item,index)}>{item.departmentName}</span>
{
item.departs&&item.show?<OrganizeTree treedata={item.departs} toggleTree={self.toggle.bind(self,item.departs)} showDepart={self.showdepart.bind(self,item.departs)}/>:''
}
</div>
})
}
</div>
</div>
)
} toggle(item,index){ // 点击左侧切换符号切换展开收起
if(typeof index === 'number'){
this.props.toggleTree(item)
}else{
this.props.toggleTree(index)
}
} showdepart(item,index){
if(typeof index === 'number'){
this.props.showDepart(item)
}else{
this.props.showDepart(index)
} } }

样式tree.less

.Organization{ // 组织架构树组件
.left-tree{width: 200px;background: #fff;margin: 15px 0 15px 15px;min-height: 700px;
.search-wrapper{padding:20px 10px;position:relative;
.search{font-size: 12px;height: 30px;padding-left: 25px;width: 100%;line-height: 30px;border:1px solid #d8d8d8;}
.icon-sousuo{position: absolute;top: 25px;left: 15px;z-index:;color: #d8d8d8;}
}
.row-li{text-align: left;font-size: 12px;line-height: 35px;
.arrow{font-size: 30px;}
.departmentname{padding-left: 16px;}
.depart{cursor: pointer;}
.icon-arrow-right{font-size: 24px;}
}
}
.right-con{width: 100%;
.title{margin: 10px 0;
.iconfont{margin-right: 10px;}
}
.bt1p{border-bottom: 1px solid #d8d8d8;padding-bottom: 10px;}
}
}
.OrganizeTree{padding-left: 15px;}

页面引用该组件 organization.jsx

import React, { Component } from 'react';
import InterfaceServer from '@/axios/interface'
const interfaceServer = new InterfaceServer();
import OrganizeTree from '@/components/organize-tree'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import * as infoActions from '@/store/userinfo/action'
import store from '@/store/store'
import './tree.less' class Organization extends React.Component { constructor(props,context){
super(props,context)
this.state = {
treedata:[],
}
} render(){
let s = store.getState().organizeInfo
return(
<div className="Organization">
<div className="nav col666"><span>组织架构</span></div>
<div className="flex">
            <!--左边的组织架构树-->
<div className="left-tree">
<div className="search-wrapper">
<input type="text" className="input search" placeholder="请输入部门名称"/>
<i className="iconfont icon-sousuo"></i>
</div>
<OrganizeTree treedata={this.state.treedata} toggleTree={this.toggleTree.bind(this)} showDepart={this.showDepart.bind(this)}/>
</div>
            <!--右边的详情-->
<div className="right-con bgcon txtleft">
<div className="title"><i className="iconfont icon-zuzhi"></i>才华有限公司 <button className="btn">编辑</button></div>
<div className="title bt1p"><i className="iconfont icon-bumen"></i>下级部门 <button className="btn">增加</button></div>
{
s.department&&s.department.length?
<table className="table" border="0" cellPadding="0" cellSpacing="0" bordercolor="#eee">
<thead>
<tr>
<th>部门名称</th>
<th>部门主管</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{
s.department.map(function(item,index){
return <tr key={index}>
<td>{item.departmentName}</td>
<td>{item.supervisor}</td>
<td><a className="linka">编辑</a><a className="linka">删除</a></td>
</tr>
})
}
</tbody>
</table>
:<div>暂无下级部门</div>
}
<div className="title bt1p"><i className="iconfont icon-bumen1"></i>部门员工 <button className="btn">增加</button></div>
{
s.employees&&s.employees.length?
<table className="table" border="0" cellPadding="0" cellSpacing="0" bordercolor="#eee">
<thead>
<tr>
<th>姓名</th>
<th>职位名称</th>
<th>联系电话</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{
s.employees.map(function(item,index){
return <tr key={index}>
<td>{item.emName}</td>
<td>{item.poName}</td>
<td>{item.emPhone}</td>
<td><a className="linka">编辑</a><a className="linka">删除</a></td>
</tr>
})
}
</tbody>
</table>
:<div>暂无下级部门</div>
}
</div>
</div>
</div>
)
} componentWillMount(){
this._sendOrganizationServer()
console.log(store.getState().organizeInfo);
let organizeInfo = store.getState().organizeInfo
this._sendShowemployeeServer({departmentName:'才华有限公司'}) } _sendOrganizationServer(){
interfaceServer.sendOrganizationServer({
onSuccess:res=>{
console.log(res);
let result = res.data
result.forEach(item=>{
item.show = true;
if(item.departs){
item.departs.forEach(i=>{
i.show = true
})
}
})
this.setState({
treedata:result
}) }
})
} _sendShowemployeeServer(param){
interfaceServer.sendShowemployeeServer({
data:param,
onSuccess:res=>{
console.log(res);
// 保存到redux里
this.props.organizeInfoActions.saveOrganizeINFO(res.data)
}
})
} toggleTree(item,index){
item.show = !item.show;
this.setState({
treedata:this.state.treedata
})
} showDepart(item){
console.log(item);
this._sendShowemployeeServer({departmentName:item.departmentName})
}
} function mapStateToProps(state){
return {
organizeInfo: state.organizeInfo
}
} function mapDispatchToProps(dispatch){
return {
organizeInfoActions: bindActionCreators(infoActions, dispatch)
}
} export default connect(
mapStateToProps,
mapDispatchToProps
)(Organization)

更多详细代码见 https://github.com/leitingting08/react-app/tree/master/src/components/organize-tree

react封装组织架构递归树的更多相关文章

  1. Android一个炫酷的树状图组织架构图开源控件实现过程

    Android一个炫酷的树状图组织架构图开源控件 文章目录 [1 简介] [2 效果展示] [3 使用步骤] [4 实现基本布局流程] [5 实现自由放缩及拖动] [6 实现添加删除及节点动画] [7 ...

  2. ASP.NET MVC5+EF6+EasyUI 后台管理系统(41)-组织架构

    系列目录 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当初设计的项目解决方案就是可伸缩可以拆离,可共享的项目解决方案.所以我们同时要添加App.Flow文件夹 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...

  4. NetBpm 组织架构(4)

    大牛的杰作,赞一个 转自:NetBPM工作流的架构设计及实现浅析 读前的话:由于本文涉及内容颇多,若有地方读来不很明白,建议先跳过,整体上有个认识后,再回过头来理解.作者认识有限,若有错误,欢迎斧正: ...

  5. 使用jOrgChart插件实现组织架构图的展示

    项目要做组织架构图,要把它做成自上而下的树形结构. 一.说明 (1)通过后台查询数据库,生成树形数组结构,返回到前台. (2)需要引入的js插件和css文件: ①jquery.jOrgChart.cs ...

  6. js前端使用jOrgChart插件实现组织架构图的展示

    项目要做组织架构图,要把它做成自上而下的树形结构. 需要购买阿里云产品的,可以点击此链接购买,有红包优惠哦: https://promotion.aliyun.com/ntms/yunparter/i ...

  7. vue2-org-tree 基于VUE的部门组织架构组件,增删节点实现

    本文所用组件传送门:vue-org-tree 本文基于antd (其他前端组件框架操作基本都类似的: iview,elementui,boostrap-vue...) 当然,github上还有其他类似 ...

  8. 重学 Java 设计模式:实战迭代器模式「模拟公司组织架构树结构关系,深度迭代遍历人员信息输出场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 相信相信的力量! 从懵懂的少年,到拿起键盘,可以写一个Hell ...

  9. Atittit.研发公司的组织架构与部门架构总结

    Atittit.研发公司的组织架构与部门架构总结 1. archi组织架构与 部门规划2 1.1. 最高五大组织机构2 1.2. 宗教事务部2 1.3. 制度与重大会议委员会2 1.4. 纠纷处理部: ...

随机推荐

  1. python操作mysql数据库实现增删改查

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...

  2. redis sentinels哨兵集群环境配置

    # Redis configuration file example. # # Note that in order to read the configuration file, Redis mus ...

  3. C#WinForm窗体事件执行次序

    当 Windows Form 应用程序启动时,会以下列顺序引发主要表单的启动事件:         System.Windows.Forms.Control.HandleCreated         ...

  4. SpringMVC------maven编译报错:Dynamic Web Module 3.0 requires Java 1.6 or newer

    如图所示: 但是 Eclipse 明明已经将编译级别设置为 1.7: 这是由于你的 Maven 编译级别是 jdk1.5 或以下,而你导入了 jdk1.6 以上的依赖包:查看 Eclipse 的 Na ...

  5. 使用webbench做压力测试

    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.下文介绍的是在Ubu ...

  6. Python对象(上)

    Python是一门面向对象的语言,在Python中一切都是对象,函数是对象,类型也是对象. 下面就看看Python中对象相关的内容. Python对象基本概念 Python对象有三个基本的要素: 身份 ...

  7. 【安全开发】C/C++安全编码规范

    C本质上是不安全的编程语言.例如如果不谨慎使用的话,其大多数标准的字符串库函数有可能被用来进行缓冲区攻击或者格式字符串攻击.但是,由于其灵活性.快速和相对容易掌握,它是一个广泛使用的编程语言.下面是针 ...

  8. PDF XSS

    漏洞测试: 下面,我们介绍如何把 JavaScript 嵌入到 PDF 文档之中.我使用的是迅捷 PDF 编辑器未注册版本 1.启动迅捷 PDF 编辑器打开一个 PDF 文件,或者使用“创建 PDF ...

  9. Android 监听apk安装替换卸载广播

    首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程 ...

  10. vc 关于局部刷新

    在绘制图像对象的时候,时刻获取其所占范围大小,并使用InvalidateRect( m_rectRefresh);刷新,但是光这样还是不行的要在onDraw()函数里获取PAINTSTRUCT结构的无 ...