filterColumn在yajra / laravel-datatables

时间:2015-12-03 08:18:21

标签: jquery laravel datatables

显示我使用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 Datatables laravel代码:

$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);

通过levelFilter过滤器组合工作正常但我想在管理员选择用户级别表单时,通过DataTable搜索框过滤,在该选定级别进行搜索。但在搜索框中键入一些字符后,再次显示完全相同的组合框过滤结果,似乎搜索框过滤在这种情况下不起作用

1 个答案:

答案 0 :(得分:0)

根据 yajra 回答yajra / laravel-datatables Github页面:

  

使用 - &gt; 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);