Laravel Blade 当变量值在循环中不同时更改行颜色

Laravel Blade change row color when variable value is different in loop

我有一个循环遍历 collection 并在通用 bootstrap 模板 table 中显示数据的报告页面 - 它显示有关用户的数据,一个用户可以有多个table 中的行。我按用户 ID 对行进行分组,我想基本上根据用户 ID 对 table 行进行条带化(而不仅仅是交替行)。因此,例如,我可能会为用户 ID = 5 显示 4 行,背景为白色,然后为用户 ID = 6 显示 2 行,背景为灰色。我只是直接在 php 中使用局部变量,但是在 blade 模板中是否有 elegant/clean 方法来做到这一点?

@foreach($payments->getDetails() AS $k=>$detail)
<tr>
    <td>{{ $detail->payment->userid }}</td>
    <td>${{ $detail->payment->amount }}</td>
    <td>{{ $detail->payment->status }}</td>
</tr>
@endforeach

在此示例中,$payments 是 $paymentsDetails objects 的 collection。因此,根据 $detail->payment->userid 值,我想确定 class 或背景颜色。

感谢您在这方面的帮助。

只是一个想法...这假设行自然地按用户 ID 排序(无论如何在您的示例中它不会工作...)...所以这是想法...

如果用户 id 是奇数,则给它一个颜色,如果它是偶数,则给它另一种颜色...这样可以...有点...

@foreach($payments->getDetails() AS $k=>$detail)
<tr>
    <td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->userid }}</td>
    <td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->amount }}</td>
    <td class="{{$detail->payment->userid % 2 == 0 ? 'evenColor':'oddColor'">{{ $detail->payment->status }}</td>
</tr>
@endforeach

像这样的东西也可以……虽然我不确定我是否喜欢它

@foreach($payments->getDetails() AS $k=>$detail)
    @if($detail->payment->userid % 2 == 0)
        <tr>
            <td class="evencolor">{{ $detail->payment->userid }}</td>
            <td class="evencolor">>${{ $detail->payment->amount }}</td>
            <td class="evencolor">{{ $detail->payment->status }}</td>
        </tr>
    @else
        <tr>
            <td class="oddcolor">{{ $detail->payment->userid }}</td>
            <td class="oddcolor">${{ $detail->payment->amount }}</td>
            <td class="oddcolor">{{ $detail->payment->status }}</td>
        </tr>
    @endif
@endforeach

无论如何...只是一个可能的方向...其他人可能有更优雅的想法...

我通过 www.laracasts.com 从用户 @Cronix 那里得到了解决方案:

I'd do it the way you were thinking originally and just use @php/@endphp blade directives to determine if the userid has changed during each loop iteration and if it does change the color.

@php 
$lastid = null;
$rowclass = 'grey';
@endphp

@foreach($payments->getDetails() AS $k=>$detail)
@php 
 //if userid changed from last iteration, store new userid and change color
 if ($lastid !== $detail->payment->userid)
 {
    $lastid = $detail->payment->userid;
    if ($rowclass == 'grey') $rowclass = 'white';
    else $rowclass = 'grey';
 }
@endphp
<tr class="{{ $rowclass }}">
    <td>{{ $detail->payment->userid }}</td>
    <td>${{ $detail->payment->amount }}</td>
    <td>{{ $detail->payment->status }}</td>
</tr>
@endforeach

如果有人对如何解决它有更优雅的 Blade-focused 想法,我很想听听。

嘿伙计们,上面的方法对我来说失败了,以防其他人需要这个:我正在使用 laravel 5.4 和 php 5.6===> 这对我有用;

@php

                if ( $post->dbdata == 'approved'):
                    $color = 'green';
                elseif ( $post->dbdata == 'Pending'):
                $color = 'yellow';
                elseif ( $post->dbdata == 'rejected'):
                $color = 'red';
                else:
                $color = 'black';
                endif;

                @endphp
                <tr style="background-color: {{$color}}">
                    <td>
                        <h6>{{ $post->dbdata }}</h6>
                    </td>
                </tr>