我有这个用户表,我已经实现了 yajra 数据表,但我没有找到关于如何根据身份验证用户隐藏列的具体解决方案。
我提到了这个答案,但它没有指定如何在 javascript 端实现它。 Unable to hide columns in the yajra datatables oracle
同样来自 yajra Laravel 站点的文档,他们要求设置 visible:false 以隐藏列,这是真的,但我想根据当前登录的用户状态隐藏/显示。
users/index.blade.php:
<table id="userDatatable" class="table table-bordered nowrap">
<thead>
<tr>
<th class="desktop tablet phone">{{ __('Name') }}</th>
<th class="desktop">{{ __('Contact') }}</th>
<th class="desktop tablet phone">{{ __('Status') }}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
用户/index.js:
$('#userDatatable').DataTable({
scrollX: true,
autoWidth: true,
bSort: false,
pageLength: 50,
lengthMenu: [
[50, 100, -1],
['50', '100', 'Show all']
],
ajax: {
url: "users",
data: {id: id},
},
columns: [
{data: 'name', name: 'name'},
if($('#userStatus').val() == 'active'){ <-------- I want to show only if this auth user is active
{data: 'contact', name: 'contact'},
}
{data: 'status', name: 'status'},
]
})
UserController.php:
public function index(Request $request)
{
$users = User::get();
if($request->ajax())
{
return datatables()
->eloquent($users)
->addColumn('name', function($user){
return $user->name;
})
->addColumn('contact', function($user){
return $user->contact;
})
->addColumn('status', function($user){
return $user->status;
})
->make(true);
}
$users = User::orderBy('name', 'asc')->get();
return view('users.index');
}