distilleries / datatable-builder
Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.
Installs: 9 528
Dependents: 2
Suggesters: 0
Security: 0
Stars: 9
Watchers: 9
Forks: 9
Open Issues: 0
Requires
- php: >=7.1.3
- distilleries/datatable: 3.1.*
- distilleries/form-builder: 2.13.*
- illuminate/filesystem: 5.8.*
- illuminate/support: 5.8.*
- illuminate/view: 5.8.*
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: ~1.0
- orchestra/database: 3.8.*
- orchestra/testbench: 3.8.*
- orchestra/testbench-browser-kit: 3.8.*
- phpunit/phpunit: ~7.0
- dev-master
- 2.14.1
- 2.14.0
- 2.13.2
- 2.13.1
- 2.13.0
- 2.12.0
- 2.11.0
- 2.10.0
- 2.9.2
- 2.9.1
- 2.9.0
- 2.8.x-dev
- 2.8.2
- 2.8.1
- 2.8.0
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.2.0
- 1.1.0
- 1.0.4
- 1.0.3
- dev-version/2.5
- dev-dev
- dev-feat/unittest
- dev-laravel-4
This package is auto-updated.
Last update: 2024-12-10 21:16:39 UTC
README
#Laravel 5 Datatable Builder
Based on chumper/datatable. It's an abstract class to implement the datatable like the form generator.
Table of contents
##Installation
Add on your composer.json
"require": { "distilleries/datatable-builder": "1.*", }
run composer update
.
Add Service provider to config/app.php
:
'providers' => [ // ... 'Distilleries\FormBuilder\FormBuilderServiceProvider', 'Distilleries\DatatableBuilder\DatatableBuilderServiceProvider', ]
And Facade (also in config/app.php
)
'aliases' => [ // ... 'FormBuilder' => 'Distilleries\FormBuilder\Facades\FormBuilder', 'Datatable' => 'Distilleries\DatatableBuilder\Facades\DatatableBuilder', ]
Add the javascript on your bower dependencies:
"dependencies": { "datatables": "~1.10.4", }
Export the configuration:
php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"
Export the views (optional):
php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider" --tag="views"
Export the Javascript assets (optional):
Include the javascript with gulp or grunt vendor/distilleries/datatable-builder/resources/**/*
.
If you don't use a task manager or you want override the javascript just publish those with the command line:
php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider" --tag="assets"
You can find those in resources/assets/vendor/datatable-builder
.
Basic usage
Creating form classes is easy. With a simple artisan command I can create a datatable:
php artisan datatable:make Datatables/PostDatatable
you create form class in path app/Datatables/PostDatatable.php
that looks like this:
<?php namespace App\Datatables; use Distilleries\DatatableBuilder\EloquentDatatable; class PostDatatable extends EloquentDatatable { public function build() { // Add fields here... $this->addDefaultAction(); } }
You can add fields which you want when creating command like this:
php artisan datatable:make Datatables/SongDatatable --fields="name, lyrics"
And that will create a datatable in path app/Datatables/SongDatatable.php
with content:
<?php namespace App\Datatables; use Distilleries\DatatableBuilder\EloquentDatatable; class SongDatatable extends EloquentDatatable { public function build() { $this ->add('name',null,_('Name')) ->add('lyrics',null,_('Lyrics')); $this->addDefaultAction(); } }
The method add
have in param:
add($name, $closure = null, $translation = '', $orderable = true)
Closure
When you create a datatatable sometime the content need to be get from other table or stylize by a template like the actions buttons.
For example an address is link to a profile. To display the profile name on the datatable like the picture you can use the closure.
$this->add('profile_name', function ($model) { $profile = $model->profile; return (empty($profile)?'':$profile->first_name . ' ' . $profile->last_name); }, _('Profile'));
On my model Address
I have:
public function profile() { return $this->belongsTo('Profile'); }
You can return a template if you want:
$this->add('html_row', function ($model) { return View::make('admin.address.html_row',[ ])->render(); });
BaseQuery
You can override the base query for the datatable query.
By default it will send a fresh full Query: $this->model->newModelQuery();
/** * {@inheritdoc} */ protected function baseQuery() { return $this->model->newModelQuery() ->selectRaw("id, data->>'$.title' AS title, data->>'$.chapo' AS intro, created_at"); }
Filters
You can use complex filter to filter your datatable. To do that I use the library FormBuilder. All the datatable have a plain form filter. If you had field on this form that display the filters.
For example we want all the user only online.
I create a choice field:
public function filters() { $this->form->add('status', 'choice', [ 'choices' => StaticLabel::status(), 'empty_value' => _('-'), 'validation' => 'required', 'label' => _('Status') ]); }
When the filters are submitted you can apply it with the method applyFilters
.
By default this method create a where with the name of the filter field if it's an attribute of the model.
If you want change this behaviour you can override it.
public function applyFilters() { $allInput = Input::all(); $columns = \Schema::getColumnListing($this->model->getTable()); foreach ($allInput as $name => $input) { if (in_array($name, $columns) and $input != '') { $this->model = $this->model->where($name, '=', $input); } } }
If you don't want create a global scope of your model you just want restrict the display for this datatable.
you can use applyFilters
to do that.
For example I want display only the customer of the application: The datable work on the user model.
public function applyFilters() { parent::applyFilters(); $customer = \Role::where('initials', '==', '@c')->get()->last(); $this->model = $this->model->where('role_id', '=', $customer->id); }
Styles
You want stylize your row to put it on green, blue or red.
You can do that with setClassRow
. By default this method check the status attribute.
If the status exist and it is empty we add the danger class to display it in red.
public function setClassRow($datatable) { $datatable->setRowClass(function ($row) { return (isset($row->status) and empty($row->status)) ? 'danger' : ''; }); return $datatable; }
##Controller
You can use the trait Distilleries\DatatableBuilder\States\DatatableStateTrait
to add in your controller the default methods use with the datatable.
Example:
I created a controller app/Http/Controllers/DatatableController
:
<?php namespace App\Http\Controllers; use App\Datatables\UserDatatable; class DatatableController extends Controller { use \Distilleries\DatatableBuilder\States\DatatableStateTrait; /* |-------------------------------------------------------------------------- | Welcome Controller |-------------------------------------------------------------------------- | | This controller renders the "marketing page" for the application and | is configured to only allow guests. Like most of the other sample | controllers, you are free to modify or remove it as you desire. | */ /** * Create a new controller instance. * * @return void */ public function __construct(\App\User $model, UserDatatable $datatable) { $this->model = $model; $this->datatable = $datatable; } /** * Show the application welcome screen to the user. * * @return Response */ public function getIndex() { return view('welcome',[ 'datatable'=>$this->getIndexDatatable() ]); } }
I add the controller on the route file :
Route::controllers([ 'datatable' => 'DatatableController' ]);
Like you can see I inject the model and the datatable on the constructor. On the welcome template I put:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css"> <link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <script src="/vendor/datatable-builder/js/datatable.js"></script> </head> <body> <div class="container"> {!! $datatable !!} </div> </body> </html>
That it you have your datatable from the user model.