在刀片中将桌子td水平对齐

时间:2018-07-23 04:21:19

标签: html5 laravel html-table laravel-blade

我现在认为我要上学,有没有办法我可以水平对齐天数和状态,而不必重复学生姓名:

enter image description here

这是我的刀片视图:

<table class="table table-hover table-bordered">
    <tr>
        <th>Student</th>
        <th>Days</th>
        <th>Status</th>
    </tr>
    @foreach($atnd as $atnds)
    <tr>
        <td>{{$atnds->lead->student_name}}</td>
        <td>{{$atnds->days}}</td>
        <td>{{$atnds->status}}</td>
    </tr>
    @endforeach
</table>

这是我的控制人

public function get_attendance(){
    $atnd = Attendance::with(['level','lead'])->where(['level_id'=> 11])->get();
    return view('attendance.index',compact('atnd'));
}

1 个答案:

答案 0 :(得分:1)

您可以做的是在->get()之后按Student_id分组,它将为您提供收藏集,然后在标题中显示日期和学生姓名(假设所有学生的日期都相同)

在控制器中

$atnds=Attendance::with(['level','lead'])->where(['level_id'=> 11])->get()->groupBy('student_id');

可见

<table class="table table-hover table-bordered">
    <tr>
        <th>Student</th>
        @foreach($atnds->first() as $row)
            <th>{{$row->days}}</th> //assuming you have dates same for all student
        @endforeach
    </tr>
    @foreach($atnds as $group)
        <tr>
            <td>{{ $group->first()->lead->student_name}}</td>
            @foreach($group as $row)
                <td>{{$row->status}}</td>
            @endforeach
        </tr>
    @endforeach
</table>

修改

对于日期过滤器,您可以添加->whereMonth('days', '07')条件

要显示状态下拉菜单,您可以像这样添加它(替换$ row-> status)

<table class="table table-hover table-bordered">
    @if($atnds->count() > 0){
        <tr>
            <th>Student</th>
            @foreach($atnds->first() as $row)
                <th>{{$row->days}}</th>
            @endforeach
        </tr>
        @foreach($atnds as $studentId => $group)
            <tr>
                <td>
                   {{ $group->first()->lead->student_name}} 
                </td>
                @foreach($group as $index => $row)
                    <td>
                        <input type="hidden" name="student[{{ $studentId }}][{{ $index }}][day]" value = "{{ $row->days }}" />
                        <select name="student[{{ $studentId }}][{{ $index }}][status]">
                            <option {{ $row->status == 'P' ? 'selected' : '' }}  value = "P">P</option>
                            <option {{ $row->status == 'L' ? 'selected' : '' }}  value = "L">L</option>
                        </select>
                    </td>
                @endforeach
            </tr>
        @endforeach
    @else
        <tr><td>No Record</td></tr>
    @endif
</table>