nodetree多级分类展示

效果如下

nodetree多级分类展示_第1张图片

nodetree多级分类展示_第2张图片

 

1.基于TP5代码

需要准备几个js文件




2.目录路径

nodetree多级分类展示_第3张图片

 

相关控制器代码

goodsclass.php

select();
    $this->assign('data',$data);
    return $this->fetch();
    }


    public function classadd(){
        $res=Db::name('goods_class')->field("*,concat(path,',',pid) as paths")->order('paths')->select();
      //  var_dump($res);exit();
        foreach ($res as $k=>$v){
            $res[$k]['name']=str_repeat('|---',$v['level']).$v['name'];
        }

        var_dump($res);
        $this->assign('data',$res);
        return $this->fetch();
    }

    //添加分类到数据库
    public function add(){
       // print_r($_POST);
        $data['name']=$_POST['name'];
        $data['pid']=$_POST['pid'];


        if(!empty($data['name'])){
            if($data['pid']==0){
                $data['level']=1;
                $re= Db::name('goods_class')->insertGetId($data);
                $path['path']='0'.','.$re;
                Db::name('goods_class')->where('id',$re)->update($path);
                $this->success('顶级添加分类成功');
            }
        $res=Db::name('goods_class')->field('path')->find($data['pid']);
        $data['path']=$res['path'];
        $data['level']=substr_count($data['path'],',');
        $re= Db::name('goods_class')->insertGetId($data);
            $path['path']=$data['path'].','.$re;
            $path['level']=substr_count($path['path'],',');
            Db::name('goods_class')->where('id',$re)->update($path);
            $this->success('添加分类成功');

        }else{
            $this->error('分类名称为空');
        }
    }

    public function getGoodCateGory(){
        $category= Db::name('goods_class')->field('id,pid as pId,name')->select();
        if($category){
            foreach ($category as $k=>$v){
                $category[$k]['open']=true;
            }
            $this->success('获取商品分类成功','',$category);
        }else{
            $this->error('获取商品分类失败');
        }

    }

}

 

html代码

classlist.html




    
    Title





     

    你可能感兴趣的:(thinkphp5)