Home > PHP Framework > ThinkPHP > TP5.1+layui realizes the calling of column number

TP5.1+layui realizes the calling of column number

XuPing.Yang
Release: 2021-05-08 08:58:14
Original
1614 people have browsed it

IntegrationLayui TP5.1, sometimes it is necessary to call the column number, which requires dynamic call of data. I have tried many methods before, but none of them worked. I checked the JS data call later. method, combined with layui and tried many times, finally found a way to achieve this function, I hope it will be useful to everyone.

The following example is an example of calling a city. The database fields include: id, order (sort), pid (upper-level city ID), name (city name), which includes provincial, municipal, and county levels. /District-level and third-level cities.

Step one: According to the method given by Layui, first write the HTML page, as shown below:

where ___ADMIN__ is yourself Just modify the configured style path to your own path. In addition, since the data calling here uses js data calling, the jquery library needs to be loaded. I am using jquery-3.2.1 here. You can load the corresponding version of jquery according to your actual needs. Library, not much to say here, just go to the code:

[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>
Copy after login

Step 2: Write the background PHP code , here I wrote the code according to the rules of TP5.1

[PHP code]

//获取栏目信息
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);
}
Copy after login

A method was used above:getAllParentId, this method is specially used to get all parent IDs. The following is the code information:

//获取父级栏目
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;
}
Copy after login

You can try it, I hope it can help you. What I am here is a cyclic output of city information. You can also try to output other information. You only need to modify the data calling rules. However, it should be noted that no matter how you call the parent and child, they must exist.

Thanks!

Related recommendations: The latest 10 thinkphp video tutorials

The above is the detailed content of TP5.1+layui realizes the calling of column number. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template