[英]Accessing the Child Nodes of a Selected Kendo TreeView Node
I have an Ajax populated treeview.... 我有一个Ajax填充树视图....
@(Html.Kendo().TreeView()
.Name("fao")
.HtmlAttributes(new {@class="fixed-height" })
.DataTextField("Text")
.TemplateId("treeview-item-template")
.DataSource(ds => ds
.Read(r => r
.Action("_ModuleData", "Home")
)
.Model(m => m
.Children("Items")
.HasChildren("HasChildren")
)
)
)
I have a requirement to update some data hidden against each child item - in the template - when a action (triggered outside of the control) occurs. 我需要更新一些隐藏在每个子项目中的数据 - 在模板中 - 当一个动作(在控件之外触发)发生时。
The template, for completeness, looks like this ... 完整性模板看起来像这样......
<script id="treeview-item-template" type="text/kendo-ui-template">
#= item.Text #<input type='hidden' class='hidden-data' data-fal='#= item.Fal#' data-uid='#=item.uid#'/>
</script>
Now, I have code for the trigger and that works just fine. 现在,我有触发器的代码,并且工作得很好。
I have code to update the hidden data. 我有代码来更新隐藏数据。 Again.
再次。 No worries.
别担心。
What I can't figure out is how to simply get at all of the child (and granchild, etc) nodes of the node that is selected when the trigger fires. 我无法弄清楚的是如何简单地获取触发器触发时所选节点的所有子节点(和granchild等)节点。
If I were trying to get at the children when the node was initially clicked, I kind of expected to be able to say something like... 如果我在最初点击节点时试图找到孩子们,我希望能够说出类似...
function doSomething(e)
{
for(n=0; n<e.node.nodes.length; n++)
{
doSomethingElse(e.node.nodes[n]);
}
}
But no such functionality seems to exist. 但似乎不存在这样的功能。
Does anyone have any suggestions how I might go about this? 有没有人有任何建议我怎么可能这样做?
You can access the children via the model which you can get from the DOM node: 您可以通过您可以从DOM节点获取的模型访问子项:
var dataItem = e.sender.dataItem(e.node);
if (dataItem.hasChildren) {
var children = dataItem.children.data();
}
This will only get you direct children, so you'd have to make it recursive to get all descendants. 这只会让你直接生孩子,所以你必须让它递归才能得到所有的后代。
OK. 好。
It seems I can get at all of the nodes on the TreeView like this ... 看来我可以像这样得到TreeView上的所有节点......
var allNodes = $(".k-item");
Likewise, I can get at all of the child nodes of a given node like this ... 同样,我可以像这样得到给定节点的所有子节点......
// node is the node under which we need all of the child nodes
var childNodes = $(".k-item", node);
And, for any given node, I can get the dataItem like this ... 并且,对于任何给定的节点,我可以像这样得到dataItem ......
var dataItem = tree.dataItem(node);
I believe that all of the above only holds if the data for all of the child nodes below the selected node have been loaded. 我相信,如果已加载所选节点下的所有子节点的数据,则上述所有内容都成立。 In my case, I'm loading from remote data (using AJAX) but I have
.LoadOnDemand(false)
in the definition of my grid. 在我的情况下,我从远程数据(使用AJAX)加载,但我的网格定义中有
.LoadOnDemand(false)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.