我正在通过ajax吸引用户。现在,我正在尝试以模式编辑用户。如果有人单击编辑按钮,则将打开一个模态。我已经做到了,并且也让用户进入了模态,但这给了我某种类型的随机用户。并且它还编辑某种类型的随机用户。下面是我的代码。可以在其中使用表格tbody的javascript,您可以看到编辑按钮和对模式的引用。
// Javascript
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
});
$('select[name="class_id"]').on('change', function() {
var classID = $(this).val();
if(classID) {
$.ajax({
url: '/attendance/ajax/'+classID,
type: "GET",
dataType: "json",
success:function(data) {
var markup = '';
markup += '<tr class="filters"><th style="width: 2%" class="align-middle text-center"><input type="checkbox" id="options"></th><th style="width: 2%" class="align-middle text-center">#</th> <th style="width: 15%" class="text-center">Student ID<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Student Name<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Attendance<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Date<input type="text" class="form-control" disabled></th> <th style="width: 15%;" class="align-middle text-center">Actions</th> <tr>';
$.each(data, function(key, value) {
markup += '<tr class="filters"> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'+value.id+'"></td> <td><input type="hidden" value="'+value.id+'" name="id[]">' + value.id + '</td> <td><input type="hidden" value="'+value.student_id+'" name="student_id[]">' + value.student_id + '</td> <td><input type="hidden" value="'+value.first_name+'" name="first_name[]"><input type="hidden" value="'+value.last_name+'" name="last_name[]">' + value.first_name+ ' ' + value.last_name + '<td><input type="hidden" value="'+value.attendance+'" name="attendance[]">' + value.attendance + '</td>' + '<td><input type="hidden" value="'+value.created_at+'" name="created_at[]">' + value.created_at + '</td>' + '<td style=" width=12%" class="text-center"> <a data-toggle="modal" data-target="#editAttendanceModal"'+value.id+'""><button title="Edit" class="btn btn-outline-primary"><span class="fas fa-pencil-alt"></span></button></a> </td>' + '</td> <tr>';
});
$('table[id="studentsData"]').html(markup);
}
});
}
});
//模态
@foreach($attendances as $attendance)
<!--EDIT CLASS MODAL-->
<div class="modal fade" id="editAttendanceModal"{{$attendance->id}}>
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header header-backgroud text-white">
<h5 class="modal-title">Class View</h5>
<button class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="container">
{!! Form::model($attendance, ['method'=>'PATCH', 'action'=>['AttendanceController@update', $attendance->id]]) !!}
<!--CLASS DETAIL-->
<div class="row mt-2">
<div class="col-md-12">
<h3 class="display-4 text-center">Edit Attendance</h3>
<div class="form-group">
<label for="attendance">Attendance</label>
{{Form::select('attendance',['present' => 'Present', 'absent' => 'Absent', 'leave'=>'Leave'], null,['class' => 'form-control'])}}
</div>
{!! Form::button(' Save Changes', ['type'=>'submit', 'class'=>'btn btn-outline-danger btn-block mb-3']) !!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@endforeach
//控制器
public function update(Request $request, $id)
{
$attendance = StudentsAttendance::findOrFail($id);
$attendance->update($request->all());
return redirect()->back();
}