主攻ASP.NET.4.5.1 MVC5.0之重生:在项目中使用zTree jQuery 树插件

效果图和json格式

Controllers代码
using HR.Models;
using HR.Models.Repository;
/************************************************************************************
 * 命名空间:HR.Controllers
 * Controller:  TreeController
 * 版本号:  F 1.0.0.0
 * 负责人:  Markfan
 * 电子邮箱:[email protected]
 * 移动电话:159 2760 2711
 * QQ号码:  115637488
 * 代码说明:全部类型树菜单
 *
 *
 * =====================================================================
 * 更新记录
 * 更新人: 
 * 版本号: F 1.0.0.0
 * 更新代码说明:
 *
 ************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR.ViewModels;
using HR.Helpers;

namespace HR.Controllers
{
  
    public class TreeController : Controller
    {
        //
        // GET: /Tree/
        DepartmentRepository departrepository = new DepartmentRepository();
        ClassRepository classrepository = new ClassRepository();
        SystemUserRepository systemuserrepository = new SystemUserRepository();
        public IQueryable<Department> list { set; get; }
        public ActionResult Index()
        {
            string cookie = CookieHelper.GetValue("SystemUserID");
            int sid = int.Parse(CookieHelper.GetValue("SystemUserID"));
            var model = systemuserrepository.GetModel(sid);
            Employees em = new Employees();
            if (model != null)
            {
                em = emprep.GetModelList().Where(d => d.WorkNumber == model.LogName).FirstOrDefault();
            }
            if (em != null)
            {
                ViewBag.empid = em.DepartmentID;
            }
           
         
         
            return View();
        }

        public ActionResult Show()
        {
            return View();
        }

        //显示部门
        public JsonResult TreeShow()
        {
            //查询顶级部门列表
            var parentdepartmentlist = departrepository.GetModelListByState(1)
                .Where(d => d.ParentID == 0)
                .OrderBy(d => d.Sort);
            //查询岗位列表
            var joblist = classrepository.GetModelListByState(1).Where(d => d.ParentID == 2);
            var treelist = new List<DepartViewModel>();

            foreach (var parentdepartment in parentdepartmentlist)
            {
                var parentdepartmentmodel = new DepartViewModel();
                parentdepartmentmodel.id = parentdepartment.DepartmentID.ToString();
                parentdepartmentmodel.name = parentdepartment.DepartmentName;
                parentdepartmentmodel.open = "true";
                parentdepartmentmodel.pId = "0";
                parentdepartmentmodel.click = "AddDepartment(" + parentdepartment.DepartmentID + ",‘" + parentdepartment.DepartmentName + "‘);";
                treelist.Add(parentdepartmentmodel);
                var departmentlist = departrepository.GetModelListByState(1)
                    .Where(d => d.ParentID == parentdepartment.DepartmentID)
                    .OrderBy(d => d.Sort);
                foreach (var deparment in departmentlist)
                {
                    var deparmentmodel = new DepartViewModel();
                    deparmentmodel.id = deparment.DepartmentID.ToString();
                    deparmentmodel.name = deparment.DepartmentName;
                    deparmentmodel.open = "false";
                    deparmentmodel.pId = deparment.ParentID.ToString();
                    deparmentmodel.click = "AddDepartment(" + deparment.DepartmentID + ",‘" + deparment.DepartmentName + "‘);";
                    treelist.Add(deparmentmodel);
                }
            }

            var source = from c in treelist
                         select c;
            return Json(source, JsonRequestBehavior.AllowGet);
        }
        EmployeesRepository emprep = new EmployeesRepository();
        public JsonResult Tree()
        {
        
          
            var parentdepartmentlist = departrepository.GetModelListByState(1)
                .Where(d => d.ParentID == 0)

                .OrderBy(d => d.Sort);
            var joblist = classrepository.GetModelListByState(1).
                Where(d => d.ParentID == 2)
                .OrderBy(d => d.Sort);
            var treelist = new List<DepartViewModel>();

            foreach (var parentdepartment in parentdepartmentlist)
            {
                var parentdepartmentmodel = new DepartViewModel();
                parentdepartmentmodel.id = parentdepartment.DepartmentID.ToString();
                parentdepartmentmodel.name = parentdepartment.DepartmentName;
                parentdepartmentmodel.open = "false";
                parentdepartmentmodel.pId = "0";
                parentdepartmentmodel.url = "/employees/index/0?Jobs=0";
                parentdepartmentmodel.target = "rform";
                treelist.Add(parentdepartmentmodel);


                var departmentlist = departrepository.GetModelListByState(1)
                    .Where(d => d.ParentID == parentdepartment.DepartmentID)
                    .OrderBy(d => d.Sort);
                foreach (var deparment in departmentlist)
                {
                    var deparmentmodel = new DepartViewModel();
                    deparmentmodel.id = deparment.DepartmentID.ToString();
                    deparmentmodel.name = deparment.DepartmentName;
                    deparmentmodel.open = "false";
                    deparmentmodel.pId = deparment.ParentID.ToString();
                    deparmentmodel.url = "/employees/index/" + deparment.DepartmentID + "?Jobs=0";
                    deparmentmodel.target = "rform";
                    treelist.Add(deparmentmodel);
                    foreach (var job in joblist)
                    {
                        var model = new DepartViewModel();
                        model.id = deparment.DepartmentID.ToString() + "-" + job.ClassID.ToString();
                        model.name = job.ClassName;
                        model.pId = deparment.DepartmentID.ToString();
                        model.open = "false";
                        model.url = "/employees/index/" + deparment.DepartmentID + "?Jobs=" + job.ClassID + "";
                        model.target = "rform";
                        treelist.Add(model);
                    }
                }

            }

            var source = from c in treelist
                         select c;
            return Json(source, JsonRequestBehavior.AllowGet);
        }
        public JsonResult TreeType(int id)
        {
            string cookie = CookieHelper.GetValue("SystemUserID");

            if (string.IsNullOrEmpty(cookie))
            {
                int sid = int.Parse(CookieHelper.GetValue("SystemUserID"));
                var model = emprep.GetModel(sid);
                Employees em = new Employees();
                if (model != null)
                {
                    var emp = emprep.GetModelList().Where(d => d.WorkNumber == model.WorkNumber).FirstOrDefault();
                }
                ViewBag.empid = em.DepartmentID;
            }
         
            var parentdepartmentlist = departrepository.GetModelListByState(1)
                .Where(d => d.ParentID == 0)
                .OrderBy(d => d.Sort);
            var joblist = classrepository.GetModelListByState(1).
                Where(d => d.ParentID == 2)
                .OrderBy(d => d.Sort);
            var treelist = new List<DepartViewModel>();

            foreach (var parentdepartment in parentdepartmentlist)
            {
                var parentdepartmentmodel = new DepartViewModel();
                parentdepartmentmodel.id = parentdepartment.DepartmentID.ToString();
                parentdepartmentmodel.name = parentdepartment.DepartmentName;
                parentdepartmentmodel.open = "false";
                parentdepartmentmodel.pId = "0";
                parentdepartmentmodel.url = "/employees/index/0?Jobs=0";
                parentdepartmentmodel.target = "rform";
                treelist.Add(parentdepartmentmodel);


                var departmentlist = departrepository.GetModelListByState(1)
                    .Where(d => d.DepartmentID == id)
                    .OrderBy(d => d.Sort);
                foreach (var deparment in departmentlist)
                {
                    var deparmentmodel = new DepartViewModel();
                    deparmentmodel.id = deparment.DepartmentID.ToString();
                    deparmentmodel.name = deparment.DepartmentName;
                    deparmentmodel.open = "false";
                    deparmentmodel.pId = deparment.ParentID.ToString();
                    deparmentmodel.url = "/employees/index/" + deparment.DepartmentID + "?Jobs=0";
                    deparmentmodel.target = "rform";
                    treelist.Add(deparmentmodel);
                    foreach (var job in joblist)
                    {
                        var model = new DepartViewModel();
                        model.id = deparment.DepartmentID.ToString() + "-" + job.ClassID.ToString();
                        model.name = job.ClassName;
                        model.pId = deparment.DepartmentID.ToString();
                        model.open = "false";
                        model.url = "/employees/index/" + deparment.DepartmentID + "?Jobs=" + job.ClassID + "";
                        model.target = "rform";
                        treelist.Add(model);
                    }
                }

            }

            var source = from c in treelist
                         select c;
            return Json(source, JsonRequestBehavior.AllowGet);
        }
    }
}

 

using HR.Models;
using HR.Models.Repository;
/************************************************************************************
 * CLR版本: 4.0.30319.18051
 * 机器名称:MARKFAN
 * 命名空间:HR.ViewModels
 * 文件名:  DepartViewModel
 * 版本号:  V1.0.0.0
 * 唯一标识:cfcc84df-4cea-4400-9e02-95ba620a453d
 * 创建人:  Markfan

 * 电子邮箱:[email protected]
 * 移动电话:159 2760 2711
 * QQ号码:  115637488
 * 创建时间:2013/12/18 13:51:56
 * 描述:
 *
 *
 * =====================================================================
 * 修改标记
 * 修改时间:2013/12/18 13:51:56
 * 修改人: mark
 * 版本号: F1.0.0.0
 * 描述:
 *
 *
 *
 *
 ************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HR.ViewModels
{
    public class DepartViewModel
    {
        /// <summary>
        /// ID
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 父级ID
        /// </summary>
        public string pId { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 是否展开
        /// </summary>
        public string open { get; set; }
        /// <summary>
        /// 跳转地址
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 跳转属性
        /// </summary>
        public string target { get; set; }
        /// <summary>
        /// 点击事件
        /// </summary>
        public string click { get; set; }
    }
}

 

 

 

页面调用代码

 

 

 

@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>
<html>
<head>
    <title>tree</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script src="~/Content/EasyUI/jquery-1.8.3.min.js"></script>
    <link href="~/Content/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <script src="~/Content/js/jquery.ztree.core-3.5.js"></script>

    <script type="text/javascript">
        <!--
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        }
    };
        var url = "/Tree/Tree";
   // var url = "/Tree/TreeType/@ViewBag.empid";
    $.get(url, function (data) {
        $.fn.zTree.init($("#treeDemo"), setting, data);
    });
    //-->
    </script>
    <style type="text/css">
    </style>
</head>

<body>
    <div class="content_wrap">
        <div class="zTreeDemoBackground left">
            <ul id="treeDemo" class="ztree"></ul>
        </div>
    </div>
</body>
</html>

下载ztree地址: http://www.ztree.me/

 

声明:本博客高度重视知识产权保护,发现本博客发布的信息包含有侵犯其著作权的链接内容时,请联系我,我将第一时间做相应处理,联系邮箱[email protected]

 

 

 

主攻ASP.NET.4.5.1 MVC5.0之重生:在项目中使用zTree jQuery 树插件,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。