【笔记】ztree的使用

时间:2022-07-13 15:49:32

引用的js和css:

<!-- zTreeJS -->
<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="zTree/js/jquery.ztree.core-3.2.js"></script>
<script type="text/javascript" src="zTree/js/jquery.ztree.excheck-3.2.js"></script>
<script type="text/javascript" src="zTree/js/jquery.ztree.exedit-3.2.js"></script>

//异步加载节点
var setting = {
data: {
key : {
name : "name"
},
simpleData: {
enable: true,
idKey: "id",
pIdKey: "pId",
rootPId: 0
}
},
async: {
//异步加载
enable: true,
url: "${ctx}/admin/learn/course!publicCourseTree.do",
autoParam : [ "id" ],
dataType : "json",
type : "post"
},
check : {
enable : false,
chkStyle : "radio",
radioType : "level"
},
callback: {
beforeExpand: beforeExpand,
onAsyncSuccess: onAsyncSuccess,
onAsyncError: onAsyncError,
onClick : function(event, treeId, treeNode) {
var courseID = treeNode.id;
$("#courseID").val(courseID);
$.post("${ctx}/admin/learn/course!getCourseTreeInfo.do?id="+courseID,
{"parm":""},
function(data) {
$.each(data, function(i,item) {
$("#codeSM").html(item.id);
$("#name").html(item.name);
$("#flag").val(item.status);
$("#description").html(item.description);
$("#createtime").html(item.createtime);
if(item.status=="1"){
$("#updateflag").val("取消发布");
$("#status").html("已发布");
}else{
$("#updateflag").val("发布");
$("#status").html("未发布");
}
if(item.imgurl!=null){
$("#img").attr("src","${ctx}/uploads/"+item.imgurl);
}else{
$("#img").attr("src","");
}
});
}, "json");
}
}
};

function beforeExpand(treeId, treeNode) {
if (!treeNode.isAjaxing) {
return true;
} else {
alert("zTree 正在下载数据中,请稍后展开节点。。。");
return false;
}
}
function onAsyncSuccess(event, treeId, treeNode, msg) {


}
function onAsyncError() {
alert(" 数据加载失败");
}

function createTree() {
$.ajax({
url: "${ctx}/admin/learn/course!publicCourseTree.do", //url action是方法的名称
data: { id: -1 },
type: 'Get',
dataType: "json", //可以是text,如果用text,返回的结果为字符串;如果需要json格式的,可是设置为json
success: function (data) {
$.fn.zTree.init($("#_courseTreeSM"), setting, eval(data));
},
error: function (msg) {
alert(" 数据加载失败!" + msg);
}
});
}

$(document).ready(function () {
createTree();
});

//删除
var tree = $.fn.zTree.getZTreeObj("_stationTreeSM");
var node = tree.getNodeByParam("ID", response.id, null);
tree.removeNode(node);
var node = treeObj.getNodeByParam("ID", response.parentid, null);
//添加
treeObj.addNodes(node, {ID : response.newStationID, NAME : $("#nameAS").val()});
//修改
node.NAME = $('#nameAS').val();
treeObj.updateNode(node);
public List<Map<String, Object>> getPublicCourseTree(String id, Integer orgID)
{
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
if (null == id && !"".equals(id)){
String hql="SELECT t.id, t.name, t.parentid as pid,t.description,DATE_FORMAT(t.createtime, '%Y-%m-%d') createtime,t.status,t.imgurl,(SELECT COUNT(t2.id) FROM c_publiccourse_tab t2 WHERE t2.parentid = t.id) coun FROM c_publiccourse_tab t WHERE t.PARENTID = -1 and t.orgid='"+orgID+"'";
Query query = super.createSQLQuery(hql).addScalar( "id" , Hibernate.STRING).addScalar( "name" , Hibernate.STRING).addScalar( "pid" , Hibernate.STRING)
.addScalar( "description" , Hibernate.STRING).addScalar( "createtime" , Hibernate.STRING).addScalar( "status" , Hibernate.STRING).addScalar( "imgurl" , Hibernate.STRING).addScalar( "coun" , Hibernate.STRING).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
list1=query.list();
}else{
String hql = "SELECT t.id, t.name , t.parentid as pid,t.description,DATE_FORMAT(t.createtime, '%Y-%m-%d') createtime,t.status,t.imgurl,(SELECT COUNT(t2.id) FROM c_publiccourse_tab t2 WHERE t2.parentid = t.id) coun FROM c_publiccourse_tab t WHERE t.PARENTID ='" + id +"' and t.orgid='"+orgID+"' order by t.id asc";
Query query = super.createSQLQuery(hql).addScalar( "id" , Hibernate.STRING).addScalar( "name" , Hibernate.STRING).addScalar( "pid" , Hibernate.STRING)
.addScalar( "description" , Hibernate.STRING).addScalar( "createtime" , Hibernate.STRING).addScalar( "status" , Hibernate.STRING).addScalar( "imgurl" , Hibernate.STRING).addScalar( "coun" , Hibernate.STRING).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
list1=query.list();
}
for (Map<String, Object> map : list1){
map.put("open", true);
String parentid = map.get("pid").toString();
if (parentid.equals("-1")){
map.put("open", true);
int count = Integer.parseInt(map.get("coun").toString());
if (count > 0){
map.put("isParent", true);
}else{
map.put("isParent", false);
}
}else{
int count = Integer.parseInt(map.get("coun").toString());
if (count > 0){
map.put("isParent", true);
}
}
list2.add(map);
}
return list2;
} List<Map<String, Object>> data=publicCourseManager.getPublicCourseTree2(id,orgID);
try
{
response.getWriter().write(JSONArray.fromObject(data).toString());
} catch (IOException e)
{
e.printStackTrace();
}