Ztree简单实现

//ztree的前台视图

@{
    Layout = null;
   
}
 
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
  


    <script src="~/jquery-1.8.2.min.js"></script>@*//jquery文件*@
    <script src="~/zTree_v3-master/js/jquery.ztree.all.min.js"></script>@*//ztree的js文件*@
    <link href="~/zTree_v3-master/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />@*//ztree的风格样式文件*@
    <script src="~/layer-v3.0.3/layer/layer.js"></script>@*//遮罩层layer的js文件*@
</head>
<body>
    <script type="text/javascript">


        //需要引用jquery.ztree.all.min.js,jquery,zTreeStyle.css,layer.js


        function addHoverDom(treeId, treeNode)
        {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_" + treeNode.id).length > 0 || $("#delBtn_" + treeNode.id).length > 0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.id
                + "' title='添加子模块'></span>" +// οnfοcus='this.blur();'
                "<span class='button edit' id='editBtn_" + treeNode.id
                + "' title='编辑模块'></span>" +
                "<span class='button del' id='delBtn_" + treeNode.id
                + "' title='编辑模块'></span>";// οnfοcus='this.blur();'
            sObj.after(addStr);
            var btnadd = $("#addBtn_" + treeNode.id);
            if (btnadd) btnadd.bind("click", function ()
            {
                layer.open({
                    type: 2,
                    area: ['40%', '80%'],
                    content: '/jurisdiction_Magaer_/Add_jurisdictionCoud/' + treeNode.id + "_" + treeNode.name,
                    // data: {"id": "" + treeNode.id + "","name": ""+treeNode.name+""},
                    end: function () {
                        Model_jurisdiction_lode();
                       
                    }
                })
            });
            var btneda = $("#editBtn_" + treeNode.id);
            if (btneda) btneda.bind("click", function () {
                layer.open({
                    type: 2,
                    area: ['40%', '60%'],
                    content: '/jurisdiction_Magaer_/UPD_jurisdictionCoud/' + treeNode.id + "_" + treeNode.name,
                    // data: {"id": "" + treeNode.id + "","name": ""+treeNode.name+""},
                    end: function () {
                        Model_jurisdiction_lode();
                     
                    }
                })
            });
            var btndel = $("#delBtn_" + treeNode.id);
            if (btndel) btndel.bind("click", function () {
                //layer.open({
                //    type: 2,
                //    area: ['40%', '60%'],
                //    content: '/jurisdiction_Magaer_/UPD_jurisdictionCoud/' + treeNode.id + "_" + treeNode.name,
                //    // data: {"id": "" + treeNode.id + "","name": ""+treeNode.name+""},
                //    end: function ()
                //    {
                //        Model_jurisdiction_lode();
                $.ajax({
                    url: "/jurisdiction_Magaer_/Del_urisdictionCoud/" + treeNode.id,
                    type:"get",
                    success: function (data)
                    {
                        alert(data);
                        Model_jurisdiction_lode();
                    }
                });
                //    }
                //})
            });
               
               // openWindow("Detail?paterCode=" + treeNode.id, "添加子模块", "800px", "400px", null);
                // artdialogIfromOpen("Detail?paterCode=" + treeNode.id, '添加子模块',800,800);

        }
        function removeHoverDom(treeId, treeNode)
        {
            $("#addBtn_" + treeNode.id).unbind().remove();
            $("#editBtn_" + treeNode.id).unbind().remove();
            $("#delBtn_" + treeNode.id).unbind().remove();
            // $("#removeBtn_"+treeNode.id).unbind().remove();
        };
        //function removeHoverDom()
        //{
        //    alert(1);
        //}


        Model_jurisdiction_lode();
        var zTree;
        var demoIframe;
        function Model_jurisdiction_lode()
        {
            $(document).ready(function () {
                $.ajax({
                    url: "/jurisdiction_Magaer_/Get_jurisdiction",
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        var setting =
                         {   
                             view: {
                                 addHoverDom:addHoverDom,
                                 removeHoverDom: removeHoverDom,
                                 dblClickExpand: false,
                                 //showLine: true,                             
                                 selectedMulti: true
                             },
                             edit:
                                 {
                                 //enable: true,
                                 //drag: {
                                 //    isMove: true,
                                 //    prev: true,
                                 //    autoOpenTime: 0
                                 //},
                                 //editNameSelectAll: true,
                                 //showRemoveBtn: true,
                                 //removeTitle: "删除节点",
                                 //renameTitle: "编辑节点名称",
                                 //showRenameBtn: true,
                             },
                             data: {
                                 simpleData: {
                                     enable: true,
                                     idKey: "id",
                                     nameKey: "pId"
                                 }
                             }
                        };
                        var zNodes = data;
                        $.fn.zTree.init($("#ztree"), setting, zNodes);

                    }

                });


            });
        }
       
    </script>
  
        <style type="text/css">
        .ztree li span.button.add 
        {
            margin-left: 2px;
            margin-right: -1px;
            background-position: -144px 0;
            vertical-align: top;
            *vertical-align: middle;
        }


    </style>
    <div> 
        <ul id="ztree" class="ztree">
            <li></li>
        </ul>
    </div>
</body>
</html>

//后台控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;



namespace jurisdiction_Yzw.Controllers
{
    public class jurisdiction_Magaer_Controller : Controller
    {
        // GET: jurisdiction_Magaer_
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult Add_jurisdictionCoud(string id)
        {
            ViewBag.id = id.Remove(id.LastIndexOf("_"));
            Session["Codeid"]= id.Remove(id.LastIndexOf("_"));
            Session["PrnCodeid"]= id.Remove(0, id.LastIndexOf("_") + 1);
            string name = id.Remove(0,id.LastIndexOf("_")+1);

            ViewBag.name = name;
            return View();

        }
        [HttpPost]
        public ActionResult Add_jurisdictionCoud(module_db model)
        {
            if (model.creator_name == "")
            {
                model.creator_name = "YZW";
            }
            if (model.creator_full_name == "")
            {
                model.creator_full_name = "YZW";
            }
            model.creator_time =Convert.ToDateTime(DateTime.Now.ToString());
            

            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=system_center_db;Integrated Security=True"))
            {
                string Codeid = Session["Codeid"].ToString();
              
                string sqls = "select  *  from  module_db  where module_code = '"+ Codeid + "'";
                SqlDataAdapter cmds = new SqlDataAdapter(sqls, conn);
                DataTable tb = new DataTable();
                cmds.Fill(tb);
                module_db pmodel = new module_db();
                foreach (DataRow item in tb.Rows)
                {
                       pmodel.module_code       = item["module_code"].ToString();
                       pmodel.module_name       =item["module_name"].ToString();
                       pmodel.parent_code = Codeid;
                       pmodel.module_level      =Convert.ToInt32(item["module_level"]);
                       pmodel.is_menu           = Convert.ToInt32(item["is_menu"]);
                       pmodel.is_action         = Convert.ToInt32(item["is_action"]);
                       pmodel.action_url        =item["action_url"].ToString();
                   
                       pmodel.module_status     = Convert.ToInt32(item["module_status"]);
                       pmodel.creator_name      =item["creator_name"].ToString();
                       pmodel.creator_full_name =item["creator_full_name"].ToString();
                       pmodel.creator_time      = Convert.ToDateTime(item["creator_time"]);
                       pmodel.modifi_name       =item["modifi_name"].ToString();
                       pmodel.modifi_full_name  =item["modifi_full_name"].ToString();
                     

                }
                model.module_level = pmodel.module_level + 1;
                model.parent_code = pmodel.parent_code;

                string sql =string.Format(
                @"insert   module_db( 
                module_code,
                module_name,
                parent_code,
                module_level,
                is_menu,
                is_action,
                action_url,
                order_by,
                module_status,
                creator_name,
                creator_full_name,
                creator_time,
                modifi_name,
                modifi_full_name,
                modifi_time)
                    values ('{0}','{1}','{2}',{3},{4},{5},'{6}','{7}',{8},'{9}','{10}','{11}','{12}','{13}',NULL)", 
                model.module_code,
                model.module_name,
                model.parent_code,
                model.module_level,
                model.is_menu,
                model.is_action,
                model.action_url,
                model.order_by,
                model.module_status,
                model.creator_name,
                model.creator_full_name, 
                model.creator_time,
                model.modifi_name,
                model.modifi_full_name
                );
                conn.Close();
                conn.Open();
                //values ('config_manager','配置管理','yixin_public',2,1,0,'','2',1,'lzh','lzh','2017-07-05 15:57:06',NULL,NULL,),");
                SqlCommand cmd = new SqlCommand(sql,conn);
                cmd.ExecuteNonQuery();
                conn.Close();


            }
            return View();

        }
        public string Get_jurisdiction()
        {
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=system_center_db;Integrated Security=True"))
            {
                SqlDataAdapter adp = new SqlDataAdapter("select * FROM module_db", conn);
                DataTable tb = new DataTable();
                adp.Fill(tb);
                string result = "[";
                foreach (DataRow item in tb.Rows)
                {
                    bool open = Convert.ToInt32(item["module_status"]) == 1 ? true : false;
                    bool checkeds = Convert.ToInt32(item["is_menu"]) == 1 ? true : false;
                    //  bool isParent = Convert.ToInt32(item["module_status"]) == 1 ? true : false;                                                                                                  //"+item["action_url"] + "                           
                    result += "{\"id\":\"" + item["module_code"] + "\",\"pId\":\"" + item["parent_code"] + "\",\"name\":\"" + item["module_name"] + "\",\"open\":\"" + open + "\",\"checked\":\"" + checkeds + "\",\"url\":\"\",\"isParent\":\"true\"},";
                }
                result = result.Substring(0, result.LastIndexOf(","));
                result += "]";
                return result;

            }



        }

        [HttpGet]
        public ActionResult UPD_jurisdictionCoud(string id)
        {
            ViewBag.id = id.Remove(id.LastIndexOf("_"));
            Session["edaCodeid"] = id.Remove(id.LastIndexOf("_"));
            string name = id.Remove(0, id.LastIndexOf("_") + 1);
            module_db pmodel = new module_db();
            #region
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=system_center_db;Integrated Security=True"))
            {

                string sqls = "select  *  from  module_db  where module_code = '" + id.Remove(id.LastIndexOf("_")) + "'";
                SqlDataAdapter cmds = new SqlDataAdapter(sqls, conn);
                DataTable tb = new DataTable();
                cmds.Fill(tb);

                foreach (DataRow item in tb.Rows)
                {
                    pmodel.module_code = item["module_code"].ToString();
                    pmodel.module_name = item["module_name"].ToString();
                    Session["pedaCodeid"]= item["parent_code"].ToString();

                    pmodel.module_level = Convert.ToInt32(item["module_level"]);
                    pmodel.modifi_name = item["modifi_name"].ToString();
                    pmodel.modifi_full_name = item["modifi_full_name"].ToString();
                    pmodel.is_menu = Convert.ToInt32(item["is_menu"]);
                    pmodel.is_action = Convert.ToInt32(item["is_action"]);
                    pmodel.action_url = item["action_url"].ToString();
                    pmodel.module_status = Convert.ToInt32(item["module_status"]);
                    pmodel.creator_name = item["creator_name"].ToString();
                    pmodel.creator_full_name = item["creator_full_name"].ToString();
                    pmodel.creator_time = Convert.ToDateTime(item["creator_time"]);
                    
                    Session["creator_time"] = (item["creator_time"]).ToString();
                    Session["creator_name"] = (item["creator_name"]).ToString();
                    Session["creator_full_name"] = (item["creator_full_name"]).ToString();
                }

            }
            #endregion
            ViewBag.name = name;
            return View(pmodel);

        }
        [HttpPost]
        public ActionResult UPD_jurisdictionCoud(module_db model)
        {
            if (model.modifi_name == "")
            {
                model.modifi_name = "YZW";
            }
            if (model.modifi_full_name == "")
            {
                model.modifi_full_name = "YZW";
            }
            model.modifi_time = Convert.ToDateTime(DateTime.Now.ToString());


            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=system_center_db;Integrated Security=True"))
            {
                string sql = string.Format(@"update   module_db   set modifi_time='{0}', module_name='{1}',  parent_code='{2}',
                                                                      module_level={3},   is_menu={4},    is_action={5},
                                                                      action_url='{6}',   order_by='{7}', module_status={8},
                                                                      creator_name='{9}', creator_full_name='{10}',creator_time='{11}',
                                                                      modifi_name='{12}',   modifi_full_name='{13}'    where  module_code='{14}'",
                                                                      model.modifi_time,
                                                                      model.module_name,
                                                                      Session["pedaCodeid"].ToString(),
                                                                      model.module_level,
                                                                      model.is_menu,
                                                                      model.is_action,
                                                                      model.action_url,
                                                                      model.order_by,
                                                                      model.module_status,
                                                                      Session["creator_time"],
                                                                      Session["creator_full_name"],
                                                                      Session["creator_time"],
                                                                      model.modifi_name,
                                                                      model.modifi_full_name,
                                                                      Session["edaCodeid"].ToString());

                conn.Close();
                conn.Open();
                //values ('config_manager','配置管理','yixin_public',2,1,0,'','2',1,'lzh','lzh','2017-07-05 15:57:06',NULL,NULL,),");
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();


            }
            return View();

        }


        public string Del_urisdictionCoud(string  id)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=system_center_db;Integrated Security=True"))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("DELETE    from  module_db  where module_code='"+ id + "'", conn);
                 int  i= cmd.ExecuteNonQuery();
                conn.Close();
                if (i==1)
                {
                    return "删除模块成功";
                }
                else
                {
                    return "删除模块失败";
                }


            }

        }





    }
}

//添加节点页面

@{
    Layout = null;
}
@model jurisdiction_Yzw.module_db
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add_jurisdictionCoud</title>
    <script src="~/jquery-1.8.2.min.js"></script>
    <link href="~/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
    <div> 
        <form action="Add_jurisdictionCoud" method="post" enctype="multipart/form-data">
            <table class="tab-pane" width="100%">
                <tr>
                    <td>父模块:</td>
                    <td><span >@ViewBag.name</span></td>
                    <td>父模块Code:</td>
                    <td><span name="parent_code">@ViewBag.id</span></td>
                </tr>
                <tr>
                    <td>模块Code:</td>
                    <td>@Html.TextBoxFor(T => T.module_code)</td>
                    <td>模块名称</td>
                    <td>@Html.TextBoxFor(T => T.module_name)</td>
                </tr>
                <tr>
                    <td>级别:</td>
                    <td>@Html.TextBoxFor(T => T.module_level)</td>
                    <td>是否菜单:</td>
                    <td><input type="radio" name="is_menu" value="1" checked="checked"/>是<input type="radio" name="is_menu" value="0" />否</td>
                </tr>
                <tr>
                    <td>是否请求:</td>
                    <td><input type="radio" name="is_action" value="1" checked="checked" />是<input type="radio" name="is_action" value="0" />否</td>
                    <td>是否有效:</td>
                    <td><input type="radio" name="module_status" value="1" checked="checked"/>是<input type="radio" name="module_status" value="0" />否</td>
                </tr>
                <tr>
                    <td>显示顺序:</td>
                    <td>@Html.TextBoxFor(T =>T.order_by)</td>
                    <td>请求url:</td>
                    <td>@Html.TextBoxFor(T => T.action_url)</td>
                </tr>

            </table>

            <input  type="submit" value="添加"  class="btn-success" οnclick="closs()"/>
            <input type="button" value="返回" class="btn-group"  οnclick="closs()" />
        </form>
        <script>

            function closs()
            {
                var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index);
            }

        </script>
    </div>
</body>
</html>

//修改节点视图

@{
    Layout = null;
}
@model jurisdiction_Yzw.module_db
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>UPD_jurisdictionCoud</title>
    <link href="~/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet" />
    <script src="~/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
    <div> 
        <form action="UPD_jurisdictionCoud" method="post" enctype="multipart/form-data">
            <table class="tab-pane" width="100%">
                <tr>
                    <td>父模块:</td>
                    <td><span>@ViewBag.name</span></td>
                    <td>父模块Code:</td>
                    <td><input name="parent_code" value="@ViewBag.id" disabled="disabled"></td>
                </tr>
                <tr>
                    <td>模块Code:</td>
                    <td>@Html.TextBoxFor(T=>T.module_code ,new { disabled = "disabled" })</td>
                    <td>模块名称</td>
                    <td>@Html.TextBoxFor(T => T.module_name)</td>
                </tr>
                <tr>
                    <td>级别:</td>
                    <td>@Html.TextBoxFor(T => T.module_level)</td>
                    <td>是否菜单:</td>
                    <td><input type="radio" name="is_menu" value="1" checked="checked" />是<input type="radio" name="is_menu" value="0" />否</td>
                </tr>
                <tr>
                    <td>是否请求:</td>
                    <td><input type="radio" name="is_action" value="1" checked="checked" />是<input type="radio" name="is_action" value="0" />否</td>
                    <td>是否有效:</td>
                    <td><input type="radio" name="module_status" value="1" checked="checked" />是<input type="radio" name="module_status" value="0" />否</td>
                </tr>
                <tr>
                    <td>显示顺序:</td>
                    <td>@Html.TextBoxFor(T => T.order_by)</td>
                    <td>请求url:</td>
                    <td>@Html.TextBoxFor(T => T.action_url)</td>
                </tr>

            </table>

            <input type="submit" value="保存" class="btn-success" οnclick="closs()" />
            <input type="button" value="返回" class="btn-group" οnclick="closs()" />
        </form>
        <script>
             function closs()
            {
                var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index);
            }
        </script>
    </div>
</body>
</html>

 

转载于:https://www.cnblogs.com/-bai/p/8017732.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值