2014-11-21 49 views
0

我已經得到了發樹項目樹視圖此控制器方法:獲取ID

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; 
} 

這是劍道的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)); 
} 

但沒有運氣。正如您在控制器方法中看到的,我正在填充textid屬性,但我只能訪問文本。任何幫助?

LIVE DEMO

回答

1

爲了得到所選節點的id,你應該使用$(e.node).data("id")

function onSelect(e) { 
    alert($(e.node).data("id")); 
} 
+1

謝謝OnaBai,它的工作原理。我很驚訝,因爲這些解決方案根本不直觀,你知道我在哪裏可以找到文檔來指導我在劍道上?文檔是必不可少的,我看不到任何有用的。此外,我看到你是一個劍道專家,你可以請檢查[這個問題](http://stackoverflow.com/questions/26929767/passing-kendo-grid-selected-item-into-kendo-window)和幫我?再次謝謝你。 +1並且正確答案 – chiapa 2014-11-24 09:46:38