easyui combotree的使用
前台HTML:
<div class="search-container"> <table class="search-container-table" cellpadding="0" cellspacing="0"> <tr> <td>统计年份:</td> <td> <input type="text" class="input-text" id="year" name="year" onclick="WdatePicker({ dateFmt: 'yyyy', minDate: '1980', maxDate: '2099' })" /> </td> <td>统计部门:</td> <td> <input type="text" name="dept" id="dept" class="easyui-combotree" style="width: 202px; height: 29px; overflow: auto" /> </td> </tr> <tr> <td>检测项目: </td> <td> <input name="testItem" id="testItem" class="easyui-combotree" style="width: 250px; height: 29px;" /> </td> <td> <a class="a-btn" href="javascript:void(0);" onclick="loadgrid()"> <img alt="" src="~/Content/images/stat.gif" /> 统计 </a> </td> </tr> </table> </div>
前台JS:
//部门树 $("#dept").combotree({ editable: false, url: '@Url.Content("~/DetReport/DetReportManage/GetDeptTree")', onSelect: function (node) { //加载检测项目 $("#testItem").combotree({ editable: false, url: '@Url.Content("~/DetReport/YSLReport/GetTestItemTree?deptCode=")' + node.id, onSelect: function (node) { //显示全路径 var parent = node; var tree = $('#testItem').combotree('tree'); var path = new Array(); do { path.unshift(parent.text); var parent = tree.tree('getParent', parent.target); } while (parent); var pathStr = ''; for (var i = 0; i < path.length; i++) { pathStr += path[i]; if (i < path.length - 1) { pathStr += ' - '; } } setTimeout(function () { $('input[name="testItem"]').prev().val(pathStr); }, 100); } }); } });
后台代码1:
/// <summary> /// 获取部门树 /// </summary> public JsonResult GetDeptTree() { List<object> list = new List<object>(); List<SYS_DEPT> deptListAll = m_DeptDal.GetDeptListAll(); foreach (SYS_DEPT dept0 in deptListAll.FindAll(a => string.IsNullOrWhiteSpace(a.PDEPTCODE))) { var obj0 = new { id = dept0.DEPTCODE, text = dept0.DEPTNAME, children = new List<object>() }; foreach (SYS_DEPT dept1 in deptListAll.FindAll(a => a.PDEPTCODE == dept0.DEPTCODE)) { var obj1 = new { id = dept1.DEPTCODE, text = dept1.DEPTNAME, children = new List<object>() }; foreach (SYS_DEPT dept2 in deptListAll.FindAll(a => a.PDEPTCODE == dept1.DEPTCODE)) { var obj2 = new { id = dept2.DEPTCODE, text = dept2.DEPTNAME, children = new List<object>() }; foreach (SYS_DEPT dept3 in deptListAll.FindAll(a => a.PDEPTCODE == dept2.DEPTCODE)) { var obj3 = new { id = dept3.DEPTCODE, text = dept3.DEPTNAME, children = new List<object>() }; obj2.children.Add(obj3); } obj1.children.Add(obj2); } obj0.children.Add(obj1); } list.Add(obj0); } return Json(list, JsonRequestBehavior.AllowGet); }
后台代码2:
/// <summary> /// 获取检测项目树(统计用) /// </summary> public JsonResult GetTestItemTree(string deptCode) { List<object> list = new List<object>(); List<SYS_DEPT> deptListAll = m_DeptDal.GetDeptListAll(); List<DETECTIONITEMS> itemListAll = m_DetectionItemsDAL.GetDetectionItemsListAll(); List<SPECIALTY> specialtyListAll = m_SpecialtyDAL.GetSpecialtyListAll(); List<SYS_DEPT> deptList = deptListAll.FindAll(a => a.DEPTCODE.IndexOf(deptCode) == 0); if (deptList.Count > 0) { foreach (SPECIALTY specialty in specialtyListAll.FindAll(a => deptList.Exists(b => b.DEPTCODE == a.DEPTCODE))) { var specialtyObj = new { id = specialty.SPECIALTYID, text = specialty.SPECIALTYNAME, leaf = false, type = 1, //1专业2样品名称3检测项目 children = new List<object>() }; foreach (DETECTIONITEMS items in itemListAll.FindAll(a => a.SPECIALTYID == specialty.SPECIALTYID && a.PID == 0)) { List<DETECTIONITEMS> subItemsList = itemListAll.FindAll(a => a.PID == items.DETITEMID); var itemsObj = new { id = items.DETITEMID, text = items.ITEMNAME, leaf = subItemsList.Count > 0 ? false : true, //只能选择leaf为true的节点 type = 2, //1专业2样品名称3检测项目 children = new List<object>() }; foreach (DETECTIONITEMS subItems in subItemsList) { var subItemsObj = new { id = subItems.DETITEMID, text = subItems.ITEMNAME, leaf = true, //只能选择leaf为true的节点 type = 3, //1专业2样品名称3检测项目 children = new List<object>() }; itemsObj.children.Add(subItemsObj); } specialtyObj.children.Add(itemsObj); } list.Add(specialtyObj); } } return Json(list, JsonRequestBehavior.AllowGet); }
示意图: