问答项目---递归重新排序无限极子分类数组

递归重新排序无限极子分类数组方法:

// 递归重新排序无限极子分类数组
function recursive($array,$pid=0,$level=0){
    $arr = array();
    foreach ($array as $v) {
        if($v['pid'] == $pid){
            $v['level'] = $level;
            $v['html'] = str_repeat('--',$level);
            $arr[] = $v;            
            $arr = array_merge($arr,recursive($array,$v['id'],$level+1));
        }
    }
    return $arr;
}

栏目做无限极分类:

public function index(){
    $cateTopList = M('category')->select();
    $cateTopList = recursive($cateTopList);
    $this->assign('cateTopList',$cateTopList);
    $this->display();
}

前台显示:

<foreach name="cateTopList" item="vo">
<tr id="{$vo['id']}" pid="{$vo['pid']}" <?php if($vo['pid'] !=0): ?><?php echo "style='display:none;'"; ?><?php endif; ?>>
    <td align="center" width="15%"><span class="open">+</span></td>
    <td align="center" width="15%">{$vo.id}</td>
    <td align="left" width="35%">{:str_repeat('&nbsp;&nbsp;',$vo['level'])}<if condition="$vo['level'] gt 0">|</if>{$vo.html}{$vo.name}</td>
    <td align="center">
        <a href="javascript:;" srcHref="{:U('addChild',array('pid'=>$vo['id']))}" class="bt1">添加子分类</a>
        <a href="javascript:;" srcHref="{:U('edit',array('pid'=>$vo['id']))}">修改</a>
        <a href="javascript:;"srcHref="{:U('delCate',array('pid'=>$vo['id']))}" onclick="if(confirm('确定删除?')==false)return false;">删除</a>
    </td>
</tr>
</foreach>

JS控制显示:

<script type="text/javascript">
    //$('tr[pid != 0]').hide();
    $( '.open' ).toggle( function () {
        var index = $( this ).parents('tr').attr('id');
        $( this ).html( '-' );
        $( 'tr[pid=' + index + ']' ).show();
    }, function () {
        var index = $( this ).parents('tr').attr('id');
        $( this ).html( '+' );
        $( 'tr[pid=' + index + ']' ).hide();
    } );
</script>
原文地址:https://www.cnblogs.com/e0yu/p/7355504.html