You would have to send the template AFTER rendering it first.
May 13, 2018
6
Level 3
How to load view blade upon Ajax Success?
$(document).ready(function(e) {
$("#search").keyup(function() {
$query = $(this).val();
$.ajax({
type: 'GET',
url: 'dashboard',
data: {'q': $query},
dataType: 'json',
success:function(data) {
$.each(data.data, function(key, value) {
$('#ajaxResults').html('
// is there a way to load an existing view blade here?
');
});
}
});
});
});
What is the best practice to load a view blade.php upon Ajax success. A view with simple table are fine but what if the table includes @foreach, an PATCH / DELETE form?
<table class="table">
<thead>
<tr>
<th>Status</th>
<th>Name</th>
<th>Email</th>
<th>Joined</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td data-label="Status">
{{ Form::open(['method' => 'PATCH', 'action' => ['AdminController@updateStatus', $user->id]]) }}
<button type="submit" class="btn is-submit">
@if ($user->status == '0')
<i class="fas fa-toggle-off fa-2 is-red"></i>
@else
<i class="fas fa-toggle-on fa-2 is-green"></i>
@endif
</button>
{{ Form::close() }}
</td>
<td data-label="Name">
{{ $user->name }}
</td>
<td data-label="Email">
{{ $user->email }}
</td>
</td>
<td data-label="Joined" class="rtl">
{{ $user->created_at->diffForHumans() }}
</td>
</tr>
@endforeach
</tbody>
</table>
Any advise is appreciated! Thanks!
Level 67
something like
return response()->json([
'html' => view('your-table-view', compact('users'))->render()
]);
and then
success:function(data) {
$('#ajaxResults').html(data.html);
}
You only want the view to contain your table like you are showing, not an entire page like something extending a master template.
4 likes
Please or to participate in this conversation.