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

TP5.1+layui realizes the calling of column number

Apr 25, 2021 am 11:13 AM
layui tp5.1

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up jump on layui login page How to set up jump on layui login page Apr 04, 2024 am 03:12 AM

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

How to get form data in layui How to get form data in layui Apr 04, 2024 am 03:39 AM

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

How to transfer data in layui How to transfer data in layui Apr 26, 2024 am 03:39 AM

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

What is the difference between layui and vue? What is the difference between layui and vue? Apr 04, 2024 am 03:54 AM

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

What does layui mean? What does layui mean? Apr 04, 2024 am 04:33 AM

layui is a front-end UI framework that provides a wealth of UI components, tools and functions to help developers quickly build modern, responsive and interactive web applications. Its features include: flexible and lightweight, modular design, rich components, Powerful tools and easy customization. It is widely used in the development of various web applications, including management systems, e-commerce platforms, content management systems, social networks and mobile applications.

What language is layui framework? What language is layui framework? Apr 04, 2024 am 04:39 AM

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

The difference between layui framework and vue framework The difference between layui framework and vue framework Apr 26, 2024 am 01:27 AM

layui and vue are front-end frameworks. layui is a lightweight library that provides UI components and tools; vue is a comprehensive framework that provides UI components, state management, data binding, routing and other functions. layui is based on a modular architecture, and vue is based on a componentized architecture. layui has a smaller ecosystem, vue has a large and active ecosystem. The learning curve of layui is low, and the learning curve of vue is steep. Layui is suitable for small projects and rapid development of UI components, while vue is suitable for large projects and scenarios that require rich functions.

See all articles