ASP.net MVC、Extjs多级目录(理论上可以无限级、非递归)treepanel显示和数据库(多级目录)存储结构的设计(下篇)

1、添加节点、子节点、编辑节点

1.1、添加节点

1.2、添加子节点

2、移动节点或者目录

3、删除目录和节点

其中,遍历的情况有2种。

通过后台获取的(这里是一次性获取)节点,在该树的attributes内。

从前台操作增加的节点,在childNodes内。

这里遍历的代码片段:

    var temp = [];

    //获取loader的所有子节点
    function findchildasy(node) {
        var childnodes = node.children;
        Ext.each(childnodes, function() { //从节点中取出子节点依次遍历
            var nd = this;
            temp.push(nd.id);
            if (!nd.leaf) { //判断子节点下是否存在子节点
                findchildasy(nd); //如果存在子节点 递归
            }
        });
    }
    //获取所有的子节点
    function findchildnode(node, node1) {
        if (node1.childNodes != 0) {//判断是否存在子节点(有可能是loader,在attributes中)
            Ext.each(node1.childNodes, function() { //从节点中取出子节点依次遍历
                var nd = this;
                temp.push(nd.id);
                if (!nd.leaf) { //判断节点下是否存在子节点
                    findchildnode(nd.attributes, nd); //如果存在子节点 递归
                }
            })
        } else {
            Ext.each(node.children, function() { //从节点中取出子节点依次遍历
                var nd = this;
                temp.push(nd.id);
                if (!nd.leaf) { //判断节点下是否存在子节点
                    findchildasy(nd); //如果存在子节点 递归
                }
            })
        }
    }

4、节点操作代码片段

        //节点的操作
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult operation()
        {
            if(Request.Form["operation"]=="append")
            {
                using (TreeBuildDataContext tem_db = new TreeBuildDataContext())
                {
                    tem_db.ExecuteCommand("update treestruct set tre_parent={0} where tre_children={1}", Request.Form["m1"], Request.Form["m2"]);
                }
            }
            else if (Request.Form["operation"] == "above" || Request.Form["operation"]=="below")
            {
                using (TreeBuildDataContext tem_db = new TreeBuildDataContext())
                {
                    var tem_parent = tem_db.treestruct.Where(p => p.tre_children == int.Parse(Request.Form["m1"])).ToList();
                    var tem_children = tem_db.treestruct.Where(p => p.tre_children == int.Parse(Request.Form["m2"])).ToList();
                    if (tem_parent[0].tre_parent != tem_children[0].tre_parent)//不在同一目录下
                    {
                        tem_db.ExecuteCommand("update treestruct set tre_parent={0} where tre_children={1};",tem_parent[0].tre_parent, tem_children[0].tre_children);
                    }
                }
            }
            else if (Request.Form["operation"] == "update")
            {
                using (TreeBuildDataContext tem_db = new TreeBuildDataContext())
                {
                    tem_db.ExecuteCommand("update treetable set tre_name={0} where tre_id={1};", Request.Form["m2"], Request.Form["m1"]);
                }
            }
            else if (Request.Form["operation"] == "addborther" || Request.Form["operation"] == "addchildren")
            {
                using (TreeBuildDataContext tem_db = new TreeBuildDataContext())
                {
                    tem_db.ExecuteCommand("insert into treetable values({0},'新建节点')", Request.Form["m2"]);
                    tem_db.ExecuteCommand("insert into treestruct values({0},{1})", Request.Form["m1"], Request.Form["m2"]);
                }
            }
            else if (Request.Form["operation"] =="delete")
            {
                using (TreeBuildDataContext tem_db = new TreeBuildDataContext())
                {
                    List<int> L = JsonHelper.Jso_DeJSON<List<int>>(Request.Form["m1"]);
                    var tem_struct = from a in tem_db.treestruct
                                    orderby a.tre_parent ascending
                                    where L.Contains(a.tre_children)
                                    select a;
                    var tem_table = from b in tem_db.treetable
                                    orderby b.tre_id ascending
                                    where L.Contains(b.tre_id)
                                    select b;
                    tem_db.treestruct.DeleteAllOnSubmit(tem_struct);
                    tem_db.treetable.DeleteAllOnSubmit(tem_table);
                    tem_db.SubmitChanges();
                }
            }
            return null;
        }

        //获取现在所要增加节点的id
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Getid()
        {
            using (TreeBuildDataContext tem_db = new TreeBuildDataContext())
            {
                var p = (from q in tem_db.treetable
                         select q.tre_id).Max()+1;
                Response.Write("{success:true,tre_id:\""+p+"\"}");
            }
            Response.End();
            Response.Clear();
            return null;
        }

 

5、总结:

1、在假期特别多的这段时间里懒惰了挺长时间的。

2、主要学会了一些前台遍历的方法。

3、在上篇中的园友建议中,懂得了很多方法和其用法、构造方法并不是很好。

4、算法方面的能力得加强,毕竟有些算法在这一领域上已经用了几十年,思想的浓缩才是学习的精华。

 

6、以下是相关配置和使用的环境:

win7系统

vs2008 sp1

SQL2005

.NET FrameWork3.5

extjs3.2版本

7、下载相关

因为源代码没有加入Extjs所以这部分添加麻烦读者自己到它们的官方网站下载了。

Extjs官方下载包:http://www.sencha.com/products/js/

源代码下载包:https://files.cnblogs.com/yongfeng/TreePanel_Last.rar

原文地址:https://www.cnblogs.com/yongfeng/p/1846434.html