Last active
October 17, 2016 03:49
-
-
Save mariapaulinar/edbacd8d561cdff4162758747392c35f to your computer and use it in GitHub Desktop.
Crear datatables desde controlador
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Yajra\Datatables\Html\Builder; // import class on controller | |
/** | |
* Datatables Html Builder | |
* @var Builder | |
*/ | |
protected $htmlBuilder; | |
public function __construct(Builder $htmlBuilder) | |
{ | |
$this->htmlBuilder = $htmlBuilder; | |
} | |
public function getBasic(Request $request) | |
{ | |
if ($request->ajax()) { | |
return Datatables::of(User::select(['id', 'name', 'email', 'created_at', 'updated_at'])) | |
->addColumn('actions', function ($user) { | |
return '<a>Edit</a>' | |
}) | |
->make(true); | |
} | |
$html = $this->htmlBuilder | |
->parameters([ | |
'language' => ['url' => 'https://cdn.datatables.net/plug-ins/1.10.12/i18n/Spanish.json'] | |
]) | |
->addColumn(['data' => 'id', 'name' => 'id', 'title' => 'Id']) | |
->addColumn(['data' => 'name', 'name' => 'name', 'title' => 'Name']) | |
->addColumn(['data' => 'email', 'name' => 'email', 'title' => 'Email']) | |
->addColumn(['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At']) | |
->addColumn(['data' => 'updated_at', 'name' => 'updated_at', 'title' => 'Updated At']) | |
->addColumn(['data' => 'actions', 'name' => 'actions', 'title' => 'Actions', 'orderable' => false, 'searchable' => false]); | |
return view('datatables.html.basic', compact('html')); | |
} | |
//En la vista | |
@section('demo') | |
{!! $html->table() !!} | |
@endsection | |
@section('scripts') | |
{!! $html->scripts() !!} | |
@endsection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment