2014-11-21 76 views
2

我試圖展示一個配置了遠程數據的Kendo TreeView,但預先填充了直接從html加載的數據的前兩個級別。在Kendo的PushCreate兒童節點HierarchicalDataSource

對於這一點,我想用的劍道的DataSource pushCreate方法將初始元素添加到樹:

homogeneous = new kendo.data.HierarchicalDataSource({ 
    transport: { read: { url: serviceRoot + "/Employees", dataType: "jsonp" } }, 
    schema: { 
     model: { 
      id: "EmployeeId", 
      hasChildren: "HasEmployees" 
     } 
    } 
}); 

// Adding root 
homogeneous.pushCreate({"EmployeeId":2,"FullName":"Andrew Fuller","HasEmployees":true,"ReportsTo":null}); 

// Adding children 
homogeneous.pushCreate([ 
    {"EmployeeId":1,"FullName":"Nancy Davolio","HasEmployees":false,"ReportsTo":2}, 
    {"EmployeeId":3,"FullName":"Janet Leverling","HasEmployees":false,"ReportsTo":2}, 
    {"EmployeeId":4,"FullName":"Margaret Peacock","HasEmployees":false,"ReportsTo":2}, 
    {"EmployeeId":5,"FullName":"Steven Buchanan","HasEmployees":true,"ReportsTo":2}, 
    {"EmployeeId":8,"FullName":"Laura Callahan","HasEmployees":false,"ReportsTo":2} 
]); 

$("#treeview").kendoTreeView({ 
    dataSource: homogeneous, 
    dataTextField: "FullName" 
}); 

然而,元素添加到根級的孩子(你可以看到這運行here)。

有一個schema.model.children配置來設置保存子元素內的子元素的屬性,我可以一次添加所有,但如果我使用它,那麼dataSource將停止使用遠程數據(您可以看到運行here )。

所以問題是,有沒有辦法使用pushCreate將元素添加爲HierarchicalDataSource中另一個元素的子元素?

回答

0

我找到了答案,同時書面方式的問題:)

好像如果我只是使用items作爲孩子的關鍵,我並不需要指定schema.model.children選項,樹欲靜與遠程工作數據:

homogeneous.pushCreate(
    {"EmployeeId":2,"FullName":"Andrew Fuller","HasEmployees":true,"ReportsTo":null, 
     "items": [ 
      {"EmployeeId":1,"FullName":"Nancy Davolio","HasEmployees":false,"ReportsTo":2}, 
      {"EmployeeId":3,"FullName":"Janet Leverling","HasEmployees":false,"ReportsTo":2}, 
      {"EmployeeId":4,"FullName":"Margaret Peacock","HasEmployees":false,"ReportsTo":2}, 
      {"EmployeeId":5,"FullName":"Steven Buchanan","HasEmployees":true,"ReportsTo":2}, 
      {"EmployeeId":8,"FullName":"Laura Callahan","HasEmployees":false,"ReportsTo":2} 
     ] 
    } 
); 

Here's the working example

我不知道這是否是和意想不到的功能,但可用於將一個完整的子樹添加到根(這解決了我的問題)。然而,問題仍然存在,我可以使用pushCreate將子元素添加到另一個?

+0

不幸的是,這是行不通的!第二級仍然從遠程加載。當您更改HierarchicalDataSource的URL時,您會看到只有父項會被加載。 – leo 2015-08-21 15:11:12