2011-09-22 27 views
1

我使用jsTree與json作爲數據源從服務器。jsTree做每個父母的阿賈克斯請求

爲什麼jsTree在每次點擊節點時都會發出ajax請求,即使節點沒有子節點,該節點也會被服務器中的子節點填滿(它取得根數據!)。

我該如何防止這種行爲?

回答

0

腳本:

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
     $(function() { 
      $("#treeview").jstree({ 
       "json_data": { 
        "ajax": { 
         "url": "/Tree/Tree" 
        } 
       }, 
       "plugins": ["themes", "json_data"] 
      }); 
     }); 
    }); 
</script> 

服務器端:

public class TreeController : Controller 
{ 
    // 
    // GET: /Tree/ 

    public ActionResult Tree() 
    { 
     return Json(new jsTreeModel 
     { 
      data = "Parent", 
      att = new JsTreeAttribute { id = 1 }, 
      state = "closed", 
      children = new List<jsTreeModel> { new jsTreeModel { data = "Child", att = new JsTreeAttribute { id = 1 }, state = "closed" } } 

     }, JsonRequestBehavior.AllowGet); 
    } 



} 
public class jsTreeModel 
{ 
    public string data { get; set; } 
    public JsTreeAttribute att { get; set; } 
    public string state { get; set; } 
    public List<jsTreeModel> children { get; set; } 
} 

public class JsTreeAttribute 
{ 
    public int id { get; set; } 
}