主要在Json字符串的生成,必须生成tree所需要的特定json字符格式。创建EasyTreeData类用于生成json字符串
namespace JPKL.Common
{
[DataContract]
[Serializable]
public class EasyTreeData
{///
/// ID
///
[DataMember]
public string id { get; set; }
///
/// 节点名称
///
[DataMember]
public string text { get; set; }
///
/// 是否展开
///
[DataMember]
public string state { get; set; }
///
/// 图标样式
///
[DataMember]
public string iconCls { get; set; }
///
/// 子节点集合
///
[DataMember]
public List children { get; set; }
///
/// 默认构造函数
///
public EasyTreeData()
{
this.children = new List();
this.state = "open";
}
///
/// 常用构造函数
///
public EasyTreeData(string id, string text, string iconCls = "", string state = "open")
: this()
{
this.id = id;
this.text = text;
this.state = state;
this.iconCls = iconCls;
}
///
/// 常用构造函数
///
public EasyTreeData(decimal id, string text, string iconCls = "", string state = "closed")
: this()
{
this.id = id.ToString();
this.text = text;
this.state = state;
this.iconCls = iconCls;
}
}
}
如何使用EasyTreeData类?
List treeList = new List();//创建一个树类集合
List category = CategoryService.GetCategorys(id);//获取所需要数据的集合
foreach (CATEGORY info in category)//将所需要数据的集合转化为树类集合
{
treeList.Add(new EasyTreeData(info.CATEGORY_ID, info.CATEGORY_NAME, "icon-user"));
}
string json = new JavaScriptSerializer().Serialize(treeList);
return Content(json);