无限极分类的应用
后端php无限极分类
$items = array();
foreach($res_arr as $v){
$items[$v['id']] = $v;
}
$tree = array();
foreach($items as $k => $item){
if(isset($items[$item['fid']])){
$items[$item['fid']]['son'][] = &$items[$k];
}else{
$tree[] = &$items[$k];
}
}
$json_arr = json_encode($tree);
echo $json_arr;
前端递归使用树形结构
function wxj(data){
for(var i = 0;i<data.length;i++){
var fname = data[i].nc;
$('#container').append("<div class='container-box'><div class='text-title'>"+data[i].name+"</div><div class='text-container'>"+data[i].content+"</div><div class='text-img'><a class='hf' id='hf' >回复</a><input id='parent_id' type='hidden' value='"+data[i].id+"'/> <a class='ck' id='ck' >查看回复</a></div><div class='text-info'>作者:"+data[i].nc+"<br>ID:"+data[i].username+"</div></div><div id='hf-container"+i+"' style='width:100%; display:none; background-color: #e1e1e1;'></div>");
if(data[i].son){
wxj_son(data[i].son,i,fname);
}
}
}
function wxj_son(data,i,fname){
for(var s = 0;s < data.length; s++){
var fname1 = data[s].nc;
var name = "#hf-container"+i;
$("#hf-container"+i).append("<div class = 'child_content'>"+data[s].nc+"@"+fname+":"+data[s].content+"<a class = 'hf' id='hf'>回复</a><input id='parent_id' type='hidden' value='"+data[s].id+"'/></div>");
if(data[s].son){
wxj_son(data[s].son,i,fname1);
}
}
}
function ajax(){
$.ajaxSettings.async = false;
$.post("<?= site_url('message/showContents')?>",{},
function(data){
var data = $.parseJSON(data);
wxj(data);
});
}
ajax();