private void CreateTree(List node, List list) {//递归创建树
if (list!=null && list.size() > 0){
Map map =new HashMap();
Map<String,Object> childsMap=new HashMap();
List list_1 =new ArrayList();
List childs =new ArrayList();
// foreach (String li in list){
for(int i=0;i<list.size();i++){
Resourse resourse=(Resourse) list.get(i);
String pid=resourse.getZyid();
if ("0".equals(resourse.getZyfid())){
map =new HashMap();
list_1 =new ArrayList();
map.put("id",resourse.getZyid());
map.put("text",resourse.getZymc());
list_1.add(map);
node.addAll(list_1);
}
// else{
// childsMap=new HashMap();
// map.put("id",resourse.getZyid());
// map.put("text",resourse.getZymc());
// childs.add(map);
// childsMap.put("item",childs);
//// list_1.add(childsMap);
// }
CreateTree(node, GetList(list, pid));
}
}
}我现在能把第一层品出来
{"id":"0","item":[{"id":"1","text":"资源1"},{"id":"4","text":"资源4"},{"id":"6","text":"资源6"}]}
if (list!=null && list.size() > 0){
Map map =new HashMap();
Map<String,Object> childsMap=new HashMap();
List list_1 =new ArrayList();
List childs =new ArrayList();
// foreach (String li in list){
for(int i=0;i<list.size();i++){
Resourse resourse=(Resourse) list.get(i);
String pid=resourse.getZyid();
if ("0".equals(resourse.getZyfid())){
map =new HashMap();
list_1 =new ArrayList();
map.put("id",resourse.getZyid());
map.put("text",resourse.getZymc());
list_1.add(map);
node.addAll(list_1);
}
// else{
// childsMap=new HashMap();
// map.put("id",resourse.getZyid());
// map.put("text",resourse.getZymc());
// childs.add(map);
// childsMap.put("item",childs);
//// list_1.add(childsMap);
// }
CreateTree(node, GetList(list, pid));
}
}
}我现在能把第一层品出来
{"id":"0","item":[{"id":"1","text":"资源1"},{"id":"4","text":"资源4"},{"id":"6","text":"资源6"}]}
解决方案 »
- web上传的问题
- 如何给一个页面的填写框赋值
- tomcat 404 错误怎么解决,急,在线等。
- weblogoc10.3在更新实现Serializable接口的form类时,出现错误
- J2EE中,往往使用封装业务实体,封装业务逻辑
- 用jQuery从服务器上取数据
- 请问 关于jsp传值的问题
- 再问一下JAVA里取余数的函数怎么写?
- java电子政务在验收时出现问题,急寻高手赐教(提供高额回报,需11月6日前解决问题)
- 向高手请教!如何利用jsp向oracle中读写二进制图片文件!
- springMVC+CXF3.0 tomcat下启动正常,用脚本启动报错.
- Oscache集群环境下报错重写并序列化了
if (list!=null && list.size() > 0){
Map map =new HashMap();
Map<String,Object> childsMap=new HashMap();
List list_1 =new ArrayList();
List childs =new ArrayList();
// foreach (String li in list){
for(int i=0;i<list.size();i++){
Resourse resourse=(Resourse) list.get(i);
String pid=resourse.getZyid();
if ("0".equals(resourse.getZyfid())){
map =new HashMap();
list_1 =new ArrayList();
map.put("id",resourse.getZyid());
map.put("text",resourse.getZymc());
list_1.add(map);
node.addAll(list_1);
}
else if(pids.equals(resourse.getZyfid())){
childsMap=new HashMap();
map=new HashMap();
map.put("id",resourse.getZyid());
map.put("text",resourse.getZymc());
childs.add(map);
childsMap.put("item",childs);
list_1.add(childsMap);
node.addAll(list_1);
}
CreateTree(node, GetList(list, pid),pid);
}
}这是我新写的但是效果不太对
{"id":"0","item":[{"id":"1","text":"资源1"},{"item":[{"id":"2","text":"资源2"},{"id":"3","text":"资源3"}]},{"item":[{"id":"2","text":"资源2"},{"id":"3","text":"资源3"}]},{"item":[{"id":"2","text":"资源2"},{"id":"3","text":"资源3"}]},{"id":"4","text":"资源4"},{"item":[{"id":"5","text":"资源5"}]},{"id":"6","text":"资源6"},{"item":[{"id":"7","text":"资源7"}]}]}
jsp+ajax树状菜单源代码
if (list!=null && list.size() > 0){
for(int i=0;i<list.size();i++){
Resourse resourse=(Resourse) list.get(i);
String pid=resourse.getZyid();
if("0".equals(resourse.getZyfid())){
Map map1=new HashMap();
List list_t =new ArrayList();
map1.put("id",resourse.getZyid());
map1.put("text",resourse.getZymc());
List templist=GetList(list, pid);
for (Iterator iterator = templist.iterator(); iterator
.hasNext();) {
Resourse r = (Resourse) iterator.next();
Map mapt=new HashMap();
mapt.put("id",r.getZyid());
mapt.put("text",r.getZymc());
list_t.add(mapt);
map1.put("item", list_t);
}
node.add(map1);
}
CreateTree(node, GetList(list, pid),pid,maps);
}
}
}
//
private List GetList(List list, String s)//此方法寻找子节点集合
{
List temp=new ArrayList();
for(int i=0;i<list.size();i++){
Resourse resourse=(Resourse) list.get(i);
if (s.equals(resourse.getZyfid())){
temp.add(resourse);
}
}
return temp;
}{"id":"0","item":[{"id":"1","text":"资源1","item":[{"id":"2","text":"资源2"},{"id":"3","text":"资源3"}]},{"id":"4","text":"资源4","item":[{"id":"5","text":"资源5"}]},{"id":"6","text":"资源6","item":[{"id":"7","text":"资源7"}]}]} id pid text1 0 资源1
2 1 资源2
3 1 资源3
4 0 资源4
5 4 资源5
6 0 资源6
7 6 资源7
8 3 资源8 我写的没有爸第id=8基点遍历进去,大家有什么号方法么
/**权限树***************/
public boolean getAuthTree(C_AUTH auth, String treeType, String treeRoot) throws IOException {
String hql = "select s from .....";
String treeRootName = "ROOTNAME";
authList = (List<C_AUTH>)sysmDao.getInfoByHql(hql);
//调用递归
JSONObject result = getAuthTreeMethod(treeRoot,treeRootName,treeType);
CommonJava.writeToResponse(result.toString());
return false;
}
private JSONObject getAuthTreeMethod(String id,String name,String treeType) throws IOException{ //加载父节点信息
JSONObject finalJson = new JSONObject();
finalJson.element("id", id);
finalJson.element("text", name);
finalJson.element("expanded","true");
JSONArray tmpJson = new JSONArray();
Iterator iterator =authList.iterator();
while(iterator.hasNext()){
C_AUTH auth= (C_AUTH)iterator.next();
if(!"".equals(auth.getPAId())&&Integer.parseInt(id) == Integer.parseInt(auth.getPAId())){
JSONObject tmpjsonObject = new JSONObject();
tmpjsonObject.element("id", auth.getAId());
tmpjsonObject.element("text", auth.getAName());
boolean leafFlag = true;
Iterator iterator2 =authList.iterator();
while(iterator2.hasNext()){
C_AUTH auth2=(C_AUTH) iterator2.next();
//如果存在子节点继续递归
if(!"".equals(auth2.getPAId())&&auth.getAId() == Integer.parseInt(auth2.getPAId())){
tmpjsonObject = getAuthTreeMethod(auth.getAId().toString(),auth.getAName(),treeType);
}
}
}
tmpJson.add(tmpjsonObject);
}
finalJson.element("children", tmpJson);
return finalJson;
}