Java代码
/**
*树形菜单VO
*/
publicclassSelectTreeimplementsSerializable{
privateintid;
privateString name;
privateList child =newArrayList();
//getter & setter ....略
}
/**
*树形菜单VO
*/
public class SelectTree implements Serializable{
private int id;
private String name;
private List child = new ArrayList();
//getter & setter ....略
}
代码片段:
Java代码
/**
* 构建树型菜单数据
*/
publicList buildNode(intpid,List channels){
List result =newArrayList();
for(YcChannel chl:channels){
SelectTree node =newSelectTree();
if(null!= chl.getParentId() && chl.getParentId().equals(pid)){
node.setId(chl.getChannelId());
node.setName(chl.getName());
List children = buildNode(chl.getChannelId(),channels);
if(null!= children &&0
node.setChild(children);
}
result.add(node);
}
}
returnresult;
}
publicString queryChannelList() {
ycChannelList =this.channelSer.queryChannelList();
List trees =newArrayList();
for(YcChannel yc:ycChannelList){
if(null== yc.getParentId()){
SelectTree t =newSelectTree();
t.setId(yc.getChannelId());
t.setName(yc.getName());
t.setChild(buildNode(t.getId(),ycChannelList));
trees.add(t);
}
}
this.setAjaxData(trees);
returnAJAX_DATA;
}
/**
* 构建树型菜单数据
*/
public List buildNode(int pid,List channels){
List result = new ArrayList();
for(YcChannel chl:channels){
SelectTree node = new SelectTree();
if(null != chl.getParentId() && chl.getParentId().equals(pid)){
node.setId(chl.getChannelId());
node.setName(chl.getName());
List children = buildNode(chl.getChannelId(),channels);
if(null != children && 0 < children.size()){
node.setChild(children);
}
result.add(node);
}
}
return result;
}
public String queryChannelList() {
ycChannelList = this.channelSer.queryChannelList();
List trees = new ArrayList();
for(YcChannel yc:ycChannelList){
if(null == yc.getParentId()){
SelectTree t = new SelectTree();
t.setId(yc.getChannelId());
t.setName(yc.getName());
t.setChild(buildNode(t.getId(),ycChannelList));
trees.add(t);
}
}
this.setAjaxData(trees);
return AJAX_DATA;
}
前端JS代码:
Js代码
//recursive tree node
functionbuildNode(len,data){
varprefix ="|";
for(vari=0;i
prefix +="-";
}
$.each(data,function(i,item){
if(0
$('#typeid').append(""+ prefix + item.name +"");
buildNode(len+1,item.child);
}else{
$('#typeid').append(""+ prefix + item.name +"");
}
});
}
$.ajax({
url:'${base}/channel/channelAction!queryChannelList.action',
type:'GET',
dataType:'json',
contentType:'application/json',
success:function(json){
if(json.success){
$('#typeid').empty();
$('#typeid').append("请选择栏目...");
$.each(json.data,function(i,item){
if(null== item.parentId){
$('#typeid').append(""+ item.name +"");
buildNode(1,item.child);
}
});
}
},
error:function(){
alert("加载栏目出错!");
}
});
});