kendoTreeView,需要注意的地方

时间:2022-01-17 17:05:00

今天使用了下kendoUI中的TreeView控件,一开始的时候总是不成功,

总是会报 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined,

后来查询相关的API,原来TreeView是比较特殊的控件,不能绑定kendo.data.DataSource数据源,

数据源必须是kendo.data.HierarchicalDataSource类型的数据源,

将数据源改为该类型的以后,任然报错,而且是奇怪的错误提示。。。

Uncaught TypeError: e.slice is not a function

使我又郁闷了半天,经由同事的提醒,我突然发现树的数据可能有问题,

代码如下:

     /**
* 获取文档设置信息(树)
*/
@RequestMapping("/getDocSettingsTree")
@ResponseBody
public String getDocSettingsTree(HttpSession session,HttpServletRequest request) {
try{
Settings rootSettings = new Settings();
rootSettings.setSettingId("-1");
rootSettings.setSettingName("文档分类");
rootSettings.setSettingType("2");
getSubSettings(rootSettings);
String settingList = JsonMapper.alwaysMapper().toJson(rootSettings);
return settingList; }catch(Exception e){
return "查询出错!";
}
}

这里的根节点是单个对象,必须在包装成LIST才行,

于是修改代码为:

    /**
* 获取文档设置信息(树)
*/
@RequestMapping("/getDocSettingsTree")
@ResponseBody
public String getDocSettingsTree(HttpSession session,HttpServletRequest request) {
try{
Settings rootSettings = new Settings();
rootSettings.setSettingId("-1");
rootSettings.setSettingName("文档分类");
rootSettings.setSettingType("2");
getSubSettings(rootSettings);
//必须包装成LIST回传
List<Settings> listSettings = new ArrayList<Settings>();
listSettings.add(rootSettings);
String settingList = JsonMapper.alwaysMapper().toJson(listSettings);

return settingList; }catch(Exception e){
return "查询出错!";
}
}

这样问题便解决了

JS代码:

 function initTree(){

     $.ajax({
type: "POST",
async: true,
url: ctx + "/Settings/getDocSettingsTree",
dataType: "json",
success: function(data){ var dataSource = new kendo.data.HierarchicalDataSource({
data: data,
schema: {
model: {
id: "settingId",
settingType: "settingType",
children: "subSettings"
}
}
});
$("#docTree").kendoTreeView({
dataSource: dataSource,
dataValueField: "id",
dataTextField: "settingName"
}); $("#docTree").data("kendoTreeView").expand(".k-item");
}
});
}

这里的重点就是数据源的格式必须是kendo.data.HierarchicalDataSource

并且数据是LIST形式的。