你好,我有一个集合分组为多维数组,其中包含键,例如:
"book1" {
{
"id" : "1",
"group" : "book1",
"name" : "Book X",
"buy" : "140",
"test" : "test 1",
} ,
{
"id" : "2",
"group" : "book1",
"name" : "Book y",
"buy" : "1200",
"test" : "test 2",
} ,
{
"id" : "3",
"group" : "book1",
"name" : "Book Z",
"buy" : "1330",
"test" : "344",
}
},
"book2" {
{
"id" : "6",
"group" : "book2",
"name" : "Book N",
"buy" : "1220",
"test" : "233",
}
....
} 我希望创建一个具有拆分行的表,因为第一列具有键(Book),而其他列动态地为子数组拆分行,例如:(这是一种html代码)。
<table>
<tr>
<th>categorie</th>
<th>Name</th>
<th>Buy</th>
</tr>
<tr>
<td>Book 1</td>
<td>Book AA</td>
<td>14</td>
</tr>
<tr>
<td rowspan="2">Book 2</td>
<td>Book BA</td>
<td>12</td>
</tr>
<tr>
<td>Book BB</td>
<td>14</td>
</tr>
<tr>
<td rowspan="2">Book 3</td>
<td>Book CA</td>
<td>22</td>
</tr>
<tr>
<td>Book CB</td>
<td>§§</td>
</tr>
<tr>
<td rowspan="3">Book 4</td>
<td>Book DA</td>
<td>12</td>
</tr>
<tr>
<td>Book DB</td>
<td>122</td>
</tr>
<tr>
<td>Book DC</td>
<td>11</td>
</tr>
</table>您能帮助使这个html示例依赖于集合和子数组的动态表.还有谢谢你!
发布于 2020-02-21 22:22:15
我通过使用以下代码来修复它:
<table class="table table-striped table-inverse table-responsive">
<thead>
<tr>
<th>Book</th>
<th>Name</th>
<th>Category</th>
<th>Buy</th>
</tr>
</thead>
<tbody>
@foreach($collection as $key => $items)
@foreach($items->books->groupBy('categorie') as $book)
@foreach ($book as $index => $item)
@if ($index == "0")
<tr >
<td class="align-middle" rowspan="{{count($book)}}">{{$item->book->category}}</td>
<td >{{$item->name}}</td>
<td>{{$item->buy}}</td>
<td>{{$item->book}}</td>
<td class="align-middle text-center" rowspan="{{count($book)}}">16</td>
</tr>
@else
<tr >
<td >{{$item->name}}</td>
<td>{{$item->buy}}</td>
<td>{{$item->book}}</td>
</tr>
@endif
@endforeach
@endforeach
@endforeach
</tbody>
</table>感谢每一个试图帮助我的人
发布于 2020-02-21 18:51:39
$books = Books::all();
$books->groupby('group');例:
$collection = collect([
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
['account_id' => 'account-x11', 'product' => 'Desk'],
]);
$grouped = $collection->groupBy('account_id');
$grouped->toArray();
/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/在你的刀刃里像这样
<table>
<thead>
<tr>
<th>categorie</th>
<th>Name</th>
<th>Buy</th>
</tr>
</thead>
<tbody>
@foreach($grouped as $key => $items)
@foreach(array_chunk($items, 2) as $chunk)
<tr>
@foreach($chunk as $index => $item)
<td @if($index == 0) rowspan="2" @endif>$key</td>
<td>$book->name</td>
<td>$book->buy</td>
@endforeach
</tr>
@endforeach
@endforeach
</tbody>
</table> https://stackoverflow.com/questions/60344505
复制相似问题