public ActionResult GetTreeJson()
{
//查询一级菜单
DataTable TreeOne = DBHelper.getDataTable("select * from menu where parentid=0 and status=1");
//查询按钮
DataTable Buttion = DBHelper.getDataTable("select * from button where status=1");
var menuList = new List<TreeEntity>();
foreach (DataRow item in TreeOne.Rows)
{
TreeEntity tree = new TreeEntity
{
//icon = item["icon"].ToStr(),
id = "y:" + item["id"].ToInt32(),
label = item["mname"].ToStr(),
//sort = item["sort"].ToStr(),
pid = "0",
//url = item["path"].ToStr(),
//spread = true,
};
//查询二级菜单
DataTable TreeTwo = DBHelper.getDataTable("select * from menu where parentid=?parentid and status=1", DBHelper.CreateParameter("parentid", item["id"].ToInt32()));
var childrenList = new List<TreeEntity>();
foreach (DataRow ChildItem in TreeTwo.Rows)
{
TreeEntity ChindrenTree = new TreeEntity
{
//icon = ChildItem["icon"].ToStr(),
id = "e:" + ChildItem["id"].ToInt32() + ",y:" + item["id"].ToInt32(),
label = ChildItem["mname"].ToStr(),
// sort = ChildItem["sort"].ToStr(),
pid = ChildItem["parentid"].ToStr(),
//url = ChildItem["path"].ToStr(),
//children = null,
// spread = true,
};
var childrenButtons = new List<TreeEntity>();
foreach (DataRow btns in Buttion.Rows)
{
TreeEntity treebtn = new TreeEntity
{
id = "a:" + btns["id"].ToInt32() + ",e:" + ChildItem["id"].ToInt32(),
label = btns["btnname"].ToStr()
};
childrenButtons.Add(treebtn);
}
ChindrenTree.children = childrenButtons;
childrenList.Add(ChindrenTree);
//childrenButtons.Clear();
}
tree.children = childrenList;
menuList.Add(tree);
}
string json = @"{""status"":""0"",""data"":" + JsonConvert.SerializeObject(menuList) + "}";
var menuJson = Newtonsoft.Json.JsonConvert.SerializeObject(json);
return Json(json);
}
查询数据
后续