2017 © Pedro Peláez
 

library datatable-builder

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

image

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.

  • Monday, February 12, 2018
  • by Kezho
  • Repository
  • 6 Watchers
  • 7 Stars
  • 5,409 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 37 Versions
  • 5 % Grown

The README.md

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License, (*1)

Laravel 5 Datatable Builder

Based on chumper/datatable. It's an abstract class to implement the datatable like the form generator., (*2)

Table of contents

  1. Installation
  2. Basic usage
  3. Closure
  4. BaseQuery
  5. Filters
  6. Styles
  7. Controller

Installation

Add on your composer.json, (*3)

``` json "require": { "distilleries/datatable-builder": "1.*", }, (*4)


run `composer update`. Add Service provider to `config/app.php`: ``` php 'providers' => [ // ... 'Distilleries\FormBuilder\FormBuilderServiceProvider', 'Distilleries\DatatableBuilder\DatatableBuilderServiceProvider', ]

And Facade (also in config/app.php), (*5)

``` php 'aliases' => [ // ... 'FormBuilder' => 'Distilleries\FormBuilder\Facades\FormBuilder', 'Datatable' => 'Distilleries\DatatableBuilder\Facades\DatatableBuilder', ], (*6)


Add the javascript on your bower dependencies: ``` json "dependencies": { "datatables": "~1.10.4", }

Export the configuration:, (*7)

php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"

Export the views (optional):, (*8)

php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"  --tag="views"

Export the Javascript assets (optional):, (*9)

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:, (*10)

php artisan vendor:publish --provider="Distilleries\DatatableBuilder\DatatableBuilderServiceProvider"  --tag="assets"

You can find those in resources/assets/vendor/datatable-builder., (*11)

Basic usage

Creating form classes is easy. With a simple artisan command I can create a datatable:, (*12)

``` sh php artisan datatable:make Datatables/PostDatatable, (*13)


you create form class in path `app/Datatables/PostDatatable.php` that looks like this: ``` php <?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:, (*14)

``` sh php artisan datatable:make Datatables/SongDatatable --fields="name, lyrics", (*15)


And that will create a datatable in path `app/Datatables/SongDatatable.php` with content: ``` php <?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:, (*16)

add($name, $closure = null, $translation = '', $orderable = true), (*17)

Param Usage
name Name of the column
closure Function with the model in parameter to return a template. By default null use the attribute of the model.
translation Translation of the column, by default empty use the column name
orderable Flag to handle column orderable state, by default 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., (*18)

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., (*19)

``` php $this->add('profile_name', function ($model) { $profile = $model->profile;, (*20)

    return (empty($profile)?'':$profile->first_name . ' ' . $profile->last_name);
}, _('Profile'));

```, (*21)

On my model Address I have:, (*22)

``` php public function profile() { return $this->belongsTo('Profile'); }, (*23)


![datatable](http://distilleri.es/markdown/formbuilder/_images/datatable.png) You can return a template if you want: ``` php $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., (*24)

By default it will send a fresh full Query: $this->model->newModelQuery();, (*25)

``` php /** * {@inheritdoc} */ protected function baseQuery() { return $this->model->newModelQuery() ->selectRaw("id, data->>'$.title' AS title, data->>'$.chapo' AS intro, created_at"); }, (*26)


## Filters You can use complex filter to filter your datatable. To do that I use the library [FormBuilder](https://github.com/Distilleries/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: ``` php public function filters() { $this->form->add('status', 'choice', [ 'choices' => StaticLabel::status(), 'empty_value' => _('-'), 'validation' => 'required', 'label' => _('Status') ]); }

filters, (*27)

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., (*28)

``` php public function applyFilters() { $allInput = Input::all(); $columns = \Schema::getColumnListing($this->model->getTable());, (*29)

    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. ``` php 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., (*30)

``` php public function setClassRow($datatable) { $datatable->setRowClass(function ($row) { return (isset($row->status) and empty($row->status)) ? 'danger' : ''; });, (*31)

    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 <?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 :, (*32)

Route::controllers([
    'datatable' => 'DatatableController'
]);

Like you can see I inject the model and the datatable on the constructor. On the welcome template I put:, (*33)

<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., (*34)

The Versions

12/02 2018

dev-master

9999999-dev

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

12/02 2018

2.11.0

2.11.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

08/02 2018

2.10.0

2.10.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

03/03 2017

2.9.2

2.9.2.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

03/03 2017

2.8.x-dev

2.8.9999999.9999999-dev

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

03/03 2017

2.8.2

2.8.2.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

23/02 2017

2.9.1

2.9.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

23/02 2017

2.9.0

2.9.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

28/10 2016

2.8.1

2.8.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

26/10 2016

2.8.0

2.8.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

11/10 2016

2.7.0

2.7.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

10/10 2016

2.6.1

2.6.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

10/10 2016

dev-version/2.5

dev-version/2.5

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

10/10 2016

2.5.1

2.5.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

15/09 2016

2.6.0

2.6.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

26/07 2016

2.5.0

2.5.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

22/07 2016

2.4.2

2.4.2.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

22/07 2016

2.4.1

2.4.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

22/07 2016

2.4.0

2.4.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

29/03 2016

2.3.1

2.3.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

15/01 2016

2.3.0

2.3.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

20/08 2015

2.2.3

2.2.3.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

03/07 2015

2.2.2

2.2.2.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

03/07 2015

2.2.1

2.2.1.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

03/07 2015

2.2.0

2.2.0.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

05/05 2015

dev-dev

dev-dev

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

05/05 2015

2.1.3

2.1.3.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois

30/04 2015

2.1.2

2.1.2.0

Based on https://github.com/chumper/datatable. It's an abstract class to implement the datatable like https://github.com/Distilleries/FormBuilder.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxime Francois