4

如何将 ( 等 ) 之类的参数传递res_idresname( DataTable Service Class)RestaurantDataTable以进行自定义数据库查询,例如 ( get only restaurant whereid = xresname = xxx)?

餐厅控制器:

use App\DataTables\restaurantDataTable;

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

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

       }
    }

餐厅数据表:

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';
    }
}

Table.blade.php:

@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) {}中的函数public function custom_ajax($param) {}吗?

4

1 回答 1

3

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

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'
            ]
        ]);
}

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

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

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

于 2017-05-26T08:28:53.700 回答