Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

图片描述

怎么实现无限极树状图的遍历?PHP遍历的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
547 views
Welcome To Ask or Share your Answers For Others

1 Answer

 //php输出版本:
public static function toTreeHtml($lists = [])
    {
        $string.='<ul class="">';
        foreach ($lists as $key => $value) {
            $string.='<li><input type="checkbox" name="ids[]" />';
            $string.=$value['name'];
            if (count($value['child'])>0){
                $string.=self::toTreeHtml($value['child']);
            }
            $string.='</li>';
        }
        $string.='</ul>'; 
        return $string;
    }
    
    //js输出版本
    function tree(list,ids){
        var string='';
        string+="<ul class=''>";
        for(i in list){
            
            string+="<li class='"+(list[i].pid==0?"item":"")+"'><label><input "+(in_array(list[i].id,ids)?"checked='checked'":"")+" type='checkbox' value='"+list[i].id+"' name='ids[]' />"+list[i].name+"</label>";
            if(list[i].child){
                string+=tree(list[i].child,ids);
            }
            string+="</li>";
        }
        string+="</ul>";
        return string;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...