【问题标题】:How to loop data inside laravel blade tables?如何在 laravel 刀片表中循环数据?
【发布时间】:2021-01-21 08:51:29
【问题描述】:

我在 laravel 刀片文件中有这个表。

@foreach($items as $item)
<table class="pack-table">
    <tr>
        <th width="30%">Color</th>
        <th width="10%">Pack</th>
        <th width="60%">Total Units</th>
    </tr>
    <tr>
        <td>
            @foreach($item['grid'] as $color => $sizes)
                {{$color}}<br><br>
            @endforeach
        </td>
        <td>
            @foreach ($item['grid'] as $color => $sizes)
                @foreach ($sizes as $size)
                    {{ $size['description'] }}<br><br>
                @endforeach
            @endforeach
        </td>
        <td>
            @foreach ($item['grid'] as $color => $sizes)
                @foreach ($sizes as $size)
                    {{ $size['total'] }}<br><br>
                @endforeach
            @endforeach
        </td>
    </tr>
</table>
@endforeach

此代码在图片中打印如下表格。

这只是打印每个数据。但我希望打印如下: 例如颜色,VELBL 的尺寸为 MEDIUM、X LARGE、LARGE、X SMALL 和 SMALL,所有这些包装尺寸的总单位为 0 .

所以我需要先打印所有这些记录,然后再打印下一种颜色BLUAS,它是包装尺寸,以及每种颜色的总和。

如何修改我的代码以根据需要打印?

更新:

<table class="pack-table">
    <tr>
        <th width="30%">Color</th>
        <th width="10%">Pack</th>
        <th width="60%">Total Units</th>
    </tr>
    @foreach($item['grid'] as $color => $sizes)
    <tr>
        <td colspan="3" align="left">{{ $color }}</td>
    </tr>
    @foreach ($sizes as $size)
    <tr>
        <td>{{ $size['description'] }}</td>
        <td>{{ $size['total'] }}</td>
    </tr>
    @endforeach
    @endforeach
</table>

【问题讨论】:

  • 你可以用 div 做得更好。如果你想用 来做,你必须在你的
    中做 sub
    来分组数据
  • 你能给我一个关于子表的例子吗?

标签: laravel foreach html-table laravel-blade


【解决方案1】:

这是您正在寻找的更少循环的内容

@foreach($items as $item)
<table class="pack-table">
    <tr>
        <th width="30%">Color</th>
        <th width="10%">Pack</th>
        <th width="60%">Total Units</th>
    </tr>
    @foreach($item['grid'] as $color => $sizes)
    <tr>
        <td colspan="{{count($sizes)+1}}">
            {{$color}}
        </td>
        <?php $totals = 0 ?>
        @foreach ($sizes as $size)
        <td>
            {{ $size['description'] }}
        </td>
        <td>
            <?php $totals += $size['total']  ?>
            {{ $size['total'] }}
        </td>
    </tr>
    <tr>
        @endforeach
        <td>TOTAL</td>
        <td>{{$totals}}</td>
    </tr>
    @endforeach
</table>
@endforeach

【讨论】:

    【解决方案2】:

    使用此代码

    @foreach($items as $item)
        <table class="pack-table">
            <tr>
                <th width="30%">Color</th>
                <th width="10%">Pack</th>
                <th width="60%">Total Units</th>
            </tr>
            @foreach($item['grid'] as $color => $sizes)
                <tr>
                    <td colspan="3" align="left">{{ $color }}</td>
                </tr>
                @foreach ($sizes as $size)
                    <tr>
                        <td></td>
                        <td>{{ $size['description'] }}</td>
                        <td>{{ $size['total'] }}</td>
                    </tr>
                @endforeach
            @endforeach
        </table>
    @endforeach
    

    【讨论】:

    • 感谢您的回答,但打印的表格与预期不符,请检查我已用图片更新的问题
    • @user13080158 您忘记在&lt;td&gt; {{$ size ['description']}} &lt;/td&gt; 上方添加一个空的td。因此,您有一个颜色所在的包。你能用我发给你的选项显示它是如何打印的吗?这样我就可以做出改变
    • 非常感谢您的回答,它给了我预期的确切结果。但问题是 align="left" 在那里不起作用,所以我在那里添加了这一行 style="text-align:left;"现在按预期打印。
    【解决方案3】:

    我认为是这样的:

    @foreach($items as $item)
        <table class="pack-table">
            <tr>
                <th width="30%">Color</th>
                <th width="10%">Pack</th>
                <th width="60%">Total Units</th>
            </tr>
            @foreach($item['grid'] as $color => $sizes)
                <tr>
                    <td>{{ $color }}</td>
                    <td>
                        <table>
                        <?php $total = 0; ?>
                        @foreach ($sizes as $size)
                            <tr>
                                <td>{{ $size['description'] }}</td>
                            <tr>
                            <?php $total += $size['total'] ?>
                        @endforeach
                        </table>
                    </td>
                    <td>{{ $total }}</td>
                </tr>
            @endforeach
            
        </table>
    @endforeach
    

    【讨论】:

      猜你喜欢
      • 2017-09-26
      • 2018-04-04
      • 2016-06-29
      • 1970-01-01
      • 2016-04-14
      • 2021-06-19
      • 2017-11-13
      • 2017-12-24
      • 2016-10-28
      相关资源
      最近更新 更多