欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

laravel5.5 PHP递归实现无限级分类列表的代码教程

程序员文章站 2023-10-27 09:31:34
1.模型代码: public function getsubjecteslists($request){ $subjectes = db::table($this->table)...

1.模型代码:

public function getsubjecteslists($request){
    $subjectes = db::table($this->table);
    //查询条件namekeyword
    if ($request->namekeyword != "") {
        $namekeyword = $request->namekeyword;
        $subjectes->where(function ($subjectes) use ($namekeyword) {
            $subjectes->where('subjectname', 'like', '%' . $namekeyword . '%');
        });
    }

    $data = $subjectes->select('id','pid','subjectname','specials','sort','top','level')->where('is_delete', 1)->orderby('top', 'desc')->orderby('sort', 'asc')->get();
    return  $data;
}

2.控制器代码 :

//获取分类列表
    public function getlists(request $request)
    {

        $datacontent = $this->subjectes->getsubjecteslists($request);
        foreach ($datacontent as $key => $value) {
            $arr[$key]['subjectname'] = $value->subjectname;
            $arr[$key]['id'] = $value->id;
            $arr[$key]['specials'] = $value->specials;
            $arr[$key]['level'] = $value->level;
            $arr[$key]['pid'] = $value->pid;
            $arr[$key]['sort'] = $value->sort;
            $arr[$key]['top'] = $value->top;
        }
        $data = $this->get_attr($arr, 0);
        $num = count($data);
        //return $arr;
        return adminresult("请求成功", 1, $num, $data);
//        return result($num, 0, $data);
    }

    //递归
    public function get_attr($a, $pid)
    {
        $tree = array();                                //每次都声明一个新数组用来放子元素
        foreach ($a as $v) {
            if ($v['pid'] == $pid) {                      //匹配子记录
                $v['children'] = $this->get_attr($a, $v['id']); //递归获取子记录
                if ($v['children'] == null) {
                    unset($v['children']);             //如果子元素为空则unset()
                }
                $tree[] = $v;                           //将记录存入新数组
            }
        }
        return $tree;                                  //返回新数组
    }

    //信息列表视图
    public function index(request $request)
    {
        //获取第一二级分类
        $result = $this->subjectes->getsubject();
        $menuinfo = getmenufrompath($request->path());
        return view("subjectes.views.index")
            ->with("result", $result)
            ->with("thisaction", $menuinfo->url)
            ->with("title", $menuinfo->title);
    }

3.数据格式:

function adminresult($msg, $code = 0, $count = 0, $data = "")
{
    $res['msg'] = $msg;
    $res['code'] = $code;
    $res['count'] = $count;
    $res['data'] = $data;
    return json_encode($res);
}

4.js填充数据模板:

function getdatalists(page,namekeyword) {

    $.ajax({
        url: adminurl + "/subjectes/lists",
        data: {
            page: page,
            namekeyword: namekeyword,
        },
        type: "get",
        datatype: "json",

        beforesend: function () {
            index = layer.load(2, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
        },

        success: function (res) {
            layer.close(index);
            if (res.code == 1) {
                innerdata = '';

                floatdatatpl(res.data);
                $('#listbox').html(innerdata);
                form.render();
            } else if (res.code == 1002) {
                layer.msg(res.msg);
                settimeout(function () {
                    location.href = adminurl + "/login";
                }, 500)
            } else {
                layer.msg(res.msg);
                return false;
            }
        },
        error: function () {
            layer.close(index);
            layer.msg("网络请求错误,稍后重试!");
            return false;
        }
    });
}


//数据格式化模板
function floatdatatpl(data, page) {

    layui.each(data, function (index, item) {
        innerdata += "<tr>";
        space = '';
        for (var i = 1; i < item.level; i++) {
            space += '&nbsp;&nbsp;&nbsp;&nbsp;';
        }
        if (item.subjectname) {
            if (item.level != 1) {
                space += '|-';
                innerdata += '<td>' + space + item.subjectname + '</td>';
            } else {
                innerdata += '<td>' + item.subjectname + '</td>';
            }
        } else {
            innerdata += '<td>未设置</td>';
        }
        if(item.specials == 1){
            innerdata += '<td>否</td>';
        }else{
            innerdata += '<td>是</td>';
        }
        innerdata += '<td>' + '<input type="number" name="sort['+ item.id +']" placeholder="序号" autocomplete="off" class="layui-input" value="'+ item.sort +'"></td>';
        innerdata += '<td>';
        innerdata += '<a  href= "' + adminurl + '/subjectes/view/' + item.id + '" class="layui-btn layui-btn-sm layui-btn handle" data-id="' + item.id + '" data-state="2"><i class="layui-icon"></i>编辑</a>' +
            '<a data-method="delete" class="layui-btn layui-btn-sm layui-btn-danger handle" data-id="' + item.id + '" data-state="3">删除</a>';
        if(item.top == 1) {
            innerdata += '<a data-method="top" class="layui-btn layui-btn-sm layui-btn handle" data-id="' + item.id + '" data-state="2">置顶</a>';
        }else if(item.top == 2) {
            innerdata += '<a data-method="top" class="layui-btn layui-btn-sm layui-btn handle" data-id="' + item.id + '" data-state="1">取消置顶</a>';
        }
        innerdata += '</td>';
        innerdata += '</tr>';
        if (item.children) {
            floatdatatpl(item.children);
        }
    });
}

5.当当当,成功实现!!!