2016-10-15 57 views
0

jstree切換每個級別

<script type="text/javascript"> 
 
    //trying to mimmick dee's answer here: http://stackoverflow.com/questions/32290570/lazy-loading-treeview-with-jstree-in-asp-net-mvc 
 
$(function() { 
 
     var $children = $("#object-children-tree"); 
 

 
     $children.jstree({ 
 
      "core": { 
 
       "animation": 0, 
 
       "data": { 
 
        "url": function(node) { 
 
         return '@Url.Action("GetNodes", "BatchData")'; 
 
        }, 
 
        "data": function (selectedNode) { 
 
         // Hopefully, each time jstree needs to make an AJAX call this function will be called. 
 
         // # is the special ID that the function receives when jstree needs to load the root nodes. 
 
         if (selectedNode.id == "#") 
 
          return { "selectedNodeId": selectedNode.id } 
 
         if (selectedNode.data.nodeType == "provider") 
 
          return { "selectedNodeId": selectedNode.id, "selectedNodeType": selectedNode.data.nodeType } 
 
         if (selectedNode.data.nodeType == "fileType") 
 
          return { "selectedNodeType": selectedNode.data.nodeType, "blockId": selectedNode.data.blockId, "selectedNodeParentId": selectedNode.parent } 
 

 
         return {"selectedNodeId": selectedNode.Id} 
 
        } 
 
       } 
 
      }, 
 
      "plugins": ["wholerow"] 
 
     }); 
 
    }); 
 
</script> 
 

 
<div id="object-children-tree"> 
 
    @* Content will be populated by jsTree *@ 
 
</div>

我一直在閱讀關於Ajax和回調jstree文檔和線程操作。但是,我無法弄清楚如何將它們應用於我的問題。我的樹有五個級別。當我在第一層上切換節點時,我希望它調用特定的服務器動作(「/ controller/action」b/c我使用的是MVC)。然後應該出現二級孩子,這是根據服務器的結果創建的。當我在第二級切換節點時,我希望它調用不同的服務器動作(「/ controller/differentAction」 - MVC)。然後出現三級孩子。依此類推:切換三級節點,並調用不同的操作,並使用該響應生成四級子級。如果這確實得到了回答,你能指導我在哪裏看,並可能解釋它是如何適用的?我認爲與我的例子不同的是,在每個級別上切換一個節點會有一個不同的操作。我需要這樣做,因爲每個節點的孩子數量非常大。

感謝,

**編輯:**我已經呼籲而放棄行動不同的想法,只是允許特定的行動來處理要調用的其他行動,這是罰款。

[HttpGet] 
    public ActionResult GetNodes(string selectedNodeId = null, string selectedNodeType = null, string blockId = null, string selectedNodeParentId = null) 
    { 
     if (selectedNodeId == "#") //Root nodes 
     { 
      return AllProviders(); 
     } 
     if (selectedNodeType == "provider") 
     { 
      return FileTypesOfProvider(Convert.ToInt32(selectedNodeId)); 
     } 
     if (selectedNodeType == "fileType") 
     { 
      return FilesOfProviderOfType(Convert.ToInt32(blockId), Convert.ToInt32(selectedNodeParentId)); 
     } 
     return AllProviders(); 

    } 
+0

請將相關的代碼添加到您的問題 –

+0

我已經做出了正確的控制器操作被調用的點。然而,現在看來jstree並不想將樹填充到第三層。 – brnis

回答

0

我錯誤地指出了導致重複的id的actualFiles的父項。現在我修復了這個功能。

+0

很酷,很高興爲您服務! )) –