首页 > 解决方案 > 如何根据值在 HTML 单元格中添加类

问题描述

我有以下循环,我从查询中提取值并用它写一个表,在字段中我需要根据自发货日期和今天过去的天数更改 's 类

我已经在 CSS like[data-age >180] 中尝试过,但我只得到错误,而且我找不到与我的需求相似的案例,另外我喜欢在循环中而不是额外的代码段中执行它,因为它扰乱了我的视野

@foreach($shipments as $shipment)
  <tr>
    <td data-title="id">{{$shipment->id}}</td>
    <td class=tracking data-title="tracking">{{$shipment->tracking}}</td>
    <td data-title="PartNumber">{{$shipment->PartNumber}}</td>
    <td data-title="DateShipped">{{$shipment->DateShipped}}</td>
    <td 
      data-title="age" 
      data-age="{{
        (int) floor(
          (time() - strtotime($shipment->DateShipped)) 
          / (60 * 60 * 24)
        )
      }}"
    >{{
      (int) floor(
        (time() - strtotime($shipment->DateShipped)) / (60 * 60 * 24)
      )
    }} days
    </td>

    <td data-title="Qtyorder">{{$shipment->Qtyorder}}</td>
    <td data-title="QtyShipped">{{$shipment->QtyShipped}}</td>
    <td data-title="QtyBckorder">{{$shipment->QtyBckorder}}</td>
    <td data-title="purchaseOrder">{{$shipment->purchaseOrder}}</td>
    <td data-title="OrderNumber">{{$shipment->OrderNumber}}</td>
    <td data-title="paperwork">{{$shipment->paperwork}}</td>
    <td data-title="VMIreceived">{{$shipment->VMIreceived}}</td>
    <td data-title="VMIticketNumber">{{$shipment->VMIticketNumber}}</td>
    <td data-title="VMILRB">{{$shipment->VMILRB}}</td>

    <td>
      <a 
        href="{{ route('shipment.edit',$shipment->id)}}" 
        class="btn btn-primary"
      >
        Edit
      </a>
    </td>
    <td>
      <form action="{{ route('shipment.destroy', $shipment->id)}}" method="post">
        @csrf
        @method('DELETE')
        <button class="btn btn-danger" type="submit">Delete</button>
      </form>
    </td>
  </tr>
@endforeach

标签: phphtmllaravellaravel-blade

解决方案


尝试单行 if 语句

{{ $condition ? value if true : value if false }}

推荐阅读