I'm using Laravel 8 and I tried using an Ajax request for showing the results of my table:
$.ajax({
url: "{{url('/admin/users/order-by')}}",
type: "POST",
data: {
order_by: orderBy,
_token: '{{csrf_token()}}'
},
dataType: 'json',
success: function (result) {
var userList = document.getElementById("user-list");
$('#contentDiv').show();
$.each(result.orderby, function (key, value) {
$("#user-list").append('<tr><td>' + value.id + '</td> <td>' + value.name + '</td> <td>' + value.email + '</td> <td>' + value.is_active + '</td><td class="d-flex"><div class="btn-group"></div></td></tr>');
});
}
});
So basically it will append some new rows to the table.
And actually at the last <td class="d-flex"></td>, I wanted to use some buttons which goes like this:
@if(!$user->isSameUser($user->id))
<td class="d-flex">
<div class="btn-group">
<a href="{{ route('admin.users.edit' , ['user' => $user->id]) }}" class="btn btn-sm btn-purple">Accesses</a>
<a href="{{ route('admin.users.edit' , ['user' => $user->id]) }}" class="btn btn-sm btn-info">Money Wallet</a>
<a href="{{ route('admin.users.edit' , ['user' => $user->id]) }}" class="btn btn-sm btn-warning"> View Information</a>
<a href="{{ route('admin.users.edit' , ['user' => $user->id]) }}" class="btn btn-sm btn-navy">Edit Profile</a>
<form id="delForm" action="{{ route('admin.users.destroy' , ['user' => $user->id]) }}" method="POST">
@csrf
@method('DELETE')
<button type="button" class="button btn btn-sm btn-orange ml-1" onclick="this.classList.toggle('button--loading')" id="btn-submit" type="submit" data-name="{{ $user->name }}" >
<span class="button__text">Delete</span>
</button>
</form>
</div>
</td>
@endif
Now in order to show this data in Ajax response, I need to add this:
...<td class="d-flex"><div class="btn-group">__BUTTONS GOES HERS__</div></td></tr>');
But don't know how to concatenate the specified code with Ajax response.
Update 1
I finally could successfully pass the buttons to the table row.
But the only problem here is that it does not read the user id variable in JavaScript:
@push('scripts')
$("#dropdown-list").on('change', function(){
var orderBy = document.getElementById("dropdown-list").value;
$('#contentDiv').hide();
$("#user-list").html('');
if(orderBy == 'asc')
{
var orderBy = 1;
} else if(orderBy == 'desc')
{
var orderBy = 2;
}
const url1 = "{{ route('admin.users.permissions' , ['user' => '__USER_ID__']) }}"
const url2 = "{{ route('admin.users.wallet' , ['user' => '__USER_ID__']) }}"
const url3 = "{{ route('admin.users.info' , ['user' => '__USER_ID__']) }}"
const url4 = "{{ route('admin.users.edit' , ['user' => '__USER_ID__']) }}"
$.ajax({
url: "{{url('/admin/users/order-by')}}",
type: "POST",
data: {
order_by: orderBy,
_token: '{{csrf_token()}}'
},
dataType: 'json',
success: function (result) {
$('#contentDiv').show();
$.each(result.orderby, function (key, value) {
const $row = $('<tr />');
$row.append('<td>' + value.id + '</td>');
$row.append('<td>' + value.name + '</td>');
$row.append('<td>' + value.email + '</td>');
$row.append('<td>' + value.is_active + '</td>');
const $btn_grp = $('<td class="d-flex"><div class="btn-group" />');
$btn_grp.append('<a href="' + url1.replace('__USER_ID__', '{!! $user->id !!}') + '" class="btn btn-sm btn-purple">Permissions</a>');
$btn_grp.append('<a href="' + url2.replace('__USER_ID__', '1') + '" class="btn btn-sm btn-info">Wallet</a>');
$btn_grp.append('<a href="' + url3.replace('__USER_ID__', '1') + '" class="btn btn-sm btn-warning">Information</a>');
$btn_grp.append('<a href="' + url4.replace('__USER_ID__', '1') + '" class="btn btn-sm btn-navy">Edit</a>');
const $btn_close = $('</div></td></tr>');
$btn_grp.append($btn_close);
$row.append($btn_grp);
$("#user-list").append($row);
});
}
});
@endpush
@component('admin.layouts.content' , ['title' => 'Users List'])
...
@endcomponent
So as you can see I tried passing a user variable id like this:
$btn_grp.append('<a href="' + url1.replace('__USER_ID__', '{!! $user->id !!}') + '" class="btn btn-sm btn-purple">Permissions</a>');
But somehow I get Undefined variable: user error:
So what's going wrong here? How can I properly pass the variable?
