1 <html> 2 <head> 3 <title>组织架构</title> 4 <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 5 <link href="jquery.ztree/style.css" rel="stylesheet" /> 6 <link href="jquery.ztree/zTreeStyle.css" rel="stylesheet" /> 7 <script src="jquery.ztree/jquery-1.4.4.min.js"></script> 8 <script src="jquery.ztree/jquery.ztree.core.js"></script> 9 <link href="jOrgChart-master/example/css/jquery.jOrgChart.css" rel="stylesheet" /> 10 <script src="jOrgChart-master/jquery.min.js"></script> 11 <script src="jOrgChart-master/example/jquery.jOrgChart.js"></script> 12 <style> 13 table { 14 margin: 0 auto; 15 } 16 a { 17 text-decoration: none; 18 color: #000000; 19 font-size: 12px; 20 } 21 .jOrgChart .node { 22 padding: 6px 1px 2px 5px; 23 border-radius: 3px; 24 width: 20px; 25 height: 100px; 26 /*line-height: 17px;*/ 27 letter-spacing: 3px; 28 border: 1px solid #726bf7; 29 /*width: 150px; 30 height: 50px; 31 line-height: 50px; 32 border-radius: 4px;*/ 33 /*margin: 0 8px;*/ 34 } 35 .tj { 36 color: #000000; 37 text-align: center; 38 font-weight: bold; 39 font-size: 12px; 40 border: 1px solid #726bf7; 41 border-radius: 3px; 42 } 43 44 </style> 45 </head> 46 <body> 47 <div class="float_affter"> 48 <div style="width:20%;height:100%; float: left; border-right: 1px solid #cac5c3; box-sizing: border-box; overflow: auto; "> 49 <ul id="treeDemo" class="ztree" style="margin-top:20px"></ul> 50 </div> 51 52 <div> 53 <table style="width:80%;height:50px; text-align:left; border-bottom : 1px solid #cac5c3;box-sizing: border-box;"> 54 <tr> 55 <td class="tj" onClick="jc_click()">基层工会:<label id="jcghs"></label></td> 56 <td class="tj" onClick="hf_click()">分会:<label id="fhs"></label></td> 57 <td class="tj onClick="hy_click()">会员:<label id="hys"></label></td> 58 <td class="tj">男:<label id="nxs"></label></td> 59 <td class="tj">女:<label id="vxs"></label></td> 60 61 62 </tr> 63 <tr> 64 <td class="tj">中专:<label id="zzs"></label></td> 65 <td class="tj">专科:<label id="zks"></label></td> 66 <td class="tj">本科:<label id="bks"></label></td> 67 <td class="tj">硕士研究生:<label id="shyjss"></label></td> 68 <td class="tj">博士研究生:<label id="bsyjss"></label></td> 69 <td class="tj">其他学历:<label id="qtxls"></label></td> 70 </tr> 71 </table> 72 <div id='jOrgChart' style="width:80%;margin-top:20px;float:left;"></div> 73 </div> 74 </div> 75 </body> 76 </html> 77 <script type="text/javascript"> 78 jQuery.noConflict(); 79 var zNodes; 80 var zTree; 81 //setting异步加载的设置 82 var setting = { 83 async: { 84 enable: true, //表示异步加载生效 85 url: "../Ajax/OrganizationalStructureAjax.ashx", // 异步加载时访问的页面 86 autoParam: ["id"], // 异步加载时自动提交的父节点属性的参数 87 otherParam: ["ajaxMethod", 'AnsyData'], //ajax请求时提交的参数 88 type: 'post', 89 dataType: 'json' 90 }, 91 checkable: true, 92 showIcon: true, 93 showLine: true, // zTree显示连接线 94 data: { //用pId来标识父子节点的关系 95 simpleData: { 96 enable: true 97 } 98 }, 99 expandSpeed: "", // 设置 zTree 节点展开、折叠的动画速度,默认为"fast",""表示无动画 100 callback: { // 回调函数 101 onClick: zTreeOnClick, // 单击鼠标事件 102 asyncSuccess: zTreeOnAsyncSuccess //异步加载成功事件 103 } 104 }; 105 106 $(document).ready(function () { 107 //初始化 108 Inint(); 109 jOrgChart(false); 110 $.fn.zTree.init($("#treeDemo"), setting, zNodes); 111 //openFirstTreenode(); 112 // alert(JSON.stringify(zNodes)); 113 }); 114 /* 115 * 初始化加载树形节点 116 * */ 117 function Inint(UNION_CODE) { 118 //$.ajax({ 119 // url: '../Ajax/OrganizationalStructureAjax.ashx', 120 // //url: 'test1.json', 121 // type: 'post', 122 // dataType: 'json', 123 // async: false, 124 // data: { 'ajaxMethod': 'FirstAnsyData' }, 125 // success: function (data) { 126 // zNodes = data; 127 128 // // alert(JSON.stringify(zNodes)); 129 // } 130 //}); 131 //初始化加载组织结构图 132 $.ajax({ 133 url: '../Ajax/OrganizationalStructureAjax.ashx', 134 type: 'post', 135 dataType: 'json', 136 async: false, 137 data: { 'ajaxMethod': 'Summary', 'OrgId': UNION_CODE}, 138 success: function (data) { 139 140 $("#jcghs").text(data.jcghs == "" ? 0 : data.jcghs); 141 $("#fhs").text(data.fhs == "" ? 0 : data.fhs); 142 $("#hys").text(data.hys == "" ? 0 : data.hys); 143 $("#nxs").text(data.nxs == "" ? 0 : data.nxs); 144 $("#vxs").text(data.vxs == "" ? 0 : data.vxs); 145 $("#zzs").text(data.zzs == "" ? 0 : data.zzs); 146 $("#zks").text(data.zks == "" ? 0 : data.zks); 147 $("#bks").text(data.bks == "" ? 0 : data.bks); 148 $("#shyjss").text(data.shyjss == "" ? 0 : data.shyjss); 149 $("#bsyjss").text(data.bsyjss == "" ? 0 : data.bsyjss); 150 $("#qtxls").text(data.qtxls == "" ? 0 : data.qtxls); 151 } 152 }); 153 154 155 }; 156 157 function jOrgChart(isshow) { 158 $.ajax({ 159 //树图根节点下次查询数据, 160 url: '../Ajax/OrganizationalStructureAjax.ashx', 161 type: 'post', 162 dataType: 'json', 163 async: false, 164 data: { 'ajaxMethod': 'GetjOrgChartJson', 'isshow': isshow }, 165 success: function (data) { 166 var showlist = $("<ul id='org' style='display:none'></ul>"); 167 showall(data, showlist); 168 jQuery("#jOrgChart").append(showlist); 169 jQuery("#org").jOrgChart({ 170 chartElement: '#jOrgChart',//指定在某个dom生成jorgchart 171 dragAndDrop: true //设置是否可拖动 172 }); 173 } 174 }); 175 EngthControl(); 176 177 178 179 180 // alert("一级"+d1+"二级" + d2+"三级" + d3); 181 } 182 //边框自适应 183 function EngthControl() { 184 185 //组织图三层数量 186 var d1 = $("#jOrgChart .node a.1").length; 187 var d2 = $("#jOrgChart .node a.2").length; 188 var d3 = $("#jOrgChart .node a.3").length; 189 //组织图方框大小根据字体个数变化 190 $("#jOrgChart .node a").each(function () { 191 //更改横竖显示 192 if ($(this).attr('class') == '1') { 193 $(this).parent().height(20); 194 $(this).parent().width($(this).text().length * 16); 195 } 196 if ($(this).attr('class') == '2') { 197 $(this).parent().height(20); 198 $(this).parent().width($(this).text().length * 16); 199 //if (d2 > 5) { 200 // return false; 201 // $(this).parents(".node-container:first").hide(); 202 //} 203 } 204 if ($(this).attr('class') == '3') { 205 206 $(this).parent().height($(this).text().length * 16); 207 $(this).parent().width(20); 208 } 209 //根据层级数据省略显示 210 211 //$(this).height($(this).text().length * 16); 212 }); 213 } 214 215 216 217 //单击时获取zTree节点的Id,和value的值 218 function zTreeOnClick(event, treeId, treeNode, clickFlag) { 219 //单机某个节点后展示组织结构图 220 //alert("单击" + treeNode.name + ",ID为" + treeNode.id); 221 222 $.ajax({ 223 url: '../Ajax/OrganizationalStructureAjax.ashx', 224 type: 'post', 225 dataType: 'json', 226 async: false, 227 data: { 'ajaxMethod': 'Summary', 'treeNodeID': treeNode.id }, 228 success: function (data) { 229 $("#jcghs").text(data.jcghs == "" ? 0 : data.jcghs); 230 $("#fhs").text(data.fhs == "" ? 0 : data.fhs); 231 $("#hys").text(data.hys == "" ? 0 : data.hys); 232 $("#nxs").text(data.nxs == "" ? 0 : data.nxs); 233 $("#vxs").text(data.vxs == "" ? 0 : data.vxs); 234 $("#zzs").text(data.zzs == "" ? 0 : data.zzs); 235 $("#zks").text(data.zks == "" ? 0 : data.zks); 236 $("#bks").text(data.bks == "" ? 0 : data.bks); 237 $("#shyjss").text(data.shyjss == "" ? 0 : data.shyjss); 238 $("#bsyjss").text(data.bsyjss == "" ? 0 : data.bsyjss); 239 $("#qtxls").text(data.qtxls == "" ? 0 : data.qtxls); 240 } 241 }); 242 $.ajax({ 243 url: '../Ajax/OrganizationalStructureAjax.ashx', 244 type: 'post', 245 dataType: 'json', 246 async: false, 247 data: { 'ajaxMethod': 'GetjOrgChartJson', 'treeNodeID': treeNode.id }, 248 success: function (data) { 249 jQuery("#jOrgChart").empty(); 250 var showlist = $("<ul id='org' style='display:none'></ul>"); 251 showall(data, showlist); 252 253 jQuery("#jOrgChart").append(showlist); 254 jQuery("#org").jOrgChart({ 255 chartElement: '#jOrgChart',//指定在某个dom生成jorgchart 256 dragAndDrop: true //设置是否可拖动 257 }); 258 } 259 }); 260 EngthControl(); 261 // var treeValue = treeNode.id + "," + treeNode.name; 262 //alert(treeNode.id + "," + treeNode.name); 263 }; 264 265 266 /** 267 * 展开树节点的第一层且关闭其他节点 268 */ 269 //function openFirstTreenode() { 270 // var tree = $.fn.zTree.getZTreeObj('treeDemo'); 271 // tree.expandAll(tree); 272 // // 获取树对象 273 // var treeObj = $.fn.zTree.getZTreeObj("treeDemo"); 274 // /* 获取所有树节点 */ 275 // var nodes = treeObj.transformToArray(treeObj.getNodes()); 276 // // 关闭所有节点 277 // //for (var i = 0, length_1 = nodes.length; i < length_1; i++) { 278 // // treeObj.expandNode(nodes[i], false);//第二个参数为false证明是折叠 279 // //} 280 // //展开第一级节点 281 // treeObj.expandNode(nodes[0], true);//第二个参数为true证明是展开 282 //} 283 284 function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) { 285 // alert("异步加载"); 286 } 287 function showall(menu_list, parent) { 288 (function (jQuery) { 289 jQuery.each(menu_list, function (index, val) { 290 if (val.childrens.length > 0) { 291 292 var li = $("<li></li>"); 293 li.append("<a href='javascript:void(0)' class='"+val.UNION_TYPE+"' οnclick=getOrgId('" + val.UNION_CODE + "','" + val.UNION_TYPE+"')>" + val.UNION_NAME + "</a>").append("<ul></ul>").appendTo(parent); 294 //递归显示 295 showall(val.childrens, $(li).children().eq(1)); 296 } else { 297 $("<li></li>").append("<a href='javascript:void(0)' class='" + val.UNION_TYPE +"' οnclick=getOrgId('" + val.UNION_CODE + "','" + val.UNION_TYPE +"')>" + val.UNION_NAME + "</a>").appendTo(parent); 298 } 299 }); 300 })(jQuery) 301 } 302 //点击节点后统计该节点数据 303 function getOrgId(UNION_CODE, UNION_NAME) { 304 if (UNION_CODE!="") { 305 $.ajax({ 306 url: '../Ajax/OrganizationalStructureAjax.ashx', 307 type: 'post', 308 dataType: 'json', 309 async: false, 310 data: { 'ajaxMethod': 'Summary', 'treeNodeID': UNION_CODE }, 311 success: function (data) { 312 313 $("#jcghs").text(data.jcghs == "" ? 0 : data.jcghs); 314 $("#fhs").text(data.fhs == "" ? 0 : data.fhs); 315 $("#hys").text(data.hys == "" ? 0 : data.hys); 316 $("#nxs").text(data.nxs == "" ? 0 : data.nxs); 317 $("#vxs").text(data.vxs == "" ? 0 : data.vxs); 318 $("#zzs").text(data.zzs == "" ? 0 : data.zzs); 319 $("#zks").text(data.zks == "" ? 0 : data.zks); 320 $("#bks").text(data.bks == "" ? 0 : data.bks); 321 $("#shyjss").text(data.shyjss == "" ? 0 : data.shyjss); 322 $("#bsyjss").text(data.bsyjss == "" ? 0 : data.bsyjss); 323 $("#qtxls").text(data.qtxls == "" ? 0 : data.qtxls); 324 } 325 }); 326 } 327 else { 328 //展示(遍历该节点下的数据) 329 330 //var htmlnode = "<td class='node-container' id='3' colspan='2'><table cellpadding='0' cellspacing='0' border='0'><tbody><tr class='node - cells'><td class='node - cell' colspan='2'><div class='node' style='height: 48px; width: 20px; '><a href='javascript: void (0)' class='3' οnclick='getOrgId('', '3')'>...</a></div></td></tr></tbody></table></td>"; 331 332 //$('.node-cells .node-cell .node a').find(function () { return this.classNmae == '3' && this.innerHTML == '...'; }).each(function () { 333 // $(this).parents('.node-container:first').after(htmlnode); 334 //}); 335 } 336 } 337 function jc_click() { 338 $("#org").remove(); 339 $(".jOrgChart").remove(); 340 jOrgChart(false); 341 } 342 function hf_click() { 343 $("#org").remove(); 344 $(".jOrgChart").remove(); 345 jOrgChart(true); 346 } 347 function hy_click() { 348 // var htmlline = "<td class='node-container' id='3' colspan='2'>< table cellpadding = '0' cellspacing = '0' border = '0' > <tbody><tr class='node - cells'><td class='node - cell' colspan='2'><div class='node' style='height: 48px; width: 20px; '><a href='javascript: void (0)' class='3' οnclick='getOrgId('', '3')'>...</a></div></td></tr></tbody></table ></td > "; 349 350 351 } 352 353 354 </script>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace LY.CCPCUEP.Model.UEP 7 { 8 public class OrganizationFramework 9 { 10 public string UNION_CODE { get; set; } 11 public string PARENT_ORGANIZATION_CODE { get; set; } 12 public string UNION_NAME { get; set; } 13 14 public string UNION_TYPE { get; set; } 15 public List<OrganizationFramework> childrens { get; set; } 16 } 17 18 19 20 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LY.CCPCUEP.Model.UEP { public class OrganizatioTree { public string id { get; set; } public string pid { get; set; } public string name { get; set; } public List<OrganizatioTree> childrens { get; set; } } }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Reflection;//反射 6 using System.Data; 7 8 /// <summary> 9 ///模块说明:数据结构化 10 ///作者:xzx 11 ///创建日期:2019-01-23 12 /// </summary> 13 namespace LY.CCPCUEP.Utility 14 { 15 public class CovertListHelper 16 { 17 //传递过来的类型必须与数据库类型保持一致问题 18 public List<T> convertToList<T>(DataTable dt) where T : new() 19 { 20 21 // 定义集合 22 List<T> ts = new List<T>(); 23 // 获得此模型的类型 24 Type type = typeof(T); 25 //定义一个临时变量 26 string tempName = string.Empty; 27 //遍历DataTable中所有的数据行 28 foreach (DataRow dr in dt.Rows) 29 { 30 T t = new T(); 31 // 获得此模型的公共属性 32 PropertyInfo[] propertys = t.GetType().GetProperties(); 33 //遍历该对象的所有属性 34 foreach (PropertyInfo pi in propertys) 35 { 36 tempName = pi.Name;//将属性名称赋值给临时变量 37 //检查DataTable是否包含此列(列名==对象的属性名) 38 if (dt.Columns.Contains(tempName)) 39 { 40 // 判断此属性是否有Setter 41 if (!pi.CanWrite) continue;//该属性不可写,直接跳出 42 //取值 43 object value = dr[tempName]; 44 //如果非空,则赋给对象的属性 45 if (value != DBNull.Value) 46 47 pi.SetValue(t, value, null); 48 } 49 } 50 //对象添加到泛型集合中 51 ts.Add(t); 52 } 53 return ts; 54 } 55 } 56 }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using LY.CCPCUEP.Model.UEP; using LY.CCPCUEP.BLL.UEP; using Newtonsoft.Json; using LY.CCPCUEP.Utility; /// <summary> ///模块说明:ajax数据交互,数据结构化 ///作者:夏志雄 ///创建日期:2019-01-23 /// </summary> namespace LY.CCPCUEP.UI.Uep.Ajax { /// <summary> /// OrganizationalStructureAjax1 的摘要说明 /// </summary> public class OrganizationalStructureAjax : IHttpHandler { private HttpContext context; DataTable dt = new DataTable(); public string strConn = ""; public string treeNodeID = ""; public string NodeID = ""; public string id = ""; public string reviseOption = ""; string ajaxMethod = ""; bool IsShow = false; public void ProcessRequest(HttpContext context) { this.context = context; id = context.Request.Form["id"]; NodeID = context.Request.Form["NodeID"];//初始id treeNodeID = context.Request.Form["treeNodeID"];//点击节点id //初始化节点为空赋值顶级节点 if (string.IsNullOrEmpty(treeNodeID)) { treeNodeID = "234E45F0077881AAE0430AA3034681AA"; } ajaxMethod = context.Request.Form["ajaxMethod"];//取得前台ajax请求的方法名称 if (!string.IsNullOrEmpty(context.Request.Form["IsShow"])) { IsShow = bool.Parse(context.Request.Form["IsShow"]);//节点是否展示 } System.Reflection.MethodInfo method = this.GetType().GetMethod(ajaxMethod); if (method != null) { method.Invoke(this, new object[] { });//通过方法名称指向对应的方法 } } /// <summary> /// 疑问联系人 /// </summary> public void iphone() { T_UEP_DB_ZTREEBLL bll = new T_UEP_DB_ZTREEBLL(); var rhts = bll.Getrhts().Tables[0].Rows[0]["lookup_value_name"].ToString(); context.Response.Write(JsonConvert.SerializeObject(rhts)); } //统计 public void Summary() { DataTable dt = new DataTable(); T_UEP_DB_ZTREEBLL bll = new T_UEP_DB_ZTREEBLL(); DataTable xbdt = bll.Getxbs(treeNodeID).Tables[0]; DataTable xldt = bll.Getxls(treeNodeID).Tables[0]; object summaryData = null; summaryData = new { jcghs = bll.Getjcghs(treeNodeID).Tables[0].Rows[0]["基层工会数"].ToString(), fhs = bll.Getfhs(treeNodeID).Tables[0].Rows[0]["分会数"].ToString(), hys = bll.Gethys(treeNodeID).Tables[0].Rows[0]["会员数"].ToString(), nxs = xbdt.Rows[0]["男性数"].ToString(), vxs = xbdt.Rows[0]["女性数"].ToString(), zzs = xldt.Rows.Count > 0 ? xldt.Select("LOOKUP_VALUE_NAME='中等专业学校(中专)'")[0][1].ToString() : "", zks = xldt.Rows.Count > 0 ? xldt.Select("LOOKUP_VALUE_NAME='大学专科'")[0][1].ToString() : "", bks = xldt.Rows.Count > 0 ? xldt.Select("LOOKUP_VALUE_NAME='大学本科'")[0][1].ToString() : "", shyjss = xldt.Rows.Count > 0 ? xldt.Select("LOOKUP_VALUE_NAME='硕士研究生'")[0][1].ToString() : "", bsyjss = "",//博士 qtxls = xldt.Rows.Count > 0 ? xldt.Select("LOOKUP_VALUE_NAME='小学/初中(职业初中)/高中(职业高中)/技工学校/无学历/其他学历'")[0][1].ToString() : "", }; //基层工会数 context.Response.Write(JsonConvert.SerializeObject(summaryData)); } /// <summary> /// 异步加载当前节点的子节点 /// </summary> public void AnsyData() { List<object> lsNode = new List<object>(); try { T_UEP_DB_ZTREEBLL dll = new T_UEP_DB_ZTREEBLL(); DataSet ds = dll.GetZtree("", false); dt = ds.Tables[0]; DataTable dataSource = new DataTable(); DataView dv = dt.DefaultView; if (!string.IsNullOrEmpty(id)) { dv.RowFilter = "PARENT_ORGANIZATION_CODE='" + id + "'"; } //1.过滤后直接获取DataTable DataTable newTable1 = dv.ToTable(); lsNode = getList(newTable1); context.Response.Write(JsonConvert.SerializeObject(lsNode)); } catch (Exception) { throw; } } /// <summary> /// 把数据形式转换成zTree的json数据格式 /// </summary> /// <param name="table"></param> /// <returns></returns> public List<object> getList(DataTable table) { try { List<object> lsNode = new List<object>(); bool isParent = true; bool iszk = true; foreach (DataRow row in table.Rows) { string iconstr = string.Empty; if (row["UNION_TYPE"].ToString() == "1") { iconstr = "../Images/8_open.png"; iszk = true; } else if (row["UNION_TYPE"].ToString() == "2") { iconstr = "../Images/1_open.png"; iszk = false; } else if (row["UNION_TYPE"].ToString() == "3") { iconstr = "../Images/6_open.png"; iszk = false; } if (isParentTrue(row["UNION_CODE"].ToString(), table)) isParent = true; else isParent = false; var zTreeData = new { //ID id = row["UNION_CODE"], //父ID pId = row["PARENT_ORGANIZATION_CODE"].ToString(), //名称 name = row["UNION_NAME"], //图标 icon = iconstr, isParent = isParent,//是否拥有子节点 open = iszk, }; lsNode.Add(zTreeData); } return lsNode; } catch (Exception ex) { throw; } } /// <summary> /// 判断当前节点是否还有子节点 /// </summary> /// <param name="ParentId">父节点Id</param> /// <returns>bool类型</returns> public bool isParentTrue(string ParentId, DataTable table) { try { T_UEP_DB_ZTREEBLL dll = new T_UEP_DB_ZTREEBLL(); DataRow[] dr = table.Select("PARENT_ORGANIZATION_CODE='" + ParentId + "'"); dt = ToDataTable(dr); if (dt == null) { return false; } else { return true; } } catch (Exception ex) { throw; } } private DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构 foreach (DataRow row in rows) { tmp.ImportRow(row); // 将DataRow添加到DataTable中 } return tmp; } public List<OrganizationFramework> Main = new List<OrganizationFramework>(); public List<OrganizatioTree> TreeMain = new List<OrganizatioTree>(); /// <summary> /// 获取所有的组织架构数据 /// </summary> /// <returns></returns> public List<OrganizationFramework> GetAllFramework(string treeNodeID) { List<OrganizationFramework> listMenuS = new List<OrganizationFramework>(); OrganizationFramework of = new OrganizationFramework(); T_UEP_DB_ZTREEBLL dll = new T_UEP_DB_ZTREEBLL(); List<object> lsNode = new List<object>(); dt = dll.GetZtree(treeNodeID, IsShow).Tables[0]; if (dt.Rows.Count > 0) { CovertListHelper co = new CovertListHelper(); listMenuS = co.convertToList<OrganizationFramework>(dt); } return listMenuS; } /// <summary> /// 获取所有的树形框架数据 /// </summary> /// <returns></returns> public List<OrganizatioTree> GetAllTree() { List<OrganizatioTree> listMenuS = new List<OrganizatioTree>(); OrganizatioTree of = new OrganizatioTree(); T_UEP_DB_ZTREEBLL dll = new T_UEP_DB_ZTREEBLL(); dt = dll.GetZtree("", IsShow).Tables[0]; if (dt.Rows.Count > 0) { listMenuS = ConvertTo(dt); } return listMenuS; } //使用Linq的ToList方法,非泛型 public static List<OrganizatioTree> ConvertTo(DataTable dt) { if (dt == null) return null; if (dt.Rows.Count <= 0) return null; List<OrganizatioTree> list = new List<OrganizatioTree>(); list = (from DataRow dr in dt.Rows select new OrganizatioTree { id = dr["UNION_CODE"].ToString(), pid = dr["PARENT_ORGANIZATION_CODE"].ToString(), name = dr["UNION_NAME"].ToString(), }).ToList(); return list; } private DataTable getData() { DataTable dt = new DataTable(); dt.Columns.Add("UNION_CODE", typeof(Guid)); dt.Columns.Add("UNION_NAME", typeof(string)); dt.Columns.Add("PARENT_ORGANIZATION_CODE", typeof(Guid)); dt.Rows.Add("1EF13BE6011A6733E0530100007F80DF", "天下会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("2EF13BE6011A6733E0530100007F80DF", "东风汽车有限公司", "10F13BE6011A6733E0530100007F80DF"); dt.Rows.Add("3EF13BE6011A6733E0530100007F80DF", "风神襄阳工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("4EF13BE6011A6733E0530100007F80DF", "大连工厂工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("5EF13BE6011A6733E0530100007F80DF", "PV机关工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("6EF13BE6011A6733E0530100007F80DF", "发动机分公司工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("7EF13BE6011A6733E0530100007F80DF", "广州风神工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("8EF13BE6011A6733E0530100007F80DF", "研发采购工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("9EF13BE6011A6733E0530100007F80DF", "郑州工厂工会", "2EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("1AF13BE6011A6733E0530100007F80DF", "工厂11", "9EF13BE6011A6733E0530100007F80DF"); dt.Rows.Add("1BF13BE6011A6733E0530100007F80DF", "工厂22", "9EF13BE6011A6733E0530100007F80DF"); return dt; } //父子级递归 public void AddMenu(List<OrganizationFramework> all, OrganizationFramework curItem) { //第一层不过滤,第二层过滤为5行,第三层过滤为5行,将三层的数据拼接一起 List<OrganizationFramework> childItems = all.Where(ee => ee.PARENT_ORGANIZATION_CODE == curItem.UNION_CODE).ToList(); //得到子节点 if (curItem.UNION_TYPE == "1" || curItem.UNION_TYPE == "2") { List<OrganizationFramework> l = new List<OrganizationFramework>(); if (childItems.Count > 3 && curItem.UNION_TYPE == "1") { OrganizationFramework ellipsisData = new OrganizationFramework { UNION_CODE = "", PARENT_ORGANIZATION_CODE = "", UNION_NAME = "...", UNION_TYPE = "2", childrens = new List<OrganizationFramework>(), }; l.Add(ellipsisData); } if (childItems.Count > 3 && curItem.UNION_TYPE == "2") { OrganizationFramework ellipsisData = new OrganizationFramework { UNION_CODE = "", PARENT_ORGANIZATION_CODE = "", UNION_NAME = "...", UNION_TYPE = "3", childrens = new List<OrganizationFramework>(), }; l.Add(ellipsisData); } l.InsertRange(0, childItems.Take(3));//序列连续的数量 childItems = l; } curItem.childrens = childItems; //将子节点加入 //遍历子节点,进行递归,寻找子节点的子节点 foreach (var subItem in childItems) { AddMenu(all, subItem); } } //父子级递归 public void AddTreeMenu(List<OrganizatioTree> all, OrganizatioTree curItem) { List<OrganizatioTree> childItems = all.Where(ee => ee.pid == curItem.id).ToList(); //得到子节点 curItem.childrens = childItems; //将子节点加入 //遍历子节点,进行递归,寻找子节点的子节点 foreach (var subItem in childItems) { AddTreeMenu(all, subItem); } } /// <summary> /// 初始化第一次节点加载 /// </summary> public void FirstAnsyData() { try { List<object> lsNode = new List<object>(); T_UEP_DB_ZTREEBLL dll = new T_UEP_DB_ZTREEBLL(); dt = dll.GetZtree("", IsShow).Tables[0]; DataRow[] dr = dt.Select("PARENT_ORGANIZATION_CODE is null "); dt = ToDataTable(dr); lsNode = getList(dt); context.Response.Write(JsonConvert.SerializeObject(lsNode)); } catch (Exception) { throw; } } /// <summary> /// 初始化 /// </summary> public void GetjOrgChartJson() { List<OrganizationFramework> allMenu = GetAllFramework(""); //得到数据 if (!string.IsNullOrEmpty(treeNodeID)) { OrganizationFramework organizationFramework = allMenu.Where(x => x.UNION_CODE == treeNodeID).FirstOrDefault(); if (organizationFramework == null) { organizationFramework = new OrganizationFramework(); } Main.Add(organizationFramework);//根节点 OrganizationFramework organizationFrameworkMain = allMenu.Where(x => x.UNION_CODE == treeNodeID).FirstOrDefault(); if (organizationFrameworkMain == null) { organizationFrameworkMain = new OrganizationFramework(); } AddMenu(allMenu, organizationFrameworkMain);//递归 } else { OrganizationFramework organizationFramework = allMenu.Where(x => x.PARENT_ORGANIZATION_CODE == null).FirstOrDefault(); if (organizationFramework == null) { organizationFramework = new OrganizationFramework(); } Main.Add(organizationFramework);//根节点 OrganizationFramework organizationFrameworkMain = allMenu.Where(x => x.UNION_CODE == Main.ElementAt(0).UNION_CODE).FirstOrDefault(); // OrganizationFramework organizationFrameworkMain = allMenu.Where(x => x.UNION_TYPE == "1").FirstOrDefault(); if (organizationFrameworkMain == null) { organizationFrameworkMain = new OrganizationFramework(); } AddMenu(allMenu, organizationFrameworkMain);//递归 } //结果树形结构 List<OrganizationFramework> treeMenu = Main; string jOrgChartJson = JsonConvert.SerializeObject(treeMenu); context.Response.Write(jOrgChartJson); } public bool IsReusable { get { return false; } } } }
![](https://i-blog.csdnimg.cn/blog_migrate/7a610e1f15009b69fc6fe5d061f986b4.jpeg)