繁体   English   中英

如何将结果分成3列

[英]How to chunk results into 3 columns

我有一组结果(例如[1,2,3,4,5,6,7,8,9,10,11])。

我想显示为1行3列

1|5|9
2|6|10
3|7|11
4|8|

我得到的是1行4列

1|4|7
2|5|8
3|6|9

10|
11|

在添加第12个对象之前,我得到1行3列

1|5|9
2|6|10
3|7|11
4|8|12

我在刀片模板中的代码

<!-- partials/tables/table_list.blade.php -->

@section('tables')
<?php $chunkSize = floor(count($tables) / 3); ?>
<section id="tables-overview">
    <div class="row">
        @foreach ($tables->chunk($chunkSize , 1) as $chunk)
        <div class="col-md-4">
            @include('partials.tables.table_chunk')
        </div>
        @endforeach
    </div>
</section>
@endsection

<!-- partials/tables/table_chunk.blade.php -->

<table class="table table-responsive">
<thead>
    <tr>
        <th class="text-center">@lang('table.identifier')</th>
        <th class="text-center">@lang('table.property')</th>
        <th class="text-center">
            @permission('manage-tables')
                <a href="{{ route('CreateTable') }}">@lang('action.create')</a>
            @endpermission
        </th>
    </tr>
</thead>
<tbody>
    @foreach ($chunk as $table)
    <tr>
        <td class="text-center">{{ $table->getId() }}</td>
        <td class="text-center">{{ $table->getProperty() }}</td>
        <td class="text-center">
            @permission('manage-tables')
                <a class="btn btn-primary" href="{{ route('UpdateTable', ['id' => $table->getId()]) }}">@lang('action.update')</a>
            @endpermission
        </td>
    </tr>
    @endforeach

</tbody>
</table>

当$ tables的数量可以除以3(我想要的列数)时,我得到3个块。 如果没有,我得到3个块+其余的1或2个对象,它们都放在第4列中。 我可以像这里一样将它们水平放置,但是我觉得这很奇怪。 阅读列表时,您将首先从上至下阅读,然后从左至右阅读。

更新我也尝试过使用Huzaib Shafi建议的ceil()。 但后来我明白了

4 objects (funny looking)
1|3|
2|4|

5 objects (better looking)
1|3|5
2|4

这也不是我想要的100%,但非常接近它。 接下来,我将尝试Homam Alhaytham的建议。

当你floor的划分结果,你会得到较低的数字。

说明: floor(11/3)=3。因此,您的结果一次被分成3个块(导致; [3,3,3,2]),但是我们需要[4,4,3]。

因此,您需要将其ceil 给你4,结果。

<?php $chunkSize = ceil(count($tables) / 3); ?>

通过获取数据库数据并使用循环示例获得$ items之后

$c=0;
echo '<div class="row">';
while(bla bla bla ..){
// counter
$c++;
echo '<div class="col-md-4">
some thing 
        </div>';
if(fmod($c,3)==0){
echo '</div><div class="row">';
}
}

echo '</div>';

在这里,我使用fmod($ c,3)3从您的列上减去,如果$ c是6,9,12,15,18 .....,fmod返回0

使用array_chunk

假设您具有以下数组:

[
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
    11,
];

在您的.blade.php文件中,您可以执行以下操作:

@section('tables')
<section id="tables-overview">
    <div class="row">
        @foreach (array_chunk($tables, 3) as  $chunk)
        <div class="col-md-4">
            @foreach($chunk as $key => $value)
            //@include('partials.tables.table_chunk')
            // No need to include anything here
            // just write your table instead of including
            @endforeach
        </div>
        @endforeach
    </div>
</section>
@endsection

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM