基于身份验证隐藏列 - Yajra 数据表

问题描述

我有这个用户表,我已经实现了 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');

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...