麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

thinkphp實(shí)現(xiàn)無限分類(使用遞歸)

2019-11-20 10:58:50
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了thinkphp實(shí)現(xiàn)無限分類的詳細(xì)代碼,希望對(duì)大家學(xué)習(xí)無限分類有所啟發(fā)。

數(shù)據(jù)庫:test
數(shù)據(jù)表:(tp_category):


Common/conf/config.php

'DB_CONFIG2' => array( 'db_type' => 'mysql', 'db_user' => 'root', 'db_pwd' => '', 'db_host' => 'localhost', 'db_port' => '3306', 'db_name' => 'test', 'DB_PREFIX' => 'tp_', // 數(shù)據(jù)庫表前綴 'DB_CHARSET'=> 'utf8', // 字符集 'DB_DEBUG' => TRUE, // 數(shù)據(jù)庫調(diào)試模式 開啟后可以記錄SQL日志 3.2.3新增),

Common/function.php 遍歷函數(shù)loop

/* * 遞歸遍歷 * @param $data array * @param $id int * return array * */function recursion($data, $id=0) { $list = array(); foreach($data as $v) { if($v['pid'] == $id) { $v['son'] = recursion($data, $v['id']); if(empty($v['son'])) { unset($v['son']); } array_push($list, $v); } } return $list;}

Controller/IndexController.class.php

public function test() { $category = M('category', '', C('DB_CONFIG2'))->select(); $result = loop($category); var_dump($result); $this->assign('list', $result); $this->display();}

在模板(View/Index/test.html)中輸出(僅支持2級(jí)分類,如果想全部顯示,建議先把數(shù)組轉(zhuǎn)換成JSON格式,然后通過AJAX請(qǐng)求,JS生成)

<ul> <volist name="list" id="vo"> <li> {$vo.category} <notempty name="vo['children']"> <ul>  <volist name="vo['children']" id="cate">  <li>{$cate.category}</li>  </volist> </ul> </notempty> </li> </volist></ul>

后續(xù)(ajax請(qǐng)求,遞歸顯示所有分類):

方法 Controller/IndexController.class.php 

public function test() { $this->display();}public function resultCategory() { $category = M('category', '', C('DB_CONFIG2'))->select(); $result = loop($category); $this->ajaxReturn(array('data'=>$result,'status'=>'1','info'=>'獲取列表成功'));}

模板View/Index/test.html

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>分類測(cè)試</title> <script src="__PUBLIC__/js/jquery.min.js"></script></head><body><ul id="menu"></ul><script> $(function() { // 遞歸列表函數(shù) function recursion(selector,data) { if(!data) return false; for(var i=0;i<data.length;i++) { var li=$('<li>'+data[i]['category']+'</li>'); if(data[i]['children'] && data[i]['children'].length>0) {  var ul=$('<ul></ul>');  recursion(ul,data[i]['children']);  li.append(ul); } selector.append(li); } } // ajax請(qǐng)求 用$.post() 會(huì)更方便 $.ajax({ url: "{:U('resultCategory')}", type: 'post', dataType: 'json', error: function(res) { console.log(res); }, success: function(res) { recursion($('#menu'),res['data']); console.log(res['data']); } }); });</script></body></html>

另一種無限級(jí)分類:

/** * 無限極分類 * @param [type] $cate [description] * @param integer $pid [description] * @param integer $level [description] * @param string $html [description] * @return [type] [description] */function sortOut($cate,$pid=0,$level=0,$html='--'){ $tree = array(); foreach($cate as $v){ if($v['pid'] == $pid){ $v['level'] = $level + 1; $v['html'] = str_repeat($html, $level); $tree[] = $v; $tree = array_merge($tree, sortOut($cate,$v['id'],$level+1,$html)); } } return $tree;}

JS遞歸(特殊):

這個(gè)函數(shù)相當(dāng)于實(shí)現(xiàn)php的str_repeat函數(shù)

/* 字符串重復(fù)函數(shù) */if(!String.str_out_times) { String.prototype.str_out_times = function(l) { return new Array(l+1).join(this); }}
// 定位到當(dāng)前選擇function recursion(selector, data, j, pid) { var space = ' ┠ '; if(!data) return false; $.each(data, function(i, item) { var opt = $('<option value="'+item.id+'">'+space.str_out_times(j)+item.name+'</option>');selector.append(opt); if(item.son && (item.son).length>0) { recursion(selector, item.son, ++j); j=0;  } }); // 當(dāng)前是哪個(gè)分類 selector.find('option').each(function() { if($(this).val() == pid) { $(this).attr('selected', 'selected'); } });}

為什么j=0呢。因?yàn)閳?zhí)行順序感覺與php不同,這里是從上到下加載。。

ajax請(qǐng)求數(shù)據(jù):

$('.btn-edit').click(function() { var id = $(this).data('id'); $.post("{:U('Article/editArticle')}", {id: id}, function(res) { // 分類 $('[name="pid"]').html(''); recursion($('[name="pid"]'), res.sort, 0, res.pid); $('[name="id"]').val(res.id); $('[name="title"]').val(res.title); $('[name="summary"]').val(res.summary); $('#thumbnailImg').attr('src', "__UPLOAD__"+'/thumbnail/'+res.thumbnail); ue.setContent(res.content); $('#modal-edit').modal('show'); });});

以上就是thinkphp實(shí)現(xiàn)無限分類的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 老a影视网站在线观看免费 国产精品久久久久久久久久尿 | 国产亚洲精品综合一区91 | 国产精品免费小视频 | 亚洲一二区视频 | 在线91视频 | bt 自拍 另类 综合 欧美 | 午夜视频在线看 | 羞羞网站入口 | 国产成人高清在线 | 免费嗨片首页中文字幕 | 伊人99在线| 国产精品成人av片免费看最爱 | 色97在线| 69性欧美高清影院 | 久久久久久麻豆 | 另类亚洲孕妇分娩网址 | 亚洲一区国产二区 | 成年免费视频黄网站在线观看 | 国产一级免费视频 | 成人福利免费在线观看 | 国产91大片| 最新国产毛片 | 亚洲天堂字幕 | 国产免费一区视频 | 精品一区二区三区欧美 | 粉嫩粉嫩一区二区三区在线播放 | 久久久久久久久久久久久久av | 狠狠操视频网站 | 一级裸体视频 | 大学生一级毛片在线视频 | 中文字幕h| 日本在线视频免费观看 | 成人宗合网 | 日本欧美一区二区三区在线播 | 精品亚洲一区二区 | 56av国产精品久久久久久久 | 古装三级在线观看 | 福利在线影院 | 在线成人免费网站 | 一区二区三区欧美在线 | 九九热视频免费在线观看 |