首页 > 解决方案 > 为 laravel 表应用备用行颜色 CSS

问题描述

如何在 laravel 表中应用备用行颜色 CSS。我想为表格行显示替代颜色。我在表格中保留了gridview cssclass。我想申请.gridview tr.even td偶数行数,即2的倍数的行数。

我的 CSS 文件

.gridview {
    font-family: "arial";
    background-color: #FFFFFF;
    width: 100%;
    font-size: small;
}

    .gridview th {
        background: #0CA3D2;
        padding: 5px;
        font-size: small;
    }

    .gridview td {
        background: #B6E1EE;
        color: #333333;
        font: small "arial";
        padding: 4px;
    }

    .gridview tr.even td {
        background: #FFFFFF;
    }

表代码

<table id="showBooksIn" class="table table-bordered gridview">
            <thead>
                <tr>
                    <th>BOOK ID</th>
                    <th>BILLED DATE</th>                      
                </tr>
            </thead> 
            <tbody>
                @foreach($ordered_books as $data)
                 <tr>
                     <td> {{$data['BookID']}} </td>                         
                     <td> {{$data['BilledDate']}} </td>
                 </tr>
                @endforeach
          </tbody>          
        </table>

标签: phpcsslaravellaravel-5

解决方案


@RamRaider 有使用 css 的最佳建议。另一种方法是使用模运算符:

@foreach($ordered_books as $i => $data) 
    @php $class = $i ÷ 2 === 0 ? 'even' : 'odd'; @endphp
    <tr class="{{ $class }}"> 
        <td> {{$data['BookID']}} </td>
        <td> {{$data['BilledDate']}} </td> 
    </tr> 
@endforeach

推荐阅读