This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
This package will not receive any new updates! You can still use this package, but be preparared that there is no active development for this project.
This package is
abandonedand not recommended for new projects. We recommend to use instead Yajra's Package which offers a nearly-similar API.
This is a Laravel 4 package for the server and client side of datatables at http://datatables.net/
A Laravel 5 package is close to being completed. To install it:
composer require chumper/datatable "dev-develop"If you find any issues, please report them in the bug tracker!
Please Note, if you want Datatable 1.10 support & Laravel 5 support, try out our newest branch!
If you upgrade from version 2.1.* or below please make sure you adjust your app.php with the new alias:
// aliases array:
//old
//'Datatable' => 'Chumper\Datatable\Facades\Datatable',
//new
'Datatable' => 'Chumper\Datatable\Facades\DatatableFacade',This package supports:
There are some differences between the collection part and the query part of this package. The differences are:
| Difference | Collection | Query |
|---|---|---|
| Speed | - | + |
| Custom fields | + | + |
| Search in custom fields | + | - |
| Order by custom fields | + | - |
| Search outside the shown data (e.g.) database | - | + |
For a detailed explanation please see the video below. http://www.youtube.com/watch?v=c9fao_5Jo3Y
Please let me know any issues or features you want to have in the issues section. I would be really thankful if you can provide a test that points to the issue.
This package is available on http://packagist.org, just add it to your composer.json
"chumper/datatable": "2.*"Alternatively, you can install it using the composer command:
composer require chumper/datatable "2.*"It also has a ServiceProvider for usage in Laravel4. Add these lines to app.php:
// providers array:
'Chumper\Datatable\DatatableServiceProvider',
// aliases array:
'Datatable' => 'Chumper\Datatable\Facades\DatatableFacade',You can then access it under the Datatable alias.
To override the default configuration options you can publish the config file.
php artisan config:publish chumper/datatableYou may now edit these options at app/config/packages/chumper/datatable/config.php.
There are two ways you can use the plugin, within one route or within two routes:
Route::resource('users', 'UsersController');
Route::get('api/users', array('as'=>'api.users', 'uses'=>'UsersController@getDatatable'));
{{ Datatable::table()
->addColumn('id','Name') // these are the column headings to be shown
->setUrl(route('api.users')) // this is the route where data will be retrieved
->render() }} public function getDatatable()
{
return Datatable::collection(User::all(array('id','name')))
->showColumns('id', 'name')
->searchColumns('name')
->orderColumns('id','name')
->make();
}You should now have a working datatable on your page.
In your route you should use the Datatable::shouldHandle method which will check whether the plugin should handle the request or not.
if(Datatable::shouldHandle())
{
return Datatable::collection(User::all(array('id','name')))
->showColumns('id', 'name')
->searchColumns('name')
->orderColumns('id','name')
->make();
}The plugin will then query the same url for information. The shouldHandle method just checks for an ajax request and if sEcho is set.
Datatable::table()
->addColumn('id',Lang::get('user.lastname'))
->setUrl(URL::to('auth/users/table'))
->render();With seperate table and script:
$table = Datatable::table()
->addColumn('Email2','Email', "Test")
->noScript();
// to render the table:
$table->render()
// later in the view you can render the javascript:
$table->script();This will generate a HTML table with two columns (id, lastname -> your translation) and will set the URL for the ajax request.
Note: This package will NOT include the
datatable.js, that is your work to do. The reason is that for example i use Basset and everybody wants to do it their way...
If you want to provide your own template for the table just provide the path to the view in laravel style.
Datatable::table()
->addColumn('id',Lang::get('user.lastname'))
->setUrl(URL::to('auth/users/table'))
->render('views.templates.datatable'); Datatable::collection(User::all())
->showColumns('id')
->addColumn('name',function($model)
{
return $model->getPresenter()->yourProperty;
}
)->make();This will generate a server side datatable handler from the collection User::all().
It will add the id column to the result and also a custom column called name.
Please note that we need to pass a function as a second parameter, it will always be called
with the object the collection holds. In this case it would be the User model.
You could now also access all relationship, so it would be easy for a book model to show the author relationship.
Datatable::collection(User::all())
->showColumns('id')
->addColumn('name',function($model)
{
return $model->author->name;
}
)->make();Note: If you pass a collection of arrays to the
collectionmethod you will have an array in the function, not a model.
The order of the columns is always defined by the user and will be the same order the user adds the columns to the Datatable.
There is a difference between query() and collection(). A collection will be compiled before any operation - like search or order - will be performed so that it can also include your custom fields. This said the collection method is not as performing as the query method where the search and order will be tackled before we query the database.
So if you have a lot of Entries (100k+) a collection will not perform well because we need to compile the whole amount of entries to provide accurate sets. A query on the other side is not able to perform a search or orderBy correctly on your custom field functions.
TLTR: If you have no custom fields, then use query() it will be much faster If you have custom fields but don't want to provide search and/or order on the fields use query(). Collection is the choice if you have data from somewhere else, just wrap it into a collection and you are good to go. If you have custom fields and want to provide search and/or order on these, you need to use a collection.
Please also note that there is a significant difference betweeen the search and order functionality if you use query compared to collections. Please see the following video for more details.
http://www.youtube.com/watch?v=c9fao_5Jo3Y
This package is separated into two smaller parts:
The second and third one is for the server side, the first one is a helper to generate the needed table and javascript calls.
collection($collection)
Will set the internal engine to the collection. For further performance improvement you can limit the number of columns and rows, i.e.:
$users = User::activeOnly()->get('id','name');
Datatable::collection($users)->...query($query)
This will set the internal engine to a Eloquent query... E.g.:
$users = DB::table('users');
Datatable::query($users)->...The query engine is much faster than the collection engine but also lacks some functionality, for further information look at the table above.
showColumns(...$columns)
This will add the named columns to the result.
Note: You need to pass the name in the format you would access it on the model or array. example: in the db:
last_name, on the modellastname-> showColumns('lastname')
You can provide as many names as you like
searchColumns(..$fields)
Will enable the table to allow search only in the given columns. Please note that a collection behaves different to a builder object.
Note: If you want to search on number columns with the query engine, then you can also pass a search column like this
//mysql
->searchColumns(array('id:char:255', 'first_name', 'last_name', 'address', 'email', 'age:char:255'))
//postgree
->searchColumns(array('id:text', 'first_name', 'last_name', 'address', 'email', 'age:text'))This will cast the columns int the given types when searching on this columns
orderColumns(..$fields)
Will enable the table to allow ordering only in the given columns. Please note that a collection behaves different to a builder object.
addColumn($name, $function)
Will add a custom field to the result set, in the function you will get the whole model or array for that row E.g.:
Datatable::collection(User::all())
->addColumn('name',function($model)
{
return $model->author->name;
}
)->make();You can also just add a predefined Column, like a DateColumn, a FunctionColumn, or a TextColumn E.g.:
…You can also overwrite the results returned by the QueryMethod by using addColumn in combination with showColumns. You must name the column exactly like the database column that you're displaying using showColumns in order for this to work.
$column = new \Chumper\Datatable\Columns\FunctionColumn('foo', function ($row) { return strtolower($row->foo); }
Datatable::query(DB::table('table')->select(array('foo')))
->showColumns('foo')
->addColumn($column)
->orderColumns('foo')
->searchColumns('foo')
->make()This will allow you to have sortable and searchable columns using the QueryEngine while also allowing you to modify the return value of that database column entry.
Eg: linking an user_id column to it's page listing
$column = new \Chumper\Datatable\Columns\FunctionColumn('user_id', function ($row) { return link_to('users/'.$row->user_id, $row->username) }
Datatable::query(DB::table('table')->select(array('user_id', 'username')))
->showColumns('user_id')
->addColumn($column)
->orderColumns('user_id')
->searchColumns('user_id')Please look into the specific Columns for further information.
**setAliasMap
No open issues yet, or sync has not completed.