【发布时间】:2017-03-02 02:08:59
【问题描述】:
我有一个医院表和日程表,我想使用这些表创建一个日程表 html
+----+--------------+
| id | name |
+----+--------------+
| 1 | hospital a |
| 2 | hospital b |
+----+--------------+
+-----+---------+--------+---------------+
| day | start | end | hospital_id |
+-----+---------+--------+---------------+
| mon | 10:00 | 14:00 | 1 |
| tue | 15:00 | 20:00 | 1 |
| wed | 14:00 | 19:00 | 2 |
| fri | 10:00 | 15:00 | 2 |
+-----+---------+--------+---------------+
问题是计划时间与正确的<th>不相符
我需要什么:
+-------+------+--------+-------+-------+-------+------+
| mon | tue | wed | thu | fri | sat | sun |
+------------------------------------------------------+
| hospital a |
+-------+-------+-------+-------+-------+-------+------+
| 10:00 | 15:00 | | | | | |
| - | - | | | | | |
| 14:00 | 20:00 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
| hospital b |
+-------+-------+-------+-------+-------+-------+------+
| | | 14:00 | | 10:00 | | |
| | | - | | - | | |
| | | 19:00 | | 15:00 | | |
+-------+-------+-------+-------+-------+-------+------+
但是变成了这个样子
+-------+-------+-------+-------+-------+-------+------+
| mon | tue | wed | thu | fri | sat | sun |
+------------------------------------------------------+
| hospital a |
+-------+------+--------+-------+-------+-------+------+
| 10:00 | 15:00 | | | | | |
| - | - | | | | | |
| 14:00 | 20:00 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
| hospital b |
+-------+-------+-------+-------+-------+-------+------+
| 14:00 | 10:00 | | | | | |
| - | - | | | | | |
| 19:00 | 15:00 | | | | | |
+-------+-------+-------+-------+-------+-------+------+
这是我的 .blade.php 脚本
<table class="table">
<thead>
<tr>
<td class="text-center">mon</td>
<td class="text-center">tue</td>
<td class="text-center">wed</td>
<td class="text-center">thu</td>
<td class="text-center">fri</td>
<td class="text-center">sat</td>
<td class="text-center">sun</td>
</tr>
</thead>
<tbody>
@foreach($hospital as $hos)
<tr>
<td class="text-center _b" colspan="7">{{$hos->name}}</td>
</tr>
<tr>
@foreach($schedule->where('hospital_id', $hos->id) as $sch)
@if($sch->day == 'mon')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam active">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'tue')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'wed')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'thu')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'fri')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'sat')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam active">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@if($sch->day == 'sun')
<td class="text-center">
<div class="jam">{{Carbon::parse($sch->start_time)->format('H:i')}}</div>
<div class="jam">s/d</div>
<div class="jam">{{Carbon::parse($sch->end_time)->format('H:i')}}</div>
</td>
@endif
@endforeach
</tr>
@endforeach
</tbody>
</table>
</table>
这是因为<td></td> 不会在if 条件不满足时创建。但是当我尝试将if 移动到td 内时,表格看起来更糟糕
【问题讨论】:
-
如果条件不满足,您是否尝试过创建空的 td id?
-
您总是需要添加所有 td,只需根据条件填写需要显示数据的那些 td
-
改用 for 循环,创建所有 td,并在每个 td 中使用 if,因此如果满足条件,则获取数据;如果不满足,则保持为空并保持表单正确对齐。