private List<TreeEntity> treeList = new List<TreeEntity>();//创建一个树的List集合 public ActionResult GetTree1Json(string id) { var root = new DataItemBLL().GetEntity(id);//树的根节点 TreeEntity tree = new TreeEntity();//创建一个树的实体对象 tree.id = root.ItemId;//根节点树的ID tree.text = root.ItemName;//根节点要在前端显示的内容 tree.value = root.ItemCode;//根节点的值 tree.isexpand = true;//是否需要展开 tree.complete = true; tree.hasChildren = true;//是否还有子节点 tree.parentId = "0";//根节点的父ID必须为0才会显示 treeList.Add(tree);//把树放到树的List集合里面去 set(id); void set(string ia) {//以此类推 var datalist = messagesendbll.GetDataList(ia); foreach (DataItemEntity die in datalist) { tree = new TreeEntity(); tree.id = die.ItemId; tree.text = die.ItemName; tree.showcheck = true; tree.isexpand = false; tree.complete = true; tree.hasChildren = true; tree.parentId = die.ParentId; treeList.Add(tree); var de4 = messagesendbll.GetDataItemList(die.ItemId); foreach (DataItemDetailEntity dde in de4) { TreeEntity tree4 = new TreeEntity(); tree4.id = dde.ItemDetailId; tree4.text = dde.ItemName; tree4.value = dde.ItemCode; tree4.Attribute = "score"; tree.AttributeValue = dde.ItemCode; tree4.showcheck = true; tree4.isexpand = false; tree4.complete = true; tree4.parentId = dde.ItemId; treeList.Add(tree4); } set(die.ItemId); } } return Content(treeList.TreeToJson()); }
前端js代码:
//初始化数据 function initControl() { var item = { showcheck: true, url: "../../SPManage/SP_WConsult/GetTree1Json", }; $("div").treeview(item); }