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

时间:2014-05-17 02:48:12   收藏:0   阅读:1011

效果图和json格式

bubuko.com,布布扣

bubuko.com,布布扣

Controllers代码
using HR.Models;
using HR.Models.Repository;
/************************************************************************************
 * 命名空间:HR.Controllers
 * Controller:  TreeController
 * 版本号:  F 1.0.0.0
 * 负责人:  Markfan
 * 电子邮箱:ffgign@qq.com
 * 移动电话: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

 * 电子邮箱:ffgign@qq.com
 * 移动电话: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/

 

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

 

作者:Mark Fan (小念头)    来源:http://cube.cnblogs.com
说明:未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有疑问,可以通过 ffgign@qq.com 联系作者,本文章采用 知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可

 

bubuko.com,布布扣

 

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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!