Laravel-datatables: Using Service Implementation - Is is possible to pass variables to view?

Created on 1 Feb 2016  路  5Comments  路  Source: yajra/laravel-datatables

Using the service implementation I would still like to pass information to the view so that I can customize the page a bit. Right now I can only pass the datatable itself. I have tried various syntax, but seem to be stumped.

Can I do something like this?

   public function index(ProductDataTable $dataTable)
    {
       $title="foo";
       $otherData="bar";
        return $dataTable->render('datatables.index',compact($title, $otherData));
    }

Most helpful comment

Render accepts the same parameter as view->make(). Passing of array variables should work like your example. BTW, your compact method seems wrong. It should be compact('title', 'otherData') and not the variable itself.

/**
     * Render view.
     *
     * @param $view
     * @param array $data
     * @param array $mergeData
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function render($view, $data = [], $mergeData = [])

All 5 comments

Render accepts the same parameter as view->make(). Passing of array variables should work like your example. BTW, your compact method seems wrong. It should be compact('title', 'otherData') and not the variable itself.

/**
     * Render view.
     *
     * @param $view
     * @param array $data
     * @param array $mergeData
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function render($view, $data = [], $mergeData = [])

You are 100% correct. I am not sure why I didn't catch the compact mistake. That was it all along.

:+1:

Hi I have the same issue here but mines its not working. Dont know what im doing wrong :(
This its my function:

public function proxy(DemandaDataTable $demandaDataTable, $filtro){

        if ($filtro == 'demandas_completas') {

            $demandas = $this->demandaRepository->getDemandasCompletas()->toArray();

            return $demandaDataTable->render('demandas.index', $demandas);  

        }

        if ($filtro == 'demandas_a_vencer') {

            $demandas = $this->demandaRepository->getDemandasAVencer()->toArray();

            return $demandaDataTable->render('demandas.index', $demandas);
        }

        if ($filtro == 'demandas_vencidas'){

            $demandas = $this->demandaRepository->getDemandasVencidas()->toArray();

            return $demandaDataTable->render('demandas.index', $demandas);
        }        
    }

And my Class:

<?php

namespace App\DataTables;

use App\Models\Demanda;
use App\Models\Gabinete;
use Form;
use Yajra\Datatables\Services\DataTable;
use Carbon\Carbon;

class DemandaDataTable extends DataTable
{

    /**
     * @return \Illuminate\Http\JsonResponse
     */

    public function ajax()
    {   

        $demandas = $this->query();

        return $this->datatables
            ->eloquent($demandas)
            ->addColumn('action', 'demandas.datatables_actions')
            ->editColumn('fecha_fin', function ($demandas) {
                return $demandas->fecha_fin ? with(new Carbon($demandas->fecha_fin))->format('d/m/Y') : '';              
            })
            ->editColumn('completa', function($demandas){
                return $this->filterCompleta($demandas->completa);
            })
            ->editColumn('reporte', function($demandas){
                $demanda_id = $demandas->id;
                return '<a href="reportes/create/' . $demanda_id . '"' .  'class="btn-xs btn-warning">Reporte</a>';
            })
            ->make(true);

    }

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

        $demandas = Demanda::with('gabinete')
                            ->with('jurisdiccion')
                            ->select('demandas.*');

        return $this->applyScopes($demandas);

    }

    /**
     * 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([
                'initComplete' => "function () {
                    this.api().columns([4]).every( function () {
                        var column = this;

                        var select = $('<select id=" . '"select2"' . "class=" . '"select2 form-control"' . "><option></option>" . "<option value=" . '"1"' . ">" . "Si</option>" . "<option value=" . '"0"' . ">" . "No</option>" . ")" . "</select>')  
                            .appendTo( $(column.footer()).empty() )
                            .on( 'change', function () {
                                var val = $.fn.dataTable.util.escapeRegex(
                                    $(this).val()    
                                );

                                column
                                    .search( val ? '^'+val+'$' : '', true, false )
                                    .draw();
                            } );

                        column.data().unique().sort().each( function ( d, j ) {
                        } );
                    });

                }",
                'responsive' => true,
                'searching' => true,
                'dom' => 'Bfrtip',
                '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 [
            'titulo' => ['name' => 'titulo', 'data' => 'titulo'],
            'gabinete' => ['name' => 'gabinete.titulo', 'data' => 'gabinete.titulo', 'class'=> 'visible-md visible-lg', 'searchable' => true],
            'jurisdiccion' => ['name' => 'jurisdiccion.nombre', 'data' => 'jurisdiccion.nombre', 'searchable' => true],     
            'Entrega' => ['name' => 'fecha_fin', 'data' => 'fecha_fin', 'class'=> 'visible-md visible-lg'],
            'Entregada' => ['name' => 'completa', 'data' => 'completa', 'class'=> 'visible-md visible-lg', 'searchable' => true],
            'reporte' => ['name' => 'reporte',  'searchable' => false]
        ];
    }

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

    private function filterCompleta($completa)
    {
        if ($completa) { 
            return 'Si'; 
        } else {
            return 'No';    
        }    
    }

}

Render accepts the same parameter as view->make(). Passing of array variables should work like your example. flow this code

public function index(UserDataTable $dataTable)
{
$title = "this is Hasan";
return $dataTable->render('index', compact('title'));
}

Was this page helpful?
0 / 5 - 0 ratings