如何在数据表中按列联合搜索?

时间:2016-02-26 09:28:08

标签: javascript mysql laravel datatables laravel-5.1

我需要按用户名字和姓氏搜索哪里合并为一个。我正在使用laravel 5.1和http://datatables.yajrabox.com/

这是我的后端查询代码:

                $orders = Order::select([
                                    'orders.created_at',
                                    'users.firstname AS user_firstname',
                                    'users.lastname AS user_lastname'
                            ])
                            ->join('users', 'users.id', '=', 'orders.user_id')
                            ->where('orders.client_id', \Auth::user()->client_id)
                            ->where('orders.status', 'finished');

返回:

        return Datatables::of($orders)
                    ->addColumn('user', function ($user) {
                        return $user->user_firstname.' '.$user->auser_lastname;
                    })
                    ->addColumn('action', function ($order) {
                        return '<div class="btn-group" style="min-width: 76px;">
                                    <a href="'.url('history/order/edit/'.$order->id).'" class="btn btn-default"><i class="fa fa-edit"></i></a>
                                    <a href="'.url('history/order/delete/'.$order->id).'" class="btn btn-default" onClick="return confirm(\'Are you sure?\');"><i class="fa fa-times"></i></a>
                                </div>';
                    })
                    ->removeColumn('user_firstname')
                    ->removeColumn('user_lastname')
                    ->make(true);

我将user列合并为user_firstname和user_lastname。

当我在datatable中搜索时,我需要按此列搜索 - USER。查询是这样的:

SELECT
    `orders`.`created_at`,
    `users`.`firstname` AS `agent_firstname`,
    `users`.`lastname` AS `agent_lastname`,
FROM
    `orders`
INNER JOIN `users` ON `users`.`id` = `orders`.`user_id`
WHERE
    `orders`.`client_id` = '3'
AND `orders`.`status` = 'finished'
AND (
    LOWER(`orders`.`created_at`) LIKE '%search_word%'
    OR LOWER(`users`.`firstname`) LIKE '%search_word%'
    OR LOWER(`users`.`lastname`) LIKE '%search_word%'
)
ORDER BY
    `orders`.`created_at` DESC
LIMIT 10 OFFSET 0

这是我的数据表JS:

// Data tables
var oTable = $('#orders-data').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": '/history/orders',
    "columns": [
        { data: 'created_at', name: 'orders.created_at' },
        { data: 'user', name: 'user' },
        { data: 'action', name: 'action', orderable: false, searchable: false }
    ],
});

如果我将JS中的行{data:'user',名称:'user'}更改为{data:'user',名称:'users.firstaname'},而不是仅在users.firstname列中搜索,但我需要也可以在users.lastname中搜索。

怎么做?

1 个答案:

答案 0 :(得分:0)

问题解决了!这是我的代码:

$orders = Order::select([
            'orders.created_at',
            \DB::raw("CONCAT(users.firstname,' ',users.lastname) as user")
        ])
        ->join('users', 'users.id', '=', 'orders.user_id')
        ->where('orders.client_id', \Auth::user()->client_id)
        ->where('orders.status', 'finished');

函数返回:

return Datatables::of($orders)
            ->filterColumn('user', function($query, $keyword) {
                $query->whereRaw("CONCAT(users.firstname,' ',users.lastname) like ?", ["%{$keyword}%"]);
            })
            ->addColumn('action', function ($order) {
                return '<div class="btn-group" style="min-width: 76px;">
                            <a href="'.url('history/order/edit/'.$order->id).'" class="btn btn-default"><i class="fa fa-edit"></i></a>
                            <a href="'.url('history/order/delete/'.$order->id).'" class="btn btn-default" onClick="return confirm(\'Are you sure?\');"><i class="fa fa-times"></i></a>
                        </div>';
            })
            ->removeColumn('user_firstname')
            ->removeColumn('user_lastname')
            ->make(true);

也许对其他人有帮助!