controller动态获取单位用户树
#region 下拉树菜单 /// <summary> /// 获取工作人员树菜单 /// </summary> /// <param name="addid"></param> /// <param name="unitid"></param> /// <returns></returns> public string GetUserMenu(int addid) { AID_UNIT_DAL unitdal = AID_UNIT_DAL._(); decimal unitid = this.CurrentUnitId; List<AID_UNIT> unitList = unitdal.GetList().Where(c => c.UNITID == unitid).ToList(); List<Menu> menulist = new List<Menu>(); if (unitList.Count > 0) { Menu menu = new Menu(); foreach (var page in unitList) { menu.id = page.UNITID.ToString(); menu.text = page.UNITNAME; var count = unitList.Where(c => c.PARENTUNIT == page.UNITID).Count(); if (count > 0) { menu.state = "closed"; BindMenu(menu, unitid); } BindStaffMenu(menu, unitid); menulist.Add(menu); } } string js = JsonMapper.ToJson(menulist); return js; } /// <summary> /// 绑定用户信息 /// </summary> /// <param name="staffMenu"></param> /// <param name="unitid"></param> private void BindStaffMenu(Menu staffMenu, decimal unitid) { INFO_STAFF_DAL staffdal = INFO_STAFF_DAL._(); List<INFO_STAFF> staffList = staffdal.GetList().Where(c => c.WORKUNIT == unitid).ToList(); foreach (var staff in staffList) { Menu childMenu = new Menu(); childMenu.id = staff.STAFFID.ToString(); childMenu.text = staff.STAFFNAME; //递归绑定 // BindStaffMenu(childMenu,unitid); //给父菜单添加子菜单 staffMenu.children.Add(childMenu); } } /// <summary> /// 绑定子单位 /// </summary> /// <param name="parentMenu"></param> private void BindMenu(Menu parentMenu, decimal unitid) { AID_UNIT_DAL unitdal = AID_UNIT_DAL._(); List<AID_UNIT> secondary = unitdal.GetList().Where(p => p.PARENTUNIT == unitid).ToList(); foreach (var second in secondary) { Menu childMenu = new Menu(); childMenu.id = second.UNITID.ToString(); childMenu.text = second.UNITNAME; var count = secondary.Where(c => c.PARENTUNIT == second.UNITID).Count(); if (count > 0) childMenu.state = "closed"; //递归绑定 BindMenu(childMenu, unitid); //给父菜单添加子菜单 parentMenu.children.Add(childMenu); } } //定义菜单类 private class Menu { private string _id; private string _text; private string _state; private List<Menu> _children = new List<Menu>(); public string id { get { return _id; } set { _id = value; } } public string text { get { return _text; } set { _text = value; } } public string state { get { return _state; } set { _state = value; } } public List<Menu> children { get { return _children; } set { _children = value; } } } #endregion /// <summary> /// 初始化单位下列树菜单 /// </summary> /// <param name="addid">addid>0时,下拉树菜单含“顶级菜单”,addid<0时,下拉树菜单不含“顶级菜单”</param> /// <returns></returns> public ActionResult getReletiveUser(int addid) { string js = GetUserMenu(addid); return Content(js, "application/json"); }
js动态生成复选框
<script type="text/javascript"> $("#USID").tree({ url: "/SystemMng/getReletiveUser?addid=" + 0, required: true, panelHeight: 120, checkbox: true, onBeforeSelect: function (node) { if (node.state == "open") { $("#hidUnit").val(node.id); } else { alert("请选择子单位!") } } }); </script>
<ul class="easyui-tree" type="text" name="USID" id="USID" style="width:100%; height:34px; margin-left:15px;" data-options="">
时间: 2024-10-24 12:48:20