我有这个报告,我试图做,但我想使行交替颜色。这是我尝试过的,但它不起作用。实现这一目标的正确方法是什么?
<div class="row">
{{$rowOrder = "even"}}
@foreach($data as $row)
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->adm_referraldate}}</span>
<span>{{$row->adm_number}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
<div class="col-sm-4 repColumn {{$rowOrder}}">
<span>{{$row->dmg_nhsnumber}}</span>
<span>{{$row->dmg_firstname." ".$row->dmg_surname}}</span>
<span>{{$row->dmg_dateofbirth." - (".$row->dmg_ageyears.")"}}</span>
<span>{{$row->dmg_sex}}</span>
</div>
@endforeach
</div>
答案 0 :(得分:1)
替换
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
带
<?php $rowOrder = ($rowOrder == "odd") ? 'even' : 'odd'; ?>
或者如果您使用的是Laravel 5.2或更高版本
@php($rowOrder = ($rowOrder == "odd") ? 'even' : 'odd')
对行{{$rowOrder = "even"}}
如果您使用{{$rowOrder = "even"}}
,它将回显结果。
答案 1 :(得分:1)
您可以使用模运算来确定和索引是奇数还是偶数:
$ isEven = index%2
如果你将它与PHP三元运算符结合起来,那么你就得到了这个
{{ $loop->index % 2 ? 'odd': 'even' }}
见
https://davidwalsh.name/php-shorthand-if-else-ternary-operators 和 https://en.wikipedia.org/wiki/Modular_arithmetic
答案 2 :(得分:1)
这是一个非常简单的解决方案:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"isBackground": true,
"problemMatcher": [
"$tsc"
]
},
{
"type": "typescript",
"label": "TypeScript Compiler Watcher Thingy...",
"tsconfig": "tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
答案 3 :(得分:0)
{{ $rowLine = ($rowOrder = "odd" ? 'even' : 'odd') }}
可能应该是
{{ $rowLine = ($rowOrder == "odd" ? 'even' : 'odd') }}
答案 4 :(得分:0)
这是我的一个工作示例:我将转储输出留在其中,以便您可以看到实际的数字计数。希望它可以帮助任何遇到这个问题的人:)。编辑:不要忘记在你的css文件中为.odd和.even添加颜色!
@if(!empty($names))
{{-- SET VARIABLE + HIDE IT --}}
<div class="hide">{!! $number = 0 !!}</div>
@foreach($names as $n)
{{ dump($number) }}
<div class="{!! $number % 2 == 0 ? 'odd' : 'even' !!}">
{{-- UP VARIABLE + HIDE IT --}}
<div class="hide">{!! $number++ !!}}</div>
{{-- DISPLAY CONTENT —}}
{{ $n }}
</div>
@endforeach
@endif
答案 5 :(得分:0)
使用变量$loop documentation($ loop-> even laravel 5.8或($ loop-> iteration%2)laravel <5.8)
@foreach ($users as $user)
@if ($loop->even)
This is even.
@else
@endif
@endforeach
或
@foreach ($listObject as $Object)
<tr class="{{ ($loop->iteration % 2) ? 'odd' : 'even' }}">
@endforeach
答案 6 :(得分:-1)
尝试从foreach循环中获取密钥并运行($ key%2) 基本上奇数数字mod 2总是有一个余数
@foreach ($rows as $key => $row)
<div class="@if ($key > 0 && $row % 2) odd @else even @endif">
</div>
@endforeach