我有这个控制器方法将树项发送到视图中的树:
private IEnumerable<TreeViewItemModel> GetTrees()
{
InstallationPlaceModel ipm = new InstallationPlaceModel();
var gipo = ipm.getRootInstallationPlaces();
List<TreeViewItemModel> fullTree = new List<TreeViewItemModel>();
foreach (wsInstallationPlace.installationPlaceOutput father in gipo.installationPlaces)
{
var gipo2 = ipm.getChildInstallationPlaces(father.installationPlace.id);
List<TreeViewItemModel> childTree = new List<TreeViewItemModel>();
foreach (wsInstallationPlace.installationPlaceOutput child in gipo2.installationPlaces)
{
TreeViewItemModel childTreeItem = new TreeViewItemModel
{
Text = child.installationPlace.mediumDescription,
Id = child.installationPlace.id
};
childTree.Add(childTreeItem);
}
TreeViewItemModel fatherTreeItem = new TreeViewItemModel
{
Text = father.installationPlace.mediumDescription,
Id = father.installationPlace.id,
Items = childTree
};
fullTree.Add(fatherTreeItem);
}
ViewBag.mytree = fullTree;
return fullTree;
}
这是Kendo TreeView:
@(Html.Kendo().TreeView()
.Name("treeview")
.DragAndDrop(true)
.Events(e => e.Select("onSelect"))
.BindTo((IEnumerable<TreeViewItemModel>)ViewBag.mytree)
)
此函数用于处理树节点的选择:
function onSelect(e) {
alert(this.text(e.node));
}
选择节点后,将显示包含节点文本的警报。我想显示节点的id。我试过了:
function onSelect(e) {
alert(this.id(e.node));
}
但没有运气。正如您在控制器方法中看到的那样,我正在填充text
和id
属性,但我只能访问该文本。有什么帮助吗?
答案 0 :(得分:1)
要获取所选节点的id
,您应使用$(e.node).data("id")
。
function onSelect(e) {
alert($(e.node).data("id"));
}