jQuery树控件zTree使用方法详解(一)

2020-05-18 09:00:15易采站长站整理

lastNodeList[i].highlight = false;
zTree.updateNode(lastNodeList[i]);
}
zTree.expandAll(false); //全部收缩
if (nodeList.length > 0) {
for (var i = 0, l = nodeList.length; i < l; i++) { //遍历找到的节点集合
if (nodeList[i].getParentNode()) {
zTree.expandNode(nodeList[i].getParentNode(), true, false, false); //展开其父节点
}
nodeList[i].highlight = true;
zTree.updateNode(nodeList[i]);
}
}
zTree.refresh(); // 很重要,否则节点状态更新混乱。
lastNodeList = nodeList;
lastKey = key;
}
}

//加载数据
function loadData() {
var zTree = $.fn.zTree.getZTreeObj("tree");
var rootNodes = zTree.getNodes();
reloadFlag = true;
checkFlag = false;
for (var i = 0; i < rootNodes.length; i++) {
if (!rootNodes[i].children) {
zTree.reAsyncChildNodes(rootNodes[i], "refresh"); //异步加载
}
}
}

//全部收缩
function closeAll() {
var zTree = $.fn.zTree.getZTreeObj("tree");
if ($("#inputCloseAll").val() == "全部收缩") {
zTree.expandAll(false);
$("#inputCloseAll").val("全部展开")
}
else {
zTree.expandAll(true);
$("#inputCloseAll").val("全部收缩")
}
}

//高亮样式
function getFontCss(treeId, treeNode) {
return (treeNode.highlight) ? { color: "#A60000", "font-weight": "bold"} : { color: "#333", "font-weight": "normal" };
}
</script>

html:


<div style="width: 200px; height: 260px; overflow: auto; border: solid 1px #666;">
<ul id="tree" class="ztree">
</ul>
</div>

后台代码(后台返回Json数据):


public void SelStudent()
{
set("getStudentsJsonUrl", to(GetStudentsJson));
}

public void GetStudentsJson()
{
List<Dictionary<string, string>> dicList = new List<Dictionary<string, string>>();

string level = ctx.Post("level");
string id = ctx.Post("id");
if (strUtil.IsNullOrEmpty(id))
{
#region 加载班级
//获取当前登录用户
Sys_User user = AdminSecurityUtils.GetLoginUser(ctx);
//获取当前用户关联的老师
Edu_Teacher teacher = edu_TeacService.FindByUserId(user.Id);
//获取班级集合
List<Edu_ClaNameFlow> list = edu_ClaNameFlowService.GetListByTeacherId(teacher.Id);
foreach (Edu_ClaNameFlow item in list)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("id", "level0" + item.Calss.Id.ToString());
dic.Add("pId", "0");
dic.Add("name", item.Gra.Name + item.Calss.Name);
dic.Add("isParent", "true");
dicList.Add(dic);
}
#endregion
}
else
{
if (level == "0")
{
//加载学生
List<Edu_Student> list = edu_StudService.GetListByClassId(id.Replace("level0", ""));