<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.CardView
android:id="@+id/mRequestCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="100dp"
android:orientation="horizontal"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/vDivisor"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/colorGreen" />
...
</RelativeLayout>
</android.support.v7.widget.CardView>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:backgroundTint="@color/colorAccent"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher"
app:layout_anchor="@id/mRequestCard"
app:layout_anchorGravity="right|end|bottom" />
</RelativeLayout>
我使用的代码就是这个,
[
{
text: 'Parent 1',
href: '#parent1',
tags: ['4'],
nodes: [
{
text: 'Child 1',
href: '#child1',
tags: ['2'],
nodes: [
{
text: 'Grandchild 1',
href: '#grandchild1',
tags: ['0']
},
{
text: 'Grandchild 2',
href: '#grandchild2',
tags: ['0']
}
]
},
{
text: 'Child 2',
href: '#child2',
tags: ['0']
}
]
},
{
text: 'Parent 2',
href: '#parent2',
tags: ['0']
},
{
text: 'Parent 3',
href: '#parent3',
tags: ['0']
}
]
我得到的输出: output
上面的代码只提供了一个级别,但我想深入了解。如何格式化循环以完成它?
答案 0 :(得分:0)
你需要编写一个递归函数来实现它。您可以在Parent - Child - Grand Child category Markup in Codeigniter view
找到答案答案 1 :(得分:0)
尝试以下方法:
public function getCategoryTree()
{
$query = $this->db->select('id,parent_id,name')
->order_by('parent_id','asc')
->get('categories');
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
foreach( $result as $k => $v )
{
$count = 0;
$v['href'] = '#'. strtolower(str_replace(' ', '', $v['name']));
foreach( $result as $k2 => $v2 )
{
// if child has this parent, count
if( $v['id'] == $v2['parent_id'] )
$count++;
}
if( $count )
$v['tags'] = array((string)$count);
$mod[] = $v;
}
$tree = $this->buildTree($mod);
$this->key_unset_recursive( $tree, 'id' );
$this->key_unset_recursive( $tree, 'parent_id' );
echo json_encode( $tree, JSON_PRETTY_PRINT );
}
}
public function buildTree( array $elements, $parentId = 0 )
{
$branch = array();
foreach( $elements as $element )
{
if( $element['parent_id'] == $parentId )
{
$children = $this->buildTree($elements, $element['id']);
if( $children )
{
$element['nodes'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
public function key_unset_recursive( &$array, $remove )
{
foreach( $array as $key => &$value )
{
if( $key === $remove )
{
unset( $array[$key] );
}
else if( is_array( $value ) )
{
$this->key_unset_recursive( $value, $remove );
}
}
}