作者:秋名
撰写时间:2020 年05 月05日
需求:
1,计算父节点里有几个子节点,显示数量
** 后台CS **
/// <summary>
public class TreeVo //计算当前父节点,有几个子节点
{
public int? ParentID { get; set; }
public int? SonID { get; set; }
public string NodeName { get; set; }
public int? SupplierParentID { get; set; }
public int? SupplierSonID { get; set; }
}
///权限组用户树形图
/// </summary>
/// <returns></returns>
public JsonResult Get_TreeView()
{
List<Dictionary<string, object>> jsonlist = new List<Dictionary<string, object>>();
List<TreeVo> treeList = new List<TreeVo>();
var listSuShe = (from TbTrees in myModel.HB宿管楼栋
select new
{
宿舍楼栋ID = TbTrees.宿舍楼栋ID,//父亲id
宿舍楼栋MC = TbTrees.宿舍楼栋MC.Trim(),儿子id
宿舍楼层ID = TbTrees.宿舍楼层ID//节点名
}).ToList();
for (int i = 0; i < listSuShe.Count; i++)
{
TreeVo tree = new TreeVo();
tree.ParentID = listSuShe[i].宿舍楼栋ID;//父亲id
tree.SonID = listSuShe[i].宿舍楼层ID;//儿子id
if (listSuShe[i].宿舍楼层ID == 0)
{
int J = int.Parse(tree.ParentID.ToString());
int Count = (from tbJurisdictionGroupUser in myModel.HB宿管楼栋
where tbJurisdictionGroupUser.宿舍楼层ID == J
select tbJurisdictionGroupUser).Count();
tree.NodeName = listSuShe[i].宿舍楼栋MC.ToString().Trim() + "(" + Count + ")";//节点名
}
else
{
tree.NodeName = listSuShe[i].宿舍楼栋MC.ToString().Trim();//节点名
}
treeList.Add(tree);
}
foreach (var model in treeList)
{
Dictionary<string, object> jsonobj = new Dictionary<string, object>();
jsonobj.Add("id", model.ParentID); //父亲id
jsonobj.Add("pId", model.SonID);//儿子id
jsonobj.Add("name", model.NodeName);//节点名称
//jsonobj.Add("icon", "");
jsonlist.Add(jsonobj);
}
return Json(jsonlist, JsonRequestBehavior.AllowGet);
}
}
Html
<div class="zTreeDemoBackground left">
<ul id="tree" class="ztree" style="width:200px; margin:0 5%;background: #fff;border:none;"></ul>
</div>
Js
function TreeView() {
$.ajax({
type: "Get",
url: "Get_TreeView",//请求后台数据
async: false,
success: function (data) {
console.log(data)
var treeObj= $.fn.zTree.init($("#tree"), setting, data);
treeObj.expandAll(true);//是否默认展开节点
}
});
}
var setting = {
check: {
enable: false,
chkStyle: "checkbox",
chkboxType: { "Y": "ps", "N": "ps" },
isSimpleData: true, //数据是否采用简单 Array 格式,默认false
treeNodeKey: "id", //在isSimpleData格式下,当前节点id属性
treeNodeParentKey: "pId", //在isSimpleData格式下,当前节点的父节点id属性
showLine: true, //是否显示节点间的连线
checkable: true
},
data: {
simpleData: {
enable: true
}
},
callback: {
onClick: zTreeOnClick
}
};
function zTreeOnClick(event, treeId, treeNode) {
console.log(treeNode)
}