java+js递归循环树实现无限下拉菜单

目前项目中需要做文章管理,每篇文章要选择文章分类,而文章的分类有层级性且没有层级限制,所以选择分类时可能会面临无限下拉菜单

我的想法是做成上图一样的如果有下一级鼠标悬浮则在右侧显示子集菜单,由于不知道具体有几层关系,所以在java端的数据获取和js端的dom对象生成都是有递归循环实现的。

1.数据格式

 库是mysql,标准的父子集关系

2.java生成树状json数据

//递归循环所有子集,并拼接成字符串
public List> getSubCategory(Map params) {
    List> list=articleService.getArticleCategory(params);
    for (Map item : list) {
        params.put("categoryParentId", item.get("category_id").toString());
        List> subList = this.getSubCategory(params);
        item.put("subList",subList);
    }
    return list;
}

//获取栏目树(递归查询)
@ResponseBody
@RequestMapping(value = "getArticleCategoryTree1", produces = "text/plain;charset=UTF-8")
public String getArticleCategory1(@RequestParam Map params) {
    RetBase ret=new RetBase();
    params.put("categoryParentId","0");
    List> list=this.getSubCategory(params);
    ret.setData(list);
    ret.setSuccess(true);
    return JSON.toJSONString(ret);
}

开始只需要在参数params中设置需要遍历的最顶端父级id为categoryParentId,就可以递归生成含子集(subList)的树状结构,并用json返回前台即可

java+js递归循环树实现无限下拉菜单_第1张图片

返回的json:

[{
	"category_createtime": 1539139789000,
	"category_createtime1": "2018-10-10 10:10:49",
	"category_id": 1,
	"category_name": "一级目录",
	"category_parent_id": 0,
	"category_status": "YES",
	"subList": [{
		"category_createtime": 1539139792000,
		"category_createtime1": "2018-10-10 10:10:52",
		"category_id": 2,
		"category_name": "二级目录",
		"category_parent_id": 1,
		"category_status": "YES",
		"subList": [{
			"category_createtime": 1539139794000,
			"category_createtime1": "2018-10-10 10:10:54",
			"category_id": 3,
			"category_name": "三级目录",
			"category_parent_id": 2,
			"category_status": "YES",
			"subList": [{
				"category_createtime": 1539139799000,
				"category_createtime1": "2018-10-10 10:10:59",
				"category_id": 4,
				"category_name": "四级目录",
				"category_parent_id": 3,
				"category_status": "YES",
				"subList": []
			}, {
				"category_createtime": 1539139802000,
				"category_createtime1": "2018-10-10 10:10:02",
				"category_id": 5,
				"category_name": "四级目录",
				"category_parent_id": 3,
				"category_status": "YES",
				"subList": []
			}]
		}]
	}]
}, {
	"category_createtime": 1539139804000,
	"category_createtime1": "2018-10-10 10:10:04",
	"category_id": 6,
	"category_name": "一级目录",
	"category_parent_id": 0,
	"category_status": "YES",
	"subList": []
}]

 3.html样式

这个下拉菜单的样式是layui中的,引入layui的css和js后引用下面代码则可生成下拉菜单demo

4.js递归遍历数据并生成html

//递归类型树
function recursionCategory(data){
    var ul=$('');
    for(item in data){
        var li=$('
  • '+data[item].category_name+'
  • '); var sublist=data[item].subList; if(sublist!=null && sublist.length>0){ li.find("a").append(''); li.append(recursionCategory(sublist)) } ul.append(li) } return ul; } //加载文章分类菜单 function loadCategory(){ $.ajax({ type: 'POST', url: '${ctx}/webmana/article/getArticleCategoryTree1', dataType: 'json', data:{ }, success: function(data){ if(data.success){ //console.log(JSON.stringify(data.data)) var data=data.data; var ul=recursionCategory(data); $(".codeView").append(ul); $(".codeView>ul").addClass("dropDown-menu").addClass("radius").addClass("box-shadow"); }else{ } }, error:function(data) { //console.log(data.msg); }, }); }

     

    你可能感兴趣的:(前端)