[jillzhang]ExtJs与WCF交互:生成树 --数据库版补充
原文见:
http://www.cnblogs.com/jillzhang/archive/2008/06/15/1222233.html
已经讲得很清楚,本文只是将该篇WCF的方法换成数据库版
1.分类表T_Class主要结构
F_ID 主键ID
F_ClassName 分类名称
F_ParentID 父类ID
2.扩展treeNode类
[DataContract]
public class treenode
{
[DataMember]
public string id;
[DataMember]
public string text;
[DataMember]
public List<treenode> children = new List<treenode>();
[DataMember]
public string cls;
[DataMember]
public bool leaf;
[DataMember]
public string parentid;
}
public class treenode
{
[DataMember]
public string id;
[DataMember]
public string text;
[DataMember]
public List<treenode> children = new List<treenode>();
[DataMember]
public string cls;
[DataMember]
public bool leaf;
[DataMember]
public string parentid;
}
即增加了一个parentid成员
3.修改WCF中的方法
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
public treenode[] GetTree()
{
using (DBDataContext db = new DBDataContext())
{
if (db.Connection != null && db.Connection.State != System.Data.ConnectionState.Open)
{
db.Connection.Open();
}
var query = db.T_Classes.Select(c => new { id = c.F_ID, name = c.F_ClassName, parentid = c.F_ParentID });
foreach (var u in query)
{
treenode _node = new treenode();
_node.id = u.id.ToString().ToLower();
_node.parentid = u.parentid.ToString().ToLower();
_node.text = u.name;
int i = db.T_Classes.Count(c => c.F_ParentID == u.id);
if (i > 0)
{
_node.leaf = false;
_node.cls = "folder";
}
else
{
_node.leaf = true;
_node.cls = "file";
}
GenTreeNode(NodeLists, _node);
}
db.Connection.Close();
}
return NodeLists.ToArray();
}
public List<treenode> NodeLists = new List<treenode>();
public List<treenode> GenTreeNode(List<treenode> nodeList, treenode node)
{
bool flag = false;
foreach (treenode n in nodeList)
{
if (n.id == node.parentid)
{
n.children.Add(node);
flag = true;
break;
}
}
if (!flag)
{
nodeList.Add(node);
}
return nodeList;
}
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
public treenode[] GetTree()
{
using (DBDataContext db = new DBDataContext())
{
if (db.Connection != null && db.Connection.State != System.Data.ConnectionState.Open)
{
db.Connection.Open();
}
var query = db.T_Classes.Select(c => new { id = c.F_ID, name = c.F_ClassName, parentid = c.F_ParentID });
foreach (var u in query)
{
treenode _node = new treenode();
_node.id = u.id.ToString().ToLower();
_node.parentid = u.parentid.ToString().ToLower();
_node.text = u.name;
int i = db.T_Classes.Count(c => c.F_ParentID == u.id);
if (i > 0)
{
_node.leaf = false;
_node.cls = "folder";
}
else
{
_node.leaf = true;
_node.cls = "file";
}
GenTreeNode(NodeLists, _node);
}
db.Connection.Close();
}
return NodeLists.ToArray();
}
public List<treenode> NodeLists = new List<treenode>();
public List<treenode> GenTreeNode(List<treenode> nodeList, treenode node)
{
bool flag = false;
foreach (treenode n in nodeList)
{
if (n.id == node.parentid)
{
n.children.Add(node);
flag = true;
break;
}
}
if (!flag)
{
nodeList.Add(node);
}
return nodeList;
}
运行效果图如下:
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。