Laravel yajra Datatable:如何将参数发送到DataTable Service Class以进行自定义查询?

时间:2017-03-16 12:08:56

标签: php laravel-5 datatables yajra-datatable

如何将(res_idresname等参数传递给DataTable Service ClassRestaurantDataTable)以进行自定义数据库查询(例如,只获取{{1 },或id = x等)?

RestaurantController:

resname = xxx

restaurantDataTable:

use App\DataTables\restaurantDataTable;

    class restaurantController extends AppBaseController
    {
       public function index(restaurantDataTable $restaurantDataTable)
       {

           return $restaurantDataTable->render('restaurant.index');

       }
    }

Table.blade.php:

class restaurantDataTable extends DataTable
{

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajax()
    {

        return $this->datatables
            ->eloquent($this->query())
            ->addColumn('action', 'restaurant.datatables_actions')
            ->make(true);
    }

    /**
     * Get the query object to be processed by datatables.
     *
     * @return \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {

        $restaurants = restaurant::query();

        return $this->applyScopes($restaurants);



    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->addAction(['width' => '10%'])
            ->ajax('')
            ->parameters([
                'dom' => 'Blfrtip',
                'scrollX' => false,
                'buttons' => [
                    'print',
                    'reset',
                    'reload',
                    [
                         'extend'  => 'collection',
                         'text'    => '<i class="fa fa-download"></i> Export',
                         'buttons' => [
                             'csv',
                             'excel',
                             'pdf',
                         ],
                    ],
                    'colvis'
                ]
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    private function getColumns()
    {
        return [
            'res_name' => ['name' => 'res_name', 'data' => 'res_name'],
            'res_address' => ['name' => 'res_address', 'data' => 'res_address'],
            'res_state' => ['name' => 'res_state', 'data' => 'res_state'],
            'res_location' => ['name' => 'res_location', 'data' => 'res_location'],
            'res_area' => ['name' => 'res_area', 'data' => 'res_area']
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'restaurant';
    }
}

我是否需要自定义@section('css') @include('layouts.datatables_css') @endsection {!! $dataTable->table(['width' => '100%']) !!} @section('scripts') @include('layouts.datatables_js') {!! $dataTable->scripts() !!} @endsection 类中的功能,例如UsersDataTablepublic function custom_query($param) {}

1 个答案:

答案 0 :(得分:2)

将参数传递给函数index @ restaurantController(总是这样做),datatable对象将捕获它们。您可以检查参数是否已发送:

public function index(restaurantDataTable $restaurantDataTable)
   {
       dd($restaurantDataTable->->request()->all());
       return $restaurantDataTable->render('restaurant.index');

   }

发送参数后,在restaurantDataTable中编辑html()函数,如下所示:

public function html()
{
    $url = 'yourUrl';
    if ($this->request()->has("res_id")) {
        $url = $url."?resId=".$this->request()->get("res_id");
    }

    return $this->builder()
        ->columns($this->getColumns())
        ->addAction(['width' => '10%'])
        ->ajax($url)
        ->parameters([
            'dom' => 'Blfrtip',
            'scrollX' => false,
            'buttons' => [
                'print',
                'reset',
                'reload',
                [
                     'extend'  => 'collection',
                     'text'    => '<i class="fa fa-download"></i> Export',
                     'buttons' => [
                         'csv',
                         'excel',
                         'pdf',
                     ],
                ],
                'colvis'
            ]
        ]);
}

现在datatable将发送参数。之后,您可以像这样过滤查询:

public function ajax()
{
    return $this->datatables
        ->eloquent($this->query())
        ->addColumn('action', 'restaurant.datatables_actions')
         ->filter(function ($query) {           
            if ($this->request()->has("resId")) {
                $query->where("res_id", $this->request()->get("resId"));
            }
        })
        ->make(true);
}

我知道这不是最好的解决方案,但现在可以使用。我希望它有所帮助。