2015-12-03 71 views
0

顯示我使用的用戶列表yajra/laravel-datatablesfilterColumn在yajra/laravel-datatables中不起作用

除了Datatable自己的搜索框來過濾結果,我有一個組合框可以根據用戶的級別對用戶進行過濾。

這是我的組合框:

<div class="form-group" id="levelFilterWrapper"> 
           <label for="levelFilter">Userlevel :</label> 
           <select id="levelFilter" name="levelFilter" class="form-control"> 
            <option value="all">All</option> 
            <option value="admin">admin</option> 
            <option value="user">user</option> 
            <option value="centerManager">centerAdmin</option> 
           </select> 
          </div> 

,這是我的jQuery代碼:

var allUsersTable = 
       $('#allUsersTable').DataTable({ 
        processing: true, 
        serverSide: true, 
        "bSort": false, 
        "responsive": true, 
        ajax: { 
         url : '{!! route('usersData') !!}', 
         data: function(d){ 
          d.level = $('#levelFilter').val() 
         } 
        }, 
        columns: [ 
         {data: 'checkbox', name: 'checkbox', "width": "20px"}, 
         {data: 'userPic', name: 'userPic', 'className': 'text-center'}, 
         {data: 'email', name: 'email'}, 
         {data: 'created_at', name: 'created_at'}, 
         {data: 'level', name: 'level', 'className': 'text-center'}, 
         {data: 'actions', name: 'actions', 'className': 'text-center'} 
        ] 
       }); 

     $('#levelFilter').change(function (e) { 


      allUsersTable.draw(); 
     }); 

,這是我yajra數據表laravel代碼:通過levelFilter組合框

$users = User::select(['user_id', 'name', 'family', 'email', 'created_at', 'level']); 

     $datatable = app('datatables')->of($users) 
      ->filter(function ($query) use ($request) { 
       if (!empty($request->get('level')) and $request->get('level') != 'all') { 
        return $query->whereLevel($request->get('level')); 
       } else { 
        return $query; 
       } 
      }) 
      ->orderBy('created_at', 'desc') 
      ->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$user_id}}" id="Check_{{$user_id}}" class="minimal">') 
      ->addColumn('userPic', function ($user) { 
       return '<img src="http://www.gravatar.com/avatar/' . md5($user->email) . '?d=mm&s=30" class="img-thumbnail img-responsive">'; 
      }) 
      ->editColumn('email', function ($user) { 
       return ' 
         ' . $user->name . ' ' . $user->family . ' 
         <div class="en-text"><a href="mailto:' . $user->email . '" target="_blank">' . $user->email . '</a></div> 
        '; 
      }) 

      ->editColumn('created_at', function ($user) { 
       return JDF::jdate('l j F Y - G:i', $user->created_at); 
      }) 
      ->addColumn('actions', function ($user) { 
       return ' 
         <a href="/admin/user/' . $user->user_id . '/edit" class="btn btn-primary btn-sm btn-flat fa fa-pencil-square-o" data-toggle="tooltip" data-placement="top" title="Edit And View"></a> 
         <a href="#" class="btn btn-danger btn-sm btn-flat fa fa-trash-o delItem" data-toggle="tooltip" data-placement="top" title="Delete" data-item-id="' . $user->user_id . '"></a> 
        '; 
      }); 

     if ($keyword = $request->get('search')['value']) { 
      $datatable->filterColumn('name', 'where', '=', "%$keyword%"); 
     } 

     return $datatable->make(true); 

濾波工作正常但我希望當管理員選擇一個用戶級別表單,通過DataTabl進行篩選在搜索框中搜索該選定的級別。但類型後搜索框中輸入一些字符,再次顯示了組合框過濾完全相同的結果,似乎搜索框篩選不會在這種情況下

回答

0

工作,根據在yajra/laravel-的DataTable Github的頁面yajra答案:

使用 - > filter()將禁用全局搜索,導致搜索 文本框不起作用。如果你想使用過濾器,那麼你需要 爲每個字段編寫每個查詢。

在另一方面,你可以只提取級過濾上查詢 水平做出這樣的工作:

$users = User::select(['user_id', 'name', 'family', 'email', 'created_at', 'level']); 
if (!empty($request->get('level')) and $request->get('level') != 'all') { 
    $users = $users->whereLevel($request->get('level')); 
} 

    $datatable = app('datatables')->of($users) 
     ->orderBy('created_at', 'desc') 
     ->addColumn('checkbox', '<input type="checkbox" name="item_id[]" value="{{$user_id}}" id="Check_{{$user_id}}" class="minimal">') 
     ->addColumn('userPic', function ($user) { 
      return '<img src="http://www.gravatar.com/avatar/' . md5($user->email) . '?d=mm&s=30" class="img-thumbnail img-responsive">'; 
     }) 
     ->editColumn('email', function ($user) { 
      return ' 
        ' . $user->name . ' ' . $user->family . ' 
        <div class="en-text"><a href="mailto:' . $user->email . '" target="_blank">' . $user->email . '</a></div> 
       '; 
     }) 

     ->editColumn('created_at', function ($user) { 
      return JDF::jdate('l j F Y - G:i', $user->created_at); 
     }) 
     ->addColumn('actions', function ($user) { 
      return ' 
        <a href="/admin/user/' . $user->user_id . '/edit" class="btn btn-primary btn-sm btn-flat fa fa-pencil-square-o" data-toggle="tooltip" data-placement="top" title="Edit And View"></a> 
        <a href="#" class="btn btn-danger btn-sm btn-flat fa fa-trash-o delItem" data-toggle="tooltip" data-placement="top" title="Delete" data-item-id="' . $user->user_id . '"></a> 
       '; 
     }); 

    return $datatable->make(true); 
+0

如果級別加密的,因此如何在laravel使用此過濾器@ABDeveloper ? 我已經使用特徵加密Crypt來加密和解密 –

相關問題