无法将Json数组从JSP动态加载到Jstree
我试图创建一个简单的LDAP客户机,以便仅从LDAP服务器检索数据。我从JSP返回JSON对象数组。单击任何值,我将从在线服务器获取一些数据。我能够将第一组数组加载到树中。下一步得到的数组不会附加到JSTree。我的代码:无法将Json数组从JSP动态加载到Jstree,json,jsp,model-view-controller,jstree,jtree,Json,Jsp,Model View Controller,Jstree,Jtree,我试图创建一个简单的LDAP客户机,以便仅从LDAP服务器检索数据。我从JSP返回JSON对象数组。单击任何值,我将从在线服务器获取一些数据。我能够将第一组数组加载到树中。下一步得到的数组不会附加到JSTree。我的代码: function getGroupsStructure(id) { console.log("in getGroupsStructure-->"); var paramId = ""; if(id == '') { console.log("in if--&g
function getGroupsStructure(id) {
console.log("in getGroupsStructure-->");
var paramId = "";
if(id == '') {
console.log("in if-->");
paramId = "c=de";
} else {
console.log("in else-->");
paramId = id;
}
var params = {
"DN" : paramId,
};
console.log("params-->",params);
var getGroupsStructureForUserService = service(webURL + "sendingValues/getGroupsStructureForUser",params,"POST");
getGroupsStructureForUserService.success(function(data) {
console.log("in success-->dta-->",data);
if(data.errorCode == '0') {
console.log("in error code 0-->dta-->",data.treeData);
$('.treeNode').jstree({
'core': {
'data': function (obj, cb) {
cb.call(this,
data.treeData);
}
}
});
console.log("Tree Created...");
} else {
console.log("error code not 0--data-->",data);
}
$(document).off('click').on('click', '.treeNode a', function() {
console.log("on click of a-->");
var id = $(this).parent().attr('id');
console.log("id-->",id);
getGroupsStructure(id);
console.log("after getGroupsStructure");
});
});
getGroupsStructureForUserService.error(function(data) {
console.log(" empty error");
// console.log(err);
});
}
JSP代码是
def NextLevelLDAP(String DN) {
// println "Next Level===>"
assert ldap!=null
def responseArray=[]
def results=ldap.search('objectClass=*',DN,SearchScope.ONE) //Will be triggered when + is pressed in GUI to get next level of tree
// assert results==null
if(DN.startsWith("c="))
{
JSONObject responseJson1=new JSONObject()
responseJson1.put("id", initialDN )
responseJson1.put("parent", "#")
responseJson1.put("text","Parent")
responseArray.add(responseJson1)
for(entry in results) {
// println entry
// println "In NextLevel Using InitialDN"
JSONObject responseJson=new JSONObject()
responseJson.put("id", entry.dn)
responseJson.put("parent", DN)
String tempResDN=entry.dn.toString()
def tempLength=tempResDN.length() - DN.length()
// println tempResDN
String tempName=tempResDN.substring(2,tempLength-1)
// println tempName
responseJson.put("text",tempName)
responseArray.add(responseJson)
// println entry
println responseJson.toString()
}
return responseArray
}
if(results.size!=0)
{
for(entry in results) {
println entry
JSONObject responseJson=new JSONObject()
responseJson.put("id", entry.dn)
responseJson.put("parent", DN)
String tempResDN=entry.dn.toString()
def tempLength=tempResDN.length() - DN.length()
// println tempResDN
String tempName=tempResDN.substring(2,tempLength-1)
println tempName
responseJson.put("text",tempName)
responseArray.add(responseJson)
// println entry
}
return responseArray
}
}
请忽略获取父ID的方式。这很复杂。
请帮助我了解如何动态创建树节点。我刚刚到达树的第一层。单击其他级别时的数据将显示在控制台中,但不会附加到树上
谢谢。您的方法正好相反-您需要创建树并让它为您发出请求,因此,请不要这样做:
'data': function (obj, cb) {
cb.call(this, data.treeData);
}
使用类似以下内容:
'data': function (obj, cb) {
// you probably need to pass the obj.id as a parameter to the service
// keep in mind if obj.id is "#" you need to return the root nodes
service(...).success(function (data) {
cb.call(this, data.treeData);
});
}
function load(id) {
var params = {
"DN" : id && id !== '#' ? id : "c=de"
};
return service(webURL + "sendingValues/getGroupsStructureForUser", params, "POST");
}
$('#tree')
.jstree({
'core' : {
'data': function (obj, cb) {
load(obj.id).success(function (data) {
cb.(data.treeData);
});
}
}
})
.on('select_node.jstree', function (e, data) {
data.instance.open_node(data.node);
});
这样,您就不需要每次都分离和重新附着单击处理程序,而且打开节点时它将立即生效。如果要在单击时打开节点,可以使用以下方法:
$('#tree').on('select_node.jstree', function (e, data) {
data.instance.open_node(data.node);
});
因此,您的整个代码应该如下所示:
'data': function (obj, cb) {
// you probably need to pass the obj.id as a parameter to the service
// keep in mind if obj.id is "#" you need to return the root nodes
service(...).success(function (data) {
cb.call(this, data.treeData);
});
}
function load(id) {
var params = {
"DN" : id && id !== '#' ? id : "c=de"
};
return service(webURL + "sendingValues/getGroupsStructureForUser", params, "POST");
}
$('#tree')
.jstree({
'core' : {
'data': function (obj, cb) {
load(obj.id).success(function (data) {
cb.(data.treeData);
});
}
}
})
.on('select_node.jstree', function (e, data) {
data.instance.open_node(data.node);
});
只需确保将返回的节点标记为有子节点(将其
children
属性设置为booleantrue
)。如果展开时的树节点没有任何子节点,那么它不应该自行终止吗?这实际上是一个错误。如果将空Json发送回树,节点会自动终止,有没有办法?i、 e组没有子节点。