【问题标题】:Laravel create a dynamic schedule tableLaravel 创建动态调度表
【发布时间】: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>

这是因为&lt;td&gt;&lt;/td&gt; 不会在if 条件不满足时创建。但是当我尝试将if 移动到td 内时,表格看起来更糟糕

【问题讨论】:

  • 如果条件不满足,您是否尝试过创建空的 td id?
  • 已经试过了,而且桌子看起来更奇怪了。在添加空 &lt;td&gt; Imgur 之前。添加空后&lt;td&gt;Imgur
  • 您总是需要添加所有 td,只需根据条件填写需要显示数据的那些 td
  • 改用 for 循环,创建所有 td,并在每个 td 中使用 if,因此如果满足条件,则获取数据;如果不满足,则保持为空并保持表单正确对齐。

标签: php html mysql laravel


【解决方案1】:

如果当天没有时间,你需要输出一个空的&lt;td&gt;&lt;/td&gt;

<?php
    $days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];   
?>

@foreach($schedule->where('hospital_id', $hos->id) as $sch)
    @foreach($days as $day)
      @if($sch->day == $day)
         <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>
      @else
         <td></td>
      @endif
    @endforeach
@endforeach

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 2018-02-05
    • 2020-05-14
    • 2015-07-17
    • 2016-12-12
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多