【发布时间】:2022-08-14 16:40:51
【问题描述】:
我正在使用 Laravel 8,并且在刀片的一个表中检索了 users 的列表:
<table class=\"table table-hover\" id=\"contentDiv\">
<tbody>
<tr>
<th></th>
<th>Username</th>
<th>Email</th>
<th>User Status</th>
</tr>
@foreach($users as $user)
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->status }}</td>
@endforeach
</table>
这是我的控制器方法:
public function index()
{
$users = User::query();
if(request(\'desc\') == 1){
$users->orderBy(\'id\',\'DESC\');
}else{
$users->orderBy(\'id\',\'ASC\');
}
$users = $users->latest()->paginate(20);
return view(\'admin.users.all\', compact(\'users\'));
}
然后我尝试添加一个可以更改表格顺序的选择选项:
<select class=\"form-control select2\" id=\"dropdown-list\">
<option value=\"asc\" selected=\"selected\">Ascending</option>
<option value=\"desc\">Descending</option>
</select>
例如,如果用户单击Descending 选项,ajax 请求必须将表顺序从asc 更改为desc。
这是我对 Ajax 的尝试:
$(\"#dropdown-list\").on(\'change\', function(){
var val = $(this).val();
if(val == \"desc\") {
$.ajax({
url: baseurl + \'/admin/users?desc=1\',
type: \'get\',
data: {
val: val,
},
jsonType: \'json\',
success: function (response) {
$(\"#contentDiv tr\").remove();
// populate new data and append to table
}
});
}else{
$.ajax({
url: baseurl + \'/admin/users\',
type: \'get\',
data: {
val: val,
},
jsonType: \'json\',
success: function (response) {
$(\"#contentDiv tr\").remove();
// populate new data and append to table
}
});
}
});
所以我尝试删除整个表格内容,然后我应该填充新数据($users)并将它们附加到表格中。
我怎样才能做到这一点?
更新 1
UserController.php:
public function index()
{
$users = User::query();
if(request(\'desc\') == 1){
$users->orderBy(\'id\',\'DESC\');
}else{
$users->orderBy(\'id\',\'ASC\');
}
$users = $users->latest()->paginate(20);
return response()->json([
\'users\' => $users
]);
}
这是我的刀片模板:
@push(\'scripts\')
var environ = window.location.host;
var index = environ.includes(\"localhost\");
if (index) {
var baseurl = \"{{ URL::to(\'/\') }}\";
} else {
var baseurl = window.location.protocol + \"//\" + window.location.host + \"/\";
}
var environ = window.location.host;
var index = environ.includes(\"localhost\");
if (index) {
var baseurl = \"{{ URL::to(\'/\') }}\";
} else {
var baseurl = window.location.protocol + \"//\" + window.location.host + \"/\";
}
$(\"#dropdown-list\").on(\'change\', function(){
var val = $(this).val();
if(val == \"desc\") {
$.ajax({
url: baseurl + \'/admin/users?desc=1\',
type: \'get\',
data: {
val: val,
},
jsonType: \'json\',
success: function (response) {
$(\'#contentDiv\').find(\'tbody\').html(\'\');
response.users.forEach(refill_table);
function refill_table(item, index){
$(\'#contentDiv\').find(\'tbody\').append(`
<tr>
<td>${item[\'id\']}</td>
<td>${item[\'name\']}</td>
<td>${item[\'email]}</td>
<td>${item[\'status\']}</td>
</tr>
`);
}
}
});
}else{
$.ajax({
url: baseurl + \'/admin/users\',
type: \'get\',
data: {
val: val,
},
jsonType: \'json\',
success: function (response) {
$(\'#contentDiv\').find(\'tbody\').html(\'\');
response.users.forEach(refill_table);
function refill_table(item, index){
$(\'#contentDiv\').find(\'tbody\').append(`
<tr>
<td>${item[\'id\']}</td>
<td>${item[\'name\']}</td>
<td>${item[\'email]}</td>
<td>${item[\'status\']}</td>
</tr>
`);
}
}
});
}
});
@endpush
@component(\'admin.layouts.content\' , [\'title\' => \'Users Lists\'])
<div class=\"card-tools d-flex\">
<div class=\"input-group input-group-sm\" style=\"width: 150px;\">
<select class=\"form-control select2\" style=\"width: 50%;\" id=\"dropdown-list\" name=\"select\">
<option value=\"asc\" selected=\"selected\">Ascending</option>
<option value=\"desc\">Descending</option>
</select>
</div>
</div>
<table class=\"table table-hover\" id=\"contentDiv\">
<tbody>
<tr>
<th></th>
<th>Username</th>
<th>Email</th>
<th>Status</th>
</tr>
@if($users->count() != 0)
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->status }}</td>
</tr>
@endforeach
@else
<td colspan=\"10\" class=\"text-center\">Nothing to show</td>
@endif
@endcomponent
-
您可以创建输出表格的路由和控制器方法(完整的 HTML)。然后不是返回 JSON,而是调用该路由并将整个表替换为 AJAX 响应。
-
另一种选择是仅使用 JSON 获取数据并使用 JS 呈现表格。然后当用户想要更改顺序时,您只需对开始时获得的 JSON 进行排序并重新渲染表格即可。如果与数据的唯一区别是项目的顺序,则无需为服务器增加额外的请求。
-
@M.Eriksson这似乎是最好的方法,你介意告诉我如何作为答案...
-
我不是 JS 大师,我不知道你已经知道什么或者你在那个流程中被卡住了。我建议您对此进行一些研究。这是一种非常常见的模式,因此肯定有一些指南/教程。分解: 1. 发出 ajax 请求以获取数据。 2. 从json渲染表格。 3. 对 JSON 进行排序。 4. 重新渲染表格。
-
@M.Eriksson 好的,告诉我我是否应该这样退货:
return response()->json($users);
标签: php jquery ajax laravel laravel-8