首页 > 其他 > 详细

搜索结果为树形结构的前端核心

时间:2018-01-31 19:43:45      阅读:357      评论:0      收藏:0      [点我收藏+]

话不多说,直接上代码

            function treeMenu(a){
                       this.tree=a||[];
                       this.groups={};
                    };
                    treeMenu.prototype={
                        init:function(pid){
                            this.group();
                            return this.getDom(this.groups[pid]);
                        },
                        group:function(){
                            for(var i=0;i<this.tree.length;i++){
                                if(this.groups[this.tree[i].pId]){
                                    this.groups[this.tree[i].pId].push(this.tree[i]);
                                }else{
                                    this.groups[this.tree[i].pId]=[];
                                    this.groups[this.tree[i].pId].push(this.tree[i]);
                                }
                            }
                            return this.groups;
                        },
                        getDom:function(a){
                            if(!a){return ‘‘}
                            var html=‘\n<ul>\n‘;
                            for(var i=0;i<a.length;i++){
                                html+= ‘你的页面片段 比如<li></li>‘;
                                html+=this.getDom(this.groups[a[i].id]);
                                html+=‘</li>\n‘;
                            };
                            html+=‘</ul>\n‘;
                            return html;
                        }
                    };
                    var html=new treeMenu(res.data).init(0),
                    alert(html);

 

搜索结果为树形结构的前端核心

原文:https://www.cnblogs.com/wangking/p/8393737.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!