jstree选择节点
问候, 我正在使用 jsTree 生成分层数据。 JsTree 生成如下:
$(function() {
$("#industries").tree({
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
它可以查找并且 jsonresult 类似于:
[{"attributes":[],"data":{"title":"Adwokaci, Notariusze","id":"1a051101-c3fa-48f2-b2e1-c60d1b67ea22"},"children":[{"attributes":[],"data":{"title":"Kancelarie adwokackie","id":"26d6cff1-3c7f-4a2f-bf5a-422e08127b43"
我的问题是:如何将选定节点的 id 保存在某个隐藏字段中?我做了这样的事情:
<script type="text/javascript">
$("#industries").click(function() {
var tree = $.tree.reference("industries");
var t = $.tree.focused(); if (t.selected) t.selected; else alert("Select a node first");
alert(t.id);
});
但它不起作用。我进入警报窗口“未定义”。有人可以帮我吗?
编辑: 我已经更改了 jstree 实例,如下所示:
$(function() {
$("#industries").tree({
callback: {
onselect: function(NODE, TREE_OBJ) {
alert(NODE.id);
}
},
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
并且我得到空警报
Greetings,
I am using jsTree to generatate my hierarchical data. JsTree is generated as follows:
$(function() {
$("#industries").tree({
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
it works find and the jsonresult is something like:
[{"attributes":[],"data":{"title":"Adwokaci, Notariusze","id":"1a051101-c3fa-48f2-b2e1-c60d1b67ea22"},"children":[{"attributes":[],"data":{"title":"Kancelarie adwokackie","id":"26d6cff1-3c7f-4a2f-bf5a-422e08127b43"
my question is: how can I save id of selected node in some hidden field? I do something like this:
<script type="text/javascript">
$("#industries").click(function() {
var tree = $.tree.reference("industries");
var t = $.tree.focused(); if (t.selected) t.selected; else alert("Select a node first");
alert(t.id);
});
but it does not work. I get in my alert window "undefined". Can someone please help me?
EDIT:
I've changed the jstree instance as follows:
$(function() {
$("#industries").tree({
callback: {
onselect: function(NODE, TREE_OBJ) {
alert(NODE.id);
}
},
data: {
type: "json",
opts: {
url: "/Admin/GetIndustries/"
}
}
});
});
and i get empty alertt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
或者只是绑定选择节点:
尝试查看变量 a 的 ID 或 NODE。我实际上是使用 REF_NODE 来获取
Or just bind the select node:
Try looking at the variable a for the ID, or NODE. I was actually using REF_NODE to get
我没有检查所有答案,但看到您没有选择任何答案,因此决定发布一个对我有用的方法,
即如果您想要节点的 id 和标题。希望这有帮助
I did not check all the answers but saw that you didnt select any so decided to post a method that worked for me
this is if you want the id and the title of the node. hope this helps
您可以在
bind()
函数中使用它,如下所示:与取消选中操作相同。
You can use it in your
bind()
function like this:The same with uncheck action.
尝试这里提到的回调: http://www.jstree.com/documentation
它应该像这样工作这:
Try the callbacks mentioned here: http://www.jstree.com/documentation
It should work something like this:
现在您已经更改了代码以使用 onselect 回调,您还有问题吗?我无法判断您的编辑是否意味着您已经解决了问题。如果有,您应该将此问题标记为已回答。
就我个人而言,我使用 onchange 而不是 onselect,但我确信两者都可以。您还可以考虑使用 jquery 的 data() 功能来存储值以及页面上的元素隐藏字段的,除非您想要提交包含该值的表单。
但如果您需要将其保存到隐藏字段,请尝试如下操作:
Now that you've changed your code to use the onselect callback, are you still having problems? I can't tell if your edit means you've solved the problem or not. If you have, you should mark this question as answered.
Personally, I use onchange instead of onselect, but I'm sure either would work. You could also consider using jquery's data() features to store values along with an element on the page instead of a hidden field, unless you are wanting submit a form with the value.
But if you need to save it to a hidden field, try something like this:
目前 id 是否已分配给任何 HTML 对象?
在我的解决方案中,我在项目的属性上设置 id,而不是在数据中,这会在每个
这具有渲染此 HTML 的效果(我也在使用 jsTree 复选框插件):
Are the ids being assigned to any of the HTML objects at the moment?
In my solution I set the id on the attribute of the item, not in the data, which sets the ids on each <li>. Like so:
and that has the effect of rendering this HTML (I am also using the jsTree checkbox plugin):
与 Matt 一样,我也将 id 放入属性对象中,因此它会附加到 li 节点(从 Matt 复制的示例)。
然后,在我的 onselect 函数中,我首先将 li 节点包装在 jQuery 中(函数参数未包装),然后获取它的 id。
Like Matt, I also put the id in an attributes object, so it gets attached to the li node (example copied from Matt).
Then, in my onselect function, I first wrap the li node in jQuery (the function parameter is not wrapped), then get it's id.
这将为您提供所选节点的详细信息。
这里是一系列文章,如果您使用 jsTree,您应该关注。
This will get you the selected node details.
Here is a series of article you should follow if you are using jsTree.