easyui_tree 复选框 动态加载树

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了easyui_tree 复选框 动态加载树相关的知识,希望对你有一定的参考价值。

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=""> 

 


 



 

以上是关于easyui_tree 复选框 动态加载树的主要内容,如果未能解决你的问题,请参考以下文章

如何在使用数据表加载页面时为所有行和列动态添加复选框?

zTreezTree根据后台数据生成树并动态设置前面的节点复选框的选中状态

BZOJ3730震波 动态树分治+线段树

震波——动态点分治+线段树

表单(上)EasyUI Form 表单EasyUI Validatebox 验证框EasyUI Combobox 组合框EasyUI Combo 组合EasyUI Combotree 组合树((代码片

从内存层次浅谈动态与静态