频道栏目
首页 > 程序开发 > Web开发 > php > 正文
laravel5.5 PHP递归实现无限级分类列表的代码教程
2018-05-14 15:25:41      个评论    来源:weishuo110的博客  
收藏   我要投稿

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.当当当,成功实现!!!

点击复制链接 与好友分享!回本站首页
上一篇:php底层运行机制与原理详解
下一篇:分享一个PHP的开发流程
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站