需要将dataTable中的数据转换成easyui tree可识别的格式才行,参考网上的一段java代码,搞了一个c#的,
节点类:
public class Node
{
public string Id;
public string Text;
public string ParentId;
public Children children = new Children();
// 先序遍历,拼接JSON字符串
public override String ToString()
{
String result = "{"
+ "\"id\": \"" + Id + "\""
+ ", \"text\" : \"" + Text + "\"";
//if (Id.Length <= 6)
//{
// //第二级之前的就是打开状态
// result += ",\"state\":\"open\"";
//}
//else
//{
// result += ",\"state\":\"closed\"";
//}
if (children != null && children.GetSize() != 0)
{
result += ", \"children\" : " + children.ToString();
}
else
{
result += ", \"leaf\" : true";
}
return result + "}";
}
// 兄弟节点横向排序
public void sortChildren()
{
if (children != null && children.GetSize() != 0)
{
children.sortChildren();
}
}
// 添加孩子节点
public void addChild(Node node)
{
this.children.AddChild(node);
}
}
节点比较器
public class NodeIDComparator : IComparer
{
// 按照节点编号比较
public int Compare(object o1, object o2)
{
int j1 = int.Parse(((Node)o1).Id);
int j2 = int.Parse(((Node)o2).Id);
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}
孩子列表类
public class Children
{
ArrayList list = new ArrayList();
public int GetSize()
{
return list.Count;
}
public void AddChild(Node node)
{
list.Add(node);
}
// 拼接孩子节点的JSON字符串
public override String ToString()
{
String result = "[";
foreach (object node in list)
{
result += ((Node)node).ToString();
result += ",";
}
result = result.Substring(0, result.Length - 1);
result += "]";
return result;
}
// 孩子节点排序
public void sortChildren()
{
// 对本层节点进行排序
// 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
list.Sort(new NodeIDComparator());
// 对每个节点的下一层节点进行排序
foreach (object node in list)
{
((Node)node).children.sortChildren();
}
}
}
类库中使用
/// <summary>
/// 获取单位结构的json树
/// </summary>
/// <returns></returns>
public string ListUnitJsonTree()
{
Hashtable data = new Hashtable();
DataTable dt = ListLastGradUnit();
foreach (DataRow dr in dt.Rows)
{
RecursionJson.Node node = new RecursionJson.Node();
node.Id = dr["unitid"].ToString();
node.Text = dr["unitname"].ToString();
node.ParentId = dr["fartherid"].ToString();
data.Add(node.Id, node);
}
RecursionJson.Node root = null;
foreach (DictionaryEntry de in data)
{
RecursionJson.Node node = (RecursionJson.Node)de.Value;
if (node.Id == "001")//根节点
{
root = node;
}
else
{
RecursionJson.Node parent = (RecursionJson.Node)data[node.ParentId];
if (parent != null)
{
parent.addChild(node);
}
}
}
return "["+root.ToString()+"]";
}
前台界面:
html部分
<ul id="utree"></ul>
js部分
function InitTreeData() {
$('#utree').tree({
url: '/sysset/listUnitTree',
checkbox: true,
onlyLeafCheck: true,
onClick: function (node) {
//alert(node.text);
},
onLoadSuccess: function (node, data) {
$('#utree').tree('collapseAll');
var root = $('#utree').tree('getRoot');//展开全校
$('#utree').tree('expand', root.target);
var childs = $('#utree').tree('getChildren', root.target);
for (var i = 0; i < childs.length; i++) {
if (childs[i].id.length == 6) {
//展开第二级
$('#utree').tree('expand', childs[i].target);
}
}
},
onCheck: function (node, checked) {
if (!checked) return;//只有选中时才进行以下操作,否则因变动其他同级的checked状态,会进入死循环
var tree=$('#utree');
var nianji = tree.tree('getParent', node.target);
var zhuanye = tree.tree('getParent', nianji.target);
//提交到数据库
$.post("addMajor", {
mid: zhuanye.id,
mname: zhuanye.text,
nid: nianji.id,
nname: nianji.text,
cid: node.id,
cname: node.text
}, function (data) {
loadMajor();
});
//
var childs = tree.tree('getChildren', nianji.target);
for(var i=0;i<childs.length;i++)
{
if(childs[i].id!=node.id)
{
tree.tree('uncheck', childs[i].target);
}
}
}
});
}
$(document).ready(function () {
InitTreeData();
});