Rad*_*dek 6 jquery jquery-plugins jstree
我想进行ajax调用以获取results节点的数据.在我的示例代码(见这里)中进行了ajax调用,但服务器没有返回任何东西(使用firebug测试)但是如果我在Web浏览器中使用相同的url,我可以保存json文件.
我的问题是:
Using both the data & ajax config options请参阅下面的代码或使用小提琴
<html>
<head>
<title>jsTree & ajax</title>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/_docs/syntax/!script.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.cookie.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.hotkeys.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0pre/jquery.jstree.js"></script>
<script type='text/javascript'>
data = [
{
"data" : "Basics",
"state" : "closed",
"children" : [ {
"data" : "login",
"state" : "closed",
"children" : [ "login", {"data" : "results", "state" : "closed"} ]
} ,
{
"data" : "Basics",
"state" : "closed",
"children" : [ "login", "something",{"data" : "results", "state" : "closed"} ]
} ]
},
{
"data" : "All",
"state" : "closed",
"children" : [ {
"data" : "AddCustomer",
"state" : "closed",
"children" : [ "login","Add", {"data" : "results", "state" : "closed"} ]
} ]
}
]
$(function () {
$("#jstree").jstree({
"json_data" : {
"data" : data ,
"ajax" : { "url" : "http://www.jstree.com/static/v.1.0pre/_docs/_json_data.json" }
},
"plugins" : [ "themes", "json_data" ]
});
});
</script>
</head>
<body>
<div id="jstree"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
即使我将示例代码从jstree.com复制到jsfiddle也无法正常工作.我想我错过了什么......
Spa*_*key 12
尽量不要从服务器到另一台服务器进行Ajax调用 - 这将导致跨域安全性异常.有很多方法(JSONP),但只是从您自己的服务器请求数据更简单.
一旦你整理了你的ajax请求,你可能想要为你的"结果"节点添加一些属性,以便ajax有一些可以挂钩的数据,例如ID.就像是:
"data": "results", "state": "closed", "attr": { "id": "node-123" }
Run Code Online (Sandbox Code Playgroud)
然后,您可以为ajax数据选项添加处理程序:
"ajax": {
"url": "/my/local/json/",
"data": function(n) {
// get parent / grandparent node
var lists = $(n).parents('ul');
var p = $(lists[0]).prev('a');
var gp = $(lists[1]).prev('a');
// the result is fed to the AJAX request 'data' option
return {
"parent": $.trim(p.text()),
"grandparent": $.trim(gp.text()),
"id": n.attr ? n.attr("id").replace("node-", "") : 1,
};
}
}
Run Code Online (Sandbox Code Playgroud)
这应该导致Ajax请求,例如:http:// myserver/my/local/json /?parent = login&grandparent = Basics&id = 123
希望有所帮助.
编辑:这是一个更新的JsFiddle给你:http://jsfiddle.net/robgallen/3uCX3/
| 归档时间: |
|
| 查看次数: |
16402 次 |
| 最近记录: |