62,046
社区成员
发帖
与我相关
我的任务
分享
function initTree()
{
$('#leftTree').tree({
animate: false,
checkbox: false,
url: '/Home/TreeView',
onClick: function (node) {
if (node.url != "") {
//addTab(node.text, node.url, node.icon); 点击节点
}
},
});
}
生成JSON格式
public static string GetMenu(List<TreeNode> list)
{
StringBuilder json = new StringBuilder();
if (list.Count > 0)
{
json.Append("[");
foreach (TreeNode dr in list)
{
json.Append("{\"id\":" + dr.ID.ToString());
json.Append(",\"text\":\"" + dr.Text + "\"");
json.Append(",\"state\":\"open\"");
json.Append(",\"url\":\"" + dr.Url + "\"");
if (dr.Nodes.Count > 0)
{
json.Append(",\"children\":");
json.Append(GetMenu(dr.Nodes));
}
json.Append("},");
}
json.Remove(json.Length - 1, 1);
json.Append("]");
}
return json.ToString();
}
public class TreeNode
{
public int ID { get; set; }
public string Text { get; set; }
public string State { get; set; }
public string Url { get; set; }
public string Icon { get; set; }
public List<TreeNode> Nodes { get; set; }
public TreeNode()
: this(string.Empty)
{
}
public TreeNode(string text)
: this(0, text, string.Empty, string.Empty)
{
}
public TreeNode(int id,string text)
: this(id, text, string.Empty, string.Empty)
{
}
public TreeNode(string text, string url)
: this(0, text, string.Empty, url)
{
}
public TreeNode(int id,string text, string url)
: this(id,text, string.Empty, url)
{
}
public TreeNode(int id,string txt, string icon, string url)
{
this.ID = id;
this.Text = txt;
this.State = "open";
this.Url = url;
this.Icon = icon;
Nodes = new List<TreeNode>();
}
}