插件窝 干货文章 TP5.1+layui实现栏目数的调用

TP5.1+layui实现栏目数的调用

amp 39 key children 163    来源:    2024-10-27

整合layui+tp5.1时,有时候需要调用栏目数,这就需要进行数据的动态化调用,之前试过很多种方法,都没有生效,后面查看了js数据调用的方法,结合layui进行多次尝试,终于找出了能够实现这一功能的方法,希望对大家有用。

以下的例子是一个调用城市的实例,数据库字段包括:id,order(排序),pid(上级城市ID)、name(城市名称),这里包含了省级、市级、县/区级三级城市。

第一步:根据Layui给出的方法,首先写出HTML页面,如下所示:

其中___ADMIN__是你自己配置的样式路径,修改成自己的路径即可。另外由于这里调用数据采用的是js数据调用,所以需要加载jquery库,我这里用的是jquery-3.2.1,大家可以根据自己的实际需求加载对应版本的jquery库,这里不多说,直接上代码:

【HTML】

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <link rel="stylesheet" href="__ADMIN__/js/jquery-3.2.1.js">
   <link rel="stylesheet" href="__ADMIN__/layui/css/layui.css">
   <script type="text/javascript" src="__ADMIN__/layui/layui.js"></script>
</head>
<body>
<!--test12对应的是layuitree.render中的elem值,这两个值必须一致-->
<div id="test12"></div>
<script type="text/javascript">
layui.use([&#39;form&#39;,&#39;layer&#39;,&#39;element&#39;,&#39;tree&#39;, &#39;util&#39;],function(){
       $ = layui.jquery;
        var form = layui.form
        ,layer = layui.layer;
        var tree = layui.tree
        ,util = layui.util;
        var element = layui.element;
        form.on(&#39;checkbox(checkeds)&#39;, function(data){
           if(data.elem.checked){
               $(&#39;.checkone&#39;).prop(&#39;checked&#39;,true);
        }else{
               $(&#39;.checkone&#39;).prop(&#39;checked&#39;,false);
        }
           form.render(&#39;checkbox&#39;);
        });
   //调用数据
    $(document).ready(function(){
           $.get(&#39;{:url("getCategoryList")}&#39;).done(function (data) {
               var arr =$.parseJSON(data);
        //alert(arr);
        tree.render({
                   elem: &#39;#test12&#39;
        ,data: arr
                   ,showCheckbox: false  //是否显示复选框
        ,id: &#39;demoId1&#39;
        ,isJump: false //是否允许点击节点时弹出新窗口跳转
        ,click: function(obj){
                       //var data = obj.data;  //获取当前点击的节点数据
        var dump_url = obj.data.href;
        window.location.href = dump_url;
        //layer.msg(&#39;状态:&#39;+ obj.state + &#39;<br>节点数据:&#39; +                                 JSON.stringify(data));
        }
               });
        })
       });
});
</script>
</body>
</html>

第二步:书写后台PHP代码,这里我是根据TP5.1 的规则书写的代码

【PHP代码】

//获取栏目信息
public function getCategoryList(){
   $id = Request::param(&#39;id&#39;);
   $parentId = $this->getAllParentId($id);//获取父级ID信息
   $list = AreaModel::where(&#39;pid&#39;,100000)
       ->where(&#39;status&#39;,0)
       ->order(&#39;order asc&#39;)
       ->select();
   $cate = array();
   foreach ($list as $key=>$v){
       $cate[$key][&#39;title&#39;] = $v[&#39;name&#39;];
       if(in_array($v[&#39;id&#39;],$parentId)){
           $cate[$key][&#39;checked&#39;] = &#39;true&#39;;
           $cate[$key][&#39;spread&#39;] = &#39;true&#39;;
       }

       $cate[$key][&#39;field&#39;] = &#39;name&#39;.$v[&#39;id&#39;];
       $cate[$key][&#39;id&#39;] = $v[&#39;id&#39;];
       $cate[$key][&#39;href&#39;] = "/yejuzhi/article/index?cid=".$v[&#39;id&#39;];
       $child = AreaModel::where(&#39;pid&#39;,$v[&#39;id&#39;])
           ->where(&#39;status&#39;,0)
           ->order(&#39;order asc&#39;)
           ->select();
       if($child){
           foreach ($child as $key1=>$c){
               /*$cate[$key][&#39;children&#39;][] = array(
                   &#39;title&#39; => $c[&#39;c_name&#39;],
                   &#39;id&#39; => $c[&#39;id&#39;],
                   &#39;href&#39; => "/yejuzhi/article/index?cid=".$c[&#39;id&#39;],
                   &#39;children&#39; => array()
               );*/
               $cate[$key][&#39;children&#39;][$key1][&#39;title&#39;] = $c[&#39;name&#39;];
               $cate[$key][&#39;children&#39;][$key1][&#39;checked&#39;] = &#39;true&#39;;
               $cate[$key][&#39;children&#39;][$key1][&#39;spread&#39;] = &#39;true&#39;;
               $cate[$key][&#39;children&#39;][$key1][&#39;field&#39;] = &#39;name&#39;.$c[&#39;id&#39;];
               $cate[$key][&#39;children&#39;][$key1][&#39;id&#39;] = $c[&#39;id&#39;];
               $cate[$key][&#39;children&#39;][$key1][&#39;href&#39;] = &#39;&#39;;
               $child1 = AreaModel::where(&#39;pid&#39;,$c[&#39;id&#39;])
                   ->where(&#39;status&#39;,0)
                   ->order(&#39;order asc&#39;)
                   ->select();
               foreach ($child1 as $key2=>$value){
                   $cate[$key][&#39;children&#39;][$key1][&#39;children&#39;][$key2][&#39;title&#39;] = $value[&#39;name&#39;];
                   $cate[$key][&#39;children&#39;][$key1][&#39;children&#39;][$key2][&#39;checked&#39;] = &#39;true&#39;;
                   $cate[$key][&#39;children&#39;][$key1][&#39;children&#39;][$key2][&#39;spread&#39;] = &#39;true&#39;;
                   $cate[$key][&#39;children&#39;][$key1][&#39;children&#39;][$key2][&#39;field&#39;] = &#39;name&#39;.$value[&#39;id&#39;];
                   $cate[$key][&#39;children&#39;][$key1][&#39;children&#39;][$key2][&#39;id&#39;] = $value[&#39;id&#39;];
                   $cate[$key][&#39;children&#39;][$key1][&#39;children&#39;][$key2][&#39;href&#39;] = &#39;&#39;;
               }
           }
       }

       //$cate[$key][&#39;children&#39;][] = array();
   }
   //dump($cate);
   return json_encode($cate);
}

在上面用到了一个方法:getAllParentId,这个方法是专门用来获取所有父级ID的方法,下面是代码信息:

//获取父级栏目
public function getAllParentId($id = 43){
   static $parentId;
   $cates = AreaModel::where(&#39;id&#39;,$id)->find();
   if($cates[&#39;pid&#39;]==0){
       $parentId[] = $cates[&#39;id&#39;];
   }
   $list = AreaModel::where(&#39;status&#39;,0)
       ->order(&#39;order asc&#39;)
       ->select();
   foreach ($list as $k => $v) {
       if ($cates[&#39;pid&#39;] == $v[&#39;id&#39;]) {
           $parentId[] = $v[&#39;id&#39;];
           $this->getAllParentId($v[&#39;id&#39;]);
       }
   }
   return $parentId;
}

大家可以试一下,希望能够给与大家帮助。我这里是对城市信息的循环输出,大家也可以尝试进行其他信息的输出,只需要修改数据调用规则即可,但是需要注意的是,不管如何调用父级、子级是必须要存在的。

谢谢!

相关推荐:最新的10个thinkphp视频教程