我在ajax
应用中使用Crud
进行laravel
次操作。
但是我面临两个问题,我已经尝试了很多堆栈溢出的解决方案,但都没有。
问题:
1。关于添加/删除/更新操作无法查看更改 刷新,我刷新页面以查看更改。 (然后没有Ajax的好处)
2. 关于成功数据 重定向网址也不起作用。
路线:
//franchisors crud controller
Route::prefix('admin/franchisors')->group(function(){
Route::get('/', 'AdminCrudController@index');
Route::match(['get', 'post'], 'create', 'AdminCrudController@create');
Route::match(['get', 'put'], 'update/{id}', 'AdminCrudController@update');
Route::delete('delete/{id}', 'AdminCrudController@destroy');
});
控制器:
public function create(Request $request)
{
if ($request->isMethod('get'))
return view('admin.add_franchisor');
else {
// $rules = [
// 'name' => 'required',
// 'email' => 'required|email',
// ];
// $validator = Validator::make($request->all(), $rules);
// if ($validator->fails())
// return response()->json([
// 'fail' => true,
// 'errors' => $validator->errors()
// ]);
$franchisor = new Franchisors();
$franchisor->name = $request->name;
$franchisor->email = $request->email;
$franchisor->password = $request->password;
$franchisor->cnic = $request->cnic;
$franchisor->address = $request->address;
$franchisor->mobile = $request->mobile;
$franchisor->shop = $request->shop;
$franchisor->dist= $request->dist;
$franchisor->coins = $request->coins;
// $amount = $request->coins;
$franchisor->save();
$request->session()->flash('alert-success', 'Franchisor was successful added!');
$franchisorID = $franchisor->id;
$id = CoinRate::latest()->first()->id;
$rate = CoinRate::latest()->first()->rate;
$coins = new Coin();
$coins->coinrate_id = $id;
$coins->amount = $request->coins;
$coins->total = $request->coins * $rate;
$coins->type = 2;
$coins->franchisor_id = $franchisorID;
$coins->save();
return redirect('admin/coins');
// return \response()->json([
// 'fail' => false,
// 'redirect_url' => url('admin/franchisors')
// ]);
}
}
public function update(Request $request, $id)
{
//
if ($request->isMethod('get'))
return view('admin.add_franchisor', ['franchisors' => Franchisors::find($id)]);
else {
// $rules = [
// 'name' => 'required',
// 'email' => 'required|email',
// ];
// $validator = Validator::make($request->all(), $rules);
// if ($validator->fails())
// return response()->json([
// 'fail' => true,
// 'errors' => $validator->errors()
// ]);
$Franchisor = Franchisors::find($id);
$Franchisor->coins = $request->coins;
$Franchisor->name = $request->name;
$Franchisor->address = $request->address;
$Franchisor->cnic = $request->cnic;
$Franchisor->shop = $request->shop;
$Franchisor->email = $request->email;
$Franchisor->password = $request->password;
$Franchisor->mobile = $request->mobile;
$Franchisor->dist = $request->dist;
$Franchisor->save();
return response()->json([
'fail' => false,
'redirect_url' => url('/admin/franchisors')
]);
// return redirect('');
// return Redirect::to('admin');
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id,Request $request)
{
Franchisors::destroy($id);
$request->session()->flash('alert-success', 'User was successful added!');
return redirect('admin');
}
查看:
<div class="modal-header">
<h5 class="modal-title">{{isset($franchisors)?'Edit':'New'}} Franchisor</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row required">
@if(isset($franchisors))
<form class="form-horizontal" id="frm" novalidate method="POST" action="{{ url('admin/franchisors/update/'.$franchisors->id) }}">
@else
<form class="form-horizontal" id="frm" novalidate method="POST" action="{{ url('admin/franchisors/create') }}">
@endif
{{ csrf_field() }}
@if(isset($franchisors))
{{ method_field('PUT') }}
@endif
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10 col-md-8">
<input type="text" class="form-control" value="{{isset($franchisors)?$franchisors->name:''}}" name="name" id="name" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10 col-md-8">
<input type="email" class="form-control" value="{{isset($franchisors)?$franchisors->email:''}}" name="email" id="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="cnic" class="col-sm-2 control-label">CNIC</label>
<div class="col-sm-10 col-md-8">
<input type="text" class="form-control" value="{{isset($franchisors)?$franchisors->cnic:''}}" id="cnic" name="cnic" placeholder="CNIC">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10 col-md-8">
<input type="password" class="form-control" value="{{isset($franchisors)?$franchisors->password:''}}" name="password" id="inputPassword3" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="Address" class="col-sm-2 control-label">Address</label>
<div class="col-sm-10 col-md-8">
<input type="text" name="address" class="form-control" value="{{isset($franchisors)?$franchisors->address:''}}" id="address" placeholder="Address">
</div>
</div>
<div class="form-group">
<label for="Mobile" class="col-sm-2 control-label">Mobile</label>
<div class="col-sm-10 col-md-8">
<input type="text" class="form-control" name="mobile" value="{{isset($franchisors)?$franchisors->mobile:''}}" id="Mobile" placeholder="Mobile">
</div>
</div>
<div class="form-group">
<label for="Shop" class="col-sm-2 control-label">Shop</label>
<div class="col-sm-10 col-md-8">
<input type="text" class="form-control" id="Shop" value="{{isset($franchisors)?$franchisors->shop:''}}" name="shop" placeholder="Shop">
</div>
</div>
<div class="form-group">
<label for="dist" class="col-sm-2 control-label">Dist.</label>
<div class="col-sm-10 col-md-8">
<input type="text" class="form-control" name="dist" value="{{isset($franchisors)?$franchisors->dist:''}}" id="dist" placeholder="District">
</div>
</div>
<div class="form-group">
<label for="Coins" class="col-sm-2 control-label">Coins</label>
<div class="col-sm-10 col-md-8">
<input type="text" class="form-control" id="Coins" value="{{isset($franchisors)?$franchisors->coins:''}}" name="coins" placeholder="Coins">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal"> Close</button>
{{--<button type="submit" class="btn btn-primary"> Save </button>--}}
{{--{!! Form::submit("Save",["class"=>"btn btn-primary"])!!}--}}
</div> <div class="modal fade" id="modalForm" tabindex="-1" role="dialog" data-backdrop="static">
<div class="modal-dialog" role="document">
<div class="modal-content" id="modal_content"></div>
</div>
</div>
<div class="modal fade" id="modalDelete" tabindex="-1" role="dialog" data-backdrop="static">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure want to delete?</p>
<input type="hidden" id="delete_token"/>
<input type="hidden" id="delete_id"/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger"
onclick="ajaxDelete('{{url('admin/franchisors/delete')}}/'+$('#delete_id').val(),$('#delete_token').val())">
Delete
</button>
</div>
</div>
</div>
</div>
<div class="pull-right">
<a href="#modalForm" data-toggle="modal" data-href="{{url('admin/franchisors/create')}}"
class="btn btn-primary">Add Franchisor</a>
</div>
<br>
<br>
<div class="row">
<div class="col-md-12">
<!-- Advanced Tables -->
<div class="panel panel-default">
<div class="panel-heading">
Franchisors
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>No</th>
<th>
Name
</th>
<th>
Email
</th>
<th>
Shop
</th>
<th>
Coins
</th>
<th width="130px" style="vertical-align: middle">Action</th>
</tr>
</thead>
<tbody>
@php
$i=1;
@endphp
@foreach($franchisors as $franchisor)
<tr>
<th>{{$i++}}</th>
<td>{{ $franchisor->name }}</td>
<td>{{ $franchisor->email }}</td>
<td>{{ $franchisor->shop}}</td>
<td>{{ $franchisor->coins }}</td>
<td>
<a class="btn btn-primary btn-sm" title="Edit" href="#modalForm" data-toggle="modal"
data-href="{{url('admin/franchisors/update/'.$franchisor->id)}}">
Edit</a>
<input type="hidden" name="_method" value="delete"/>
<a class="btn btn-danger btn-sm" title="Delete" data-toggle="modal"
href="#modalDelete"
data-id="{{$franchisor->id}}"
data-token="{{csrf_token()}}">
Delete
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
<!--End Advanced Tables -->
</div>
</div>
的Ajax / JS / Jquery的:
$(document).on('click', 'a.page-link', function (event) {
event.preventDefault();
ajaxLoad($(this).attr('href'));
});
$(document).on('submit', 'form#frm', function (event) {
event.preventDefault();
var form = $(this);
var data = new FormData($(this)[0]);
var url = form.attr("action");
$.ajax({
type: form.attr('method'),
url: url,
data: data,
cache: false,
contentType: false,
processData: false,
success: function (data) {
$('.is-invalid').removeClass('is-invalid');
if (data.fail) {
for (control in data.errors) {
$('input[name=' + control + ']').addClass('is-invalid');
$('#error-' + control).html(data.errors[control]);
}
} else {
$('#modalForm').modal('hide');
//window.location.href = data.redirect_url;
ajaxLoad(data.redirect_url);
}
},
error: function (xhr, textStatus, errorThrown) {
alert("Error: " + errorThrown);
}
});
return false;
});
function ajaxLoad(filename, content) {
content = typeof content !== 'undefined' ? content : 'content';
$('.loading').show();
$.ajax({
type: "GET",
url: filename,
contentType: false,
success: function (data) {
$("#" + content).html(data);
$('.loading').hide();
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
function ajaxDelete(filename, token, content) {
content = typeof content !== 'undefined' ? content : 'content';
$('.loading').show();
$.ajax({
type: 'POST',
data: {_method: 'DELETE', _token: token},
url: filename,
success: function (data) {
$('#modalDelete').modal('hide');
$("#" + content).html(data);
$('.loading').hide();
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
$('#modalForm').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
ajaxLoad(button.data('href'), 'modal_content');
});
$('#modalDelete').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
$('#delete_id').val(button.data('id'));
$('#delete_token').val(button.data('token'));
});
$('#modalForm').on('shown.bs.modal', function () {
$('#focus').trigger('focus')
});
非常感谢任何帮助! 在此先感谢!!