简体   繁体   中英

applying alternate row color CSS for laravel table

How can I apply alternate row color CSS in laravel table. i want to display alternate color for the table rows. I've kept gridview cssclass in the table. I want to apply .gridview tr.even td for the even row count ie the rows with multiple of 2.

My CSS file

.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 code

<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>

@RamRaider has the best suggestion of using css. Another way is using the modulus operator:

@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

If you just want to play with css:

tr:nth-child(even) td {
   background: #FFFFFF;
}

For odd tr:

tr:nth-child(odd) td {
   background: grey;
}
<table id="showBooksIn" class="table table-bordered gridview" style="color:{{ !is_null(user()) ? 'red' : 'green' }}">
        <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>

It is example with php, but i think that the best way to do this with js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM