我创建了这个表(让我们说老师)可以通过选中复选框元素('是'或'否')来检查出席情况。并且页面不应该刷新,必须保持复选框('是'或'否',其中一个在框中为空或勾选)所以我编写了代码(逻辑上),但它不工作。你能帮助我解决问题吗? 提前谢谢
这是我的 visit.blade.php
>@extends('header')
>@section('content')
> ...
> <div>
> ...
> <tbody class="compact ">
> @forelse ($group->students as $key => $student)
> <tr class="center aligned">
> <td>{{ ++$key }}</td>
> <td class="left aligned">{{ $student->fio }}</td>
> @for($k = 1; $k<=$group->number_of_lessons; $k++)
> <td>
> @if($student->has)
> <div class="ui checkbox visit"><input type="checkbox" name="example" data-group-id="{{
> $group->id }}" data-student-id="{{ $student->id }}"
> data-lesson-id="{{$lesson->id}}">
> </div>
> @else
> <div class="ui checkbox visit"><input type="checkbox" name="example" data-group-id="{{
> $group->id }}" data-student-id="{{ $student->id }}"
> data-lesson-id="{{$lesson->id}}">
> </div>
> @endif
>
>
> </td>
> @endfor
> </tr>
> @empty
> <tr class="center aligned">
> <td></td>
> </tr>
> @endforelse
> </tbody>
> </div>
> @endsection
这是我的 app.js
> $('.button.add.lesson.attandence').click(function(){
> var lesson_id = $(this).data('lesson-id');
> var student_id = $(this).data('student-id');
> var group_id = $(this).data('group-id');
> var $addButton =($this);
> $.ajax({
> url:laroute.route('group.visits',{id:group_id}),
> data:'student-id=' + student_id,
> data:'lesson-id=' + lesson_id,
> success:function(){
> $addButton.parent().parent().add();
> alertify.success('Attendance checked');
> }
> });
>
> });
这是我的 Controller.php
> public function visits($id){
> $group = Group::findOrFail($id);
> foreach(Student::all() as $student) {
> $data['students'][$student->id] = $student->fio;
> }
> return view('groups.visit', compact('group', 'data'));
>
> }
这是我的 Routes.php
Route::get('group/{id}/visits', ['as' => 'group.visits', 'uses' => 'GroupController@visits']);
Route::post('group/{id}/visits', ['as' => 'group.visits', 'uses' => 'GroupController@visits']);
以下是tableview TABLE IMAGE 的链接 https://scontent-frt3-1.xx.fbcdn.net/v/t31.0-8/fr/cp0/e15/q65/16992175_779940032172228_6157498598844322627_o.jpg?efg=eyJpIjoidCJ9&oh=5d746dd6dd90e12ed5e379b9ad02f0b4&oe=5935D05C
答案 0 :(得分:1)
一个月前我问过qustion。自己试了一下,最后我设法做到了这一点。所以我发布了答案。也许有人将来会需要这个。 我有访问表。所以我在Controller.php中添加了更多行
> public function addStudent($id, Request $request){
> $group = Group::findOrFail($id);
> $group->students()->sync([$request->student_id],$detaching = false);
> $lessons = Lesson::where('group_id', $id)->get();
>
> foreach ($lessons as $k => $lesson) {
> $visit = new Visit();
> $visit->group_id = $id;
> $visit->lesson_id = $lesson->id;
> $visit->student_id = $request->student_id;
> $visit->has = false;
> $visit->save();
> }
>
> return back();
> }
然后我在 visit.blade.php 中删除旧的@if
和@andif
,然后我添加了
> @foreach($student->visits as $k => $visit)
> <td>
> <div class="ui {{ $visit->has ? 'checked':'' }} checkbox visit">
> <input type="checkbox" data-group-id="{{ $group->id }}" data-visit-id="{{ $visit->id }}" {{
> $visit->has ? 'checked':'' }}>
> </div>
> </td>
> @endforeach
我最后在 app.js
上写了这个脚本> $('.checkbox.visit').checkbox({
> onChange: function() {
> var group_id = $(this).data('group-id');
> var visit_id = $(this).data('visit-id');
> var has = ($(this).parent().checkbox('is checked') ? 1:0);
> $.ajax({
> url: laroute.route('group.visits', { id : group_id }),
> data:
> 'visit_id=' + visit_id+
> '&has=' + has,
> success: function(result) {
> if(result.success) {
> alertify.success(result.success);
> }
> }
> });
> }
> });
I DID IT;)