次のコードについて考えてみます。
<?php foreach($payment_type->revisionHistory as $history): ?>
<tr>
<td><?= $history->userResponsible()->alias ?></td>
<td><?= $history->fieldName() ?></td>
<td><?= $history->oldValue() ?></td>
<td><?= $history->newValue() ?></td>
<td><?= $history->created_at ?></td>
</tr>
<?php endforeach; ?>
created_at
フィールドの値が前の反復と異なる場合にのみ行の色を変更し、値が同じである間は同じ行の色を維持できるようにしたいと思います。
私はいくつかの方法を試しましたが、結果は本当に悲惨でした。
あなたはそれをcssと混ぜるべきです、しかしそれは非常に簡単です:
<?php
$lastCreatedAt = ''; // for storing value of the previous date, start with empty
$classNameEven = TRUE; // begin with "even" class
foreach($payment_type->revisionHistory as $history):
$switchClass = ($lastCreatedAt != $history->created_at);
if ($switchClass) {
$classNameEven = !$classNameEven;
}
?>
<tr class="<?php echo ($classNameEven ? 'even' : 'odd'); ?>">
<td><?= $history->userResponsible()->alias ?></td>
<td><?= $history->fieldName() ?></td>
<td><?= $history->oldValue() ?></td>
<td><?= $history->newValue() ?></td>
<td><?= $history->created_at ?></td>
</tr>
<?php //remember the last date for next iteration
$lastCreatedAt = $history->created_at;
?>
<?php endforeach; ?>
次に、次のように、このテーブルにCSSを追加する必要があります。
<style>
table tr.even td {
background-color: #FFF;
}
table tr.odd td {
background-color: #999; //odd rows have darker background
}
</style>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加