我试图在我的 $key
时设置颜色值被改变。我的意思是当我的 $key
生成一个表时在改变。现在我想在 $key 发生变化 并创建一个新表时设置不同的 bg-color。现在 <th colspan="5">{{$key}}</th>
正在为每个 $key
更改和创建表我需要设置 $key
的颜色在改变。
@foreach($lists as $key => $ser)
<table class="table table-striped table-bordered table-hover" id="sample_3">
<thead>
<tr>
<th colspan="5">{{$key}}</th>
</tr>
<tr>
<th> Destination</th>
<th> Services</th>
<th> Status</th>
<th> Time</th>
</tr>
</thead>
<tbody>
@foreach($ser as $s)
<tr>
<td style="background: rgb(176,224,230);"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
@endforeach
</tbody>
</table>
@endforeach
最佳答案
您可以使用变量来存储每个循环中$key
的值。然后您可以检查 $key
是否与前一个相同,并相应地更改颜色。
@php($prevvalue='')
@foreach($ser as $s)
<tr>
<td style="background: {{$s->key == $prevvalue ? 'oldcolor','newcolor'}}"> TE 17 <br/>{{$s->sp}}</td>
<td> {{$s->dd}}</td>
<td> {{$s->ss}}</td>
<td> {{$s->dt}}</td>
</tr>
@php($prevvalue = $s->key)
@endforeach
关于css - 如何根据foreach循环中的键更改更改表 'td'颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54733376/