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

话不多说,直接上代码

            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);

 

posted @ 2018-01-31 18:03  我爱吃小丸子  阅读(211)  评论(0编辑  收藏  举报