整合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(['form','layer','element','tree', 'util'],function(){ $ = layui.jquery; var form = layui.form ,layer = layui.layer; var tree = layui.tree ,util = layui.util; var element = layui.element; form.on('checkbox(checkeds)', function(data){ if(data.elem.checked){ $('.checkone').prop('checked',true); }else{ $('.checkone').prop('checked',false); } form.render('checkbox'); }); //调用数据 $(document).ready(function(){ $.get('{:url("getCategoryList")}').done(function (data) { var arr =$.parseJSON(data); //alert(arr); tree.render({ elem: '#test12' ,data: arr ,showCheckbox: false //是否显示复选框 ,id: 'demoId1' ,isJump: false //是否允许点击节点时弹出新窗口跳转 ,click: function(obj){ //var data = obj.data; //获取当前点击的节点数据 var dump_url = obj.data.href; window.location.href = dump_url; //layer.msg('状态:'+ obj.state + '<br>节点数据:' + JSON.stringify(data)); } }); }) }); }); </script> </body> </html>
#第二步:書寫後台PHP程式碼,這裡我是根據TP5.1
的規則書寫的程式碼
【PHP程式碼】
//获取栏目信息 public function getCategoryList(){ $id = Request::param('id'); $parentId = $this->getAllParentId($id);//获取父级ID信息 $list = AreaModel::where('pid',100000) ->where('status',0) ->order('order asc') ->select(); $cate = array(); foreach ($list as $key=>$v){ $cate[$key]['title'] = $v['name']; if(in_array($v['id'],$parentId)){ $cate[$key]['checked'] = 'true'; $cate[$key]['spread'] = 'true'; } $cate[$key]['field'] = 'name'.$v['id']; $cate[$key]['id'] = $v['id']; $cate[$key]['href'] = "/yejuzhi/article/index?cid=".$v['id']; $child = AreaModel::where('pid',$v['id']) ->where('status',0) ->order('order asc') ->select(); if($child){ foreach ($child as $key1=>$c){ /*$cate[$key]['children'][] = array( 'title' => $c['c_name'], 'id' => $c['id'], 'href' => "/yejuzhi/article/index?cid=".$c['id'], 'children' => array() );*/ $cate[$key]['children'][$key1]['title'] = $c['name']; $cate[$key]['children'][$key1]['checked'] = 'true'; $cate[$key]['children'][$key1]['spread'] = 'true'; $cate[$key]['children'][$key1]['field'] = 'name'.$c['id']; $cate[$key]['children'][$key1]['id'] = $c['id']; $cate[$key]['children'][$key1]['href'] = ''; $child1 = AreaModel::where('pid',$c['id']) ->where('status',0) ->order('order asc') ->select(); foreach ($child1 as $key2=>$value){ $cate[$key]['children'][$key1]['children'][$key2]['title'] = $value['name']; $cate[$key]['children'][$key1]['children'][$key2]['checked'] = 'true'; $cate[$key]['children'][$key1]['children'][$key2]['spread'] = 'true'; $cate[$key]['children'][$key1]['children'][$key2]['field'] = 'name'.$value['id']; $cate[$key]['children'][$key1]['children'][$key2]['id'] = $value['id']; $cate[$key]['children'][$key1]['children'][$key2]['href'] = ''; } } } //$cate[$key]['children'][] = array(); } //dump($cate); return json_encode($cate); }
在上面用到了一個方法:getAllParentId
,這個方法是專門用來取得所有父級ID的方法,以下是程式碼資訊:
//获取父级栏目 public function getAllParentId($id = 43){ static $parentId; $cates = AreaModel::where('id',$id)->find(); if($cates['pid']==0){ $parentId[] = $cates['id']; } $list = AreaModel::where('status',0) ->order('order asc') ->select(); foreach ($list as $k => $v) { if ($cates['pid'] == $v['id']) { $parentId[] = $v['id']; $this->getAllParentId($v['id']); } } return $parentId; }
大家可以試試看,希望能夠給與大家幫忙。我這裡是對城市資訊的循環輸出,大家也可以嘗試進行其他資訊的輸出,只需要修改資料呼叫規則即可,但是需要注意的是,不管如何呼叫父級、子級是必須存在的。
謝謝!
#相關推薦:最新的10個thinkphp影片教學
以上是TP5.1+layui實現欄位數的調用的詳細內容。更多資訊請關注PHP中文網其他相關文章!