如何在jstree中获取选定节点的文本 [英] How to get selected node's text in jstree
问题描述
我有一个如下所示的jstree
I have a jstree like shown below
并在我的脚本中包含代码
and have the code in my script
$("#jstree").click(function (e) {
var node = $("#jstree").jstree('get_selected');
$('#resultText').html(node);
});
我需要的是如果我要选择书籍,书名,价格,我需要将输出显示为书名-价格.通过使用上面的代码,我可以获得 id ,即.我的选定节点的1-1_1-1_2,但是如何获得上述格式的选定节点的文本?
What i need is if i am selecting book, title, price i need to display output as book-title-price. By using above code i can get ids ie. 1-1_1-1_2 of my selected nodes but how can i get texts of the selected nodes in the above format?
用于jstree集成的完整代码.
COMPLETE CODE for jstree integration.
@model List<XMLNodeSearch.Models.NodeSearchModel>
<link href="~/vakata-jstree-8ea6ce7/dist/themes/default/style.min.css" rel="stylesheet" />
<script src="~/Script/jquery-1.12.0.min.js"></script>
<script src="~/vakata-jstree-8ea6ce7/dist/jstree.min.js"></script>
<script src="~/vakata-jstree-8ea6ce7/dist/jstree.js"></script>
<script>
$(function () {
$('#jstree').jstree();
$("#jstree").click(function (e) {
var node = $("#jstree").jstree('get_selected').text();//This line with text() will throw an error
$('#resultText').html(node);
});
</script>
<section>
<label>Result - <span id="resultText"></span></label>
<div id="NodeResult">
@if (Model != null)
{
<div id="jstree">
@foreach (var treeNode in Model)
{
<!-- in this example the tree is populated from inline HTML -->
<ul>
<li id="@treeNode.Id">@treeNode.NodeName
@if(treeNode.ChildNode.Count() > 0)
{
<ul>
@foreach (var childNd in treeNode.ChildNode)
{
<li id="@childNd.Id">@childNd.NodeName</li>
}
</ul>
}
</li>
</ul>
}
</div>
}
</div>
</section>
推荐答案
终于找到了解决我问题的方法.我将答案发布在这里,以供将来任何人参考.
Finally found the solution to my problem. I am posting the answer here for anyone's future reference.
我所做的是在脚本块中添加了以下脚本.
What i did is added the following script in my script block.
$('#jstree').on('changed.jstree', function (e, data) {
var i, j, r = [];
for (i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
}
$('#resultText').html('Selected: ' + r.join(', '));
}).jstree();
这给了我想要的输出.
This given me the desired output.
这篇关于如何在jstree中获取选定节点的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!