ASP.net MVC、Extjs多级目录(理论上可以无限级、非递归)treepanel显示和数据库(多级目录)存储结构的设计(下篇)
2010-10-09 11:24 夜雨瞳 阅读(1428) 评论(0) 编辑 收藏 举报1、添加节点、子节点、编辑节点
1.1、添加节点
1.2、添加子节点
2、移动节点或者目录
3、删除目录和节点
其中,遍历的情况有2种。
通过后台获取的(这里是一次性获取)节点,在该树的attributes内。
从前台操作增加的节点,在childNodes内。
这里遍历的代码片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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、节点操作代码片段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | //节点的操作 [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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步