今天用到了KendoTreeview的东西,所以写了个类
package com.yuchengtech.weixin.wbms.menu.entity;
import java.util.ArrayList;
import java.util.List;
public class KendoTree<T> {
private String id;
private String text;
private T dataItem;
private List<KendoTree<T>> items = new ArrayList<KendoTree<T>>();
private boolean expanded = true;
private boolean checked = false;
public KendoTree() {
super();
}
public KendoTree(String id, String text, T dataItem) {
super();
this.id = id;
this.text = text;
this.dataItem = dataItem;
// List<KendoTree<T>> temptree = new ArrayList<KendoTree<T>>()
}
public KendoTree(String id, String text, T dataItem,
List<KendoTree<T>> items) {
super();
this.id = id;
this.text = text;
this.dataItem = dataItem;
this.items = items;
}
public KendoTree(String id, String text, T dataItem,
List<KendoTree<T>> items, boolean expanded, boolean checked) {
this.id = id;
this.text = text;
this.dataItem = dataItem;
this.items = items;
this.expanded = expanded;
this.checked = checked;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public T getDataItem() {
return dataItem;
}
public void setDataItem(T dataItem) {
this.dataItem = dataItem;
}
public List<KendoTree<T>> getItems() {
return items;
}
public void setItems(List<KendoTree<T>> items) {
this.items = items;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
controller部分,前端想后端传过来的Json数据需要进行转换,这里用递归函数的话就会清晰很多了,当时也不知道自己怎么想的……
@RequestMapping(value = "/updateTree", method = RequestMethod.POST)
public @ResponseBody
Map<String, Object> updateTree(Model model,
@RequestBody List<Map<Object, Object>> list) {
String accountId = list.get(0).get("id").toString();// 公众号ID拼接在根节点上传回来
// System.out.println(accountId);
// 此处最好使用递归,对不起后来人了……
KendoTree<WbMenu> menu = new KendoTree<WbMenu>();
WbMenu wbMenu = new WbMenu();
JSONArray jsonArray = JSONArray.fromObject(list);
for (int i = 0; i < jsonArray.size(); i++) {
// Map<Object, Object> tempMap = list.get(i);
JSONObject jsonObject = jsonArray.getJSONObject(i);
menu = (KendoTree<WbMenu>) JSONObject.toBean(jsonObject,
menu.getClass());
menu.setDataItem((WbMenu) JSONObject.toBean(
jsonObject.getJSONObject("dataItem"), wbMenu.getClass()));
if (jsonObject.get("items") != null) {
List<KendoTree<WbMenu>> items = new ArrayList<KendoTree<WbMenu>>();
JSONArray array = jsonObject.getJSONArray("items");
for (int j = 0; j < array.size(); j++) {
WbMenu tempMenu = new WbMenu();
KendoTree<WbMenu> tempmenu = new KendoTree<WbMenu>();
JSONObject object = array.getJSONObject(j);
tempmenu = (KendoTree<WbMenu>) JSONObject.toBean(object,
tempmenu.getClass());
tempmenu.setDataItem((WbMenu) JSONObject.toBean(
object.getJSONObject("dataItem"),
tempMenu.getClass()));
if (object.get("items") != null) {
// 第三层
JSONArray sub_array = object.getJSONArray("items");
List<KendoTree<WbMenu>> sub_items = new ArrayList<KendoTree<WbMenu>>();
for (int x = 0; x < sub_array.size(); x++) {
WbMenu sub_tempMenu = new WbMenu();
KendoTree<WbMenu> sub_tempmenu = new KendoTree<WbMenu>();
JSONObject sub_object = sub_array.getJSONObject(x);
sub_tempmenu = (KendoTree<WbMenu>) JSONObject
.toBean(sub_object, sub_tempmenu.getClass());
sub_tempmenu.setDataItem((WbMenu) JSONObject
.toBean(sub_object
.getJSONObject("dataItem"),
sub_tempMenu.getClass()));
sub_items.add(sub_tempmenu);
}
tempmenu.setItems(sub_items);
}
items.add(tempmenu);
}
menu.setItems(items);
}
}
List<KendoTree<WbMenu>> menulist = new ArrayList<KendoTree<WbMenu>>();
menulist.add(menu);
wbMenuService.menuPublish(menulist);
Map<String, Object> result = new HashMap<String, Object>();
result.put("success", true);
return result;
}
前端代码
var homogeneous = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: ctx+"/wbMenu/getMenuTree.do",
dataType: "json"
}
},
schema: {
model: {
id: "id",
text:"text",
children: "items",
data:"dataItem"
}
}
});