在使用 ajax 使用 DataTables 和 ajax 更新记录时,我遇到了 Laravel 中某些按钮的问题。
这是刀片文件中的脚本
<script>
$(function() {
var table=$('#roll-table').DataTable({
autoWidth: false,
processing: true,
serverSide: true,
stateSave: true,
ajax: '{{ route('getCurrentRoll') }}',
columns: [
{ data: 'action', name: 'action', orderable: false, searchable: false, "width": "25%"},
{ data: 'member.membership_number'},
{ data: 'member.first_name'},
{ data: 'member.last_name'},
{ data: 'rollstatus.status'},
{ data: 'account', render: $.fn.dataTable.render.number(',', '.', 2, '$')},
],
});
})
function updateStatus(id, status){
$.ajax({
url: '{{ route('updateStatus') }}',
type: 'POST',
data: {id: id, status: status},
datatype: 'JSON',
success: function(response){
if(response.success){
$('#roll-table').DataTable().ajax.reload();
toastr.success(response.message);
}
else{
toastr.error(response.message);
}
}
});
}
</script>
这是控制器
public function rollstatus(Request $request)
{
$r = Roll::find($request->id);
$points = Pointsmaster::where('Reason', '=', 'Attendance')->value('Value');
$member = Roll::where('id', '=', $request->id)->value('member_id');
$year = Carbon::parse(now())->year;
$rollid = RollMapping::latest()->value('id');
$status = $request->status;
switch ($status) {
// Define variables for member paying using account and check balance
case 'V':
// Check Account Balance and back out if account balance is too low
if($r->member->Accounts->sum('amount') < 10)
{
Alert()->error("Error", "Insufficient Account Balance")->autoclose(1500);
return redirect(action('RollController@index'));
}
$paid = 'Y';
$title = 'Member Present';
$message = 'Member paid using account balance';
// Add Voucher use record
$voucher = new Accounts();
$voucher->member_id = $r->member_id;
$voucher->Reason = 'Weekly Subs';
$voucher->amount = -10;
$voucher->user = Auth::user()->username;
$voucher->save();
break;
// Define variables for member who didn't pay
case 'P':
$paid = 'N';
$title = "Member Present";
$message = "Member has not paid";
break;
// Define variables for member who is online
case 'O':
$paid = 'N';
$title = "Member Online";
$message = "Member marked as present online";
break;
// Define variables for member who paid cash
case 'C':
$paid = "Y";
$title = "Member Present";
$message = "Member paid by Cash";
break;
default:
Alert()->error("Error", "System Error has occured")->autoclose(1500);
return redirect(action('RollController@index'));
}
$r->status = $status;
if($paid != 'N')
{
$r->paidrollid = $rollid;
}
$r->save();
if (config('global.Squadron_Points') != 'N')
{
$p=new Points();
$p->member_id = $member;
$p->value = $points;
$p->year = $year;
$p->reason = "Squadron Night Attendance";
$p->save();
}
alert()->success($title, $message)->autoclose(1500);
return response()->json($request->id);
}
public function getCurrentRoll(Request $request)
{
$rollid = RollMapping::latest()->value('id');
$roll = Roll::with(array('member' => function($q) {
return $q->orderby('rank');
}))
->where('roll_id', '=', $rollid)->orderby('status')
->with ('rollstatus')
->get();
return DataTables::of($roll)
->addColumn('account', function($roll) {
return $roll->member->Accounts->sum('amount');
})
->addColumn('action', function($row){
$btn = '<a href="'.action('MembersController@show', $row->member_id).'" target="_blank" title="View" class="btn btn-round btn-info"><i class="fa fa-info"></i></a>';
if ($row->status == 'A')
{
$btn .= '<a href="javascript:void(0)" data-toggle="tooltip" title="Paid Cash" class="btn btn-round btn-success" onClick="updateStatus('.$row->id.', "C")><i class="fa fa-check"></i></a>';
$btn .= '<a href="'.action('RollController@rollstatus', [$row->id, 'P']).'" class="btn btn-round btn-danger" title="Not Paid"><i class="fa fa-times"></i></a>';
$btn .= '<a href="'.action('RollController@rollstatus', [$row->id, 'V']).'" class="btn btn-round btn-warning" title="Account Payment"><i class="fa fa-money"></i></a>';
}
return $btn;
})
->make(true);
}
public function index_test()
{
//
$rollid = RollMapping::latest()->value('id');
$rolldate = RollMapping::latest()->value('roll_date');
$members = Roll::with(array('member' => function($q) {
return $q->orderby('rank');
}))
->where('roll_id', '=', $rollid)->orderby('status')->get();
$online = Settings::where('setting', 'Online Meetings')->value('value');
return view('roll.index_test', compact('members', 'rolldate', 'rollid', 'online'));
}
rollstatus 是我希望在更新记录时调用的函数 index_text 加载视图 getCurrentRoll 构建 DataTable
- 请注意,在检查状态后,我只更新了第一个按钮
if ($row->status == 'A')
{
$btn .= '<a href="javascript:void(0)" data-toggle="tooltip" title="Paid Cash" class="btn btn-round btn-success" onClick="updateStatus('.$row->id.', "C")><i class="fa fa-check"></i></a>';
一旦这个工作正常,我可以更新其他人。
我面临的问题是,当我单击“已付现金”按钮时,我的
Uncaught SyntaxError: Unexpected end of input
控制台中出现错误
我想要做的是将行 ID 和“P”用于支付到控制器中我的路线有以下内容
Route::post('post/roll/updateStatus', 'RollController@rollstatus')->name('updateStatus');
我想知道我哪里出错了,如果我可以在不刷新整个表的情况下更新记录(所以如果我有一个搜索过的过滤器,这仍然存在,但记录已更新)
谢谢