我正在通过Ajax从数据库中获取一些用户数据。我在表中添加了一些复选框以删除数据。我为此编写了代码,但我不知道如何将用户ID从Ajax发送到控制器,如果有人能帮助我会很好。下面是我的代码
// Javascript
$(document).ready(function() {
$(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><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> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]"></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><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);
}
});
}
});
});
//提交按钮以删除
<form method="post" action="delete/attendance" class="form-inline">
{{csrf_field()}}
{{method_field('delete')}}
<div class="form-group">
<select name="checkBoxArray" id="" class="form-control">
<option value="">Delete</option>
</select>
</div>
<div class="form-group">
<input type="submit" name="delete_all" class="btn btn-primary">
</div>
</form>
//路线
Route::delete('/students/delete/attendance', 'AttendanceController@deleteAttendance');
//控制器
public function deleteAttendance(Request $request, $id) {
if ($request->delete_single) {
$this->destroy($id);
}
if(isset($request->delete_all) && !empty($request->checkBoxArray)) {
$attendances = StudentsAttendance::findOrFail($request->checkBoxArray);
foreach($attendances as $attendance) {
$attendance->delete();
}
return redirect()->back();
} else {
return redirect()->back();
}
}
答案 0 :(得分:1)
由于您要向操作发送一组ID,因此请首先创建POST路由:
id
然后,您必须将<input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'+value.id+'">
附加到每个复选框上,例如:
<form method="post" action="/students/delete/attendance" class="form-inline">
并将您的表单操作更新为:
deleteAttendance
试试看,通过在顶部添加dd()
来检查您是否在public function deleteAttendance(Request $request, $id) {
dd( $request->checkBoxArray );
...
}
动作中获得了所有选定的ID:
let attributesUnstrike = [NSAttributedString.Key.strikethroughStyle : 0]
let titleUnselected = NSAttributedString(string: "YOUR STRING", attributes: attributesUnstrike)
//Set strike for selected state
let attributesStrike = [NSAttributedString.Key.strikethroughStyle : 1]
let titleSelected = NSAttributedString(string: "YOUR STRING", attributes: attributesStrike)
btn.setAttributedTitle(titleUnselected, for: .normal)
btn.setAttributedTitle(titleSelected, for: .selected)
@IBAction func reloadtable(sender:UIButton){
sender.isSelected = !sender.isSelected
//Automatically change Button state automatically based on curent state
}
答案 1 :(得分:0)
您的Ajax网址与您的路线不同。
ajax : /attendance/ajax/{id}
route : /students/delete/attendance
如果我们遵循来自控制器的相同指令,则需要输入ID,因此请将ajax网址更改为:'/students/delete/attendance/' +classID
您的ajax类型错误,将GET
更改为DELETE
(与路由相同)。
那么您的路线不是使用ID,而是您使用传递ID的路线和控制器的传递URL。
Route::delete('/students/delete/attendance/{id}', 'AttendanceController@deleteAttendance');
在您的控制器上执行:
public function deleteAttendance(Request $request, $id) {
$query = StudentsAttendance::findOrFail($id);
$query->delete();
}
如果要删除多个学生,将使用不同的代码。