bogdanpet/eloquent-datatable
Composer 安装命令:
composer require bogdanpet/eloquent-datatable
包简介
README 文档
README
PHP class for displaying Eloquent collections as HTML table. Tested in Laravel, but should work in any project using eloquent as standalone package.
Installation
Download and require Datatable.php and DatatableActions.php. or install it via composer.
composer require bogdanpet/eloquent-datatable
Usage
Basic usage
For example, let's say that we have collection of users we want to show as a table. Creating collection and sharing it to your view should look like this in Laravel.
public function showUsers() {
$users = User::all();
return view('users', compact('users'));
}
Now, if you are intending to show this collection as HTML table you can do this simply with Datatable class. You can either create new instance or take advantage of laravel's dependency injection.
use Bogdanpet\Datatables\Datatable;
...
public function showUsers(Datatable $datatable) {
$users = User::all();
$datatable->setData($users);
$datatable->setColumns(['id, 'name', 'email']);
return view('users', compact('datatable'));
}
Then, in your blade template, just put table where you want it using show() method.
<div class="table-container">
{!! $datatable->show(); !!}
</div>
If you need to customize table more, you can use separate methods instead of show() method.
<div class="table-container">
{!! $datatable->open(['class' => 'table-responsive']); // Generate opening <table> tag with optional attributes. !!}
{!! $datatable->tableHead(); // Generate table column headings. !!}
{!! $datatable->tableBody(); // Generate main table rows. !!}
{!! $datatable->tableFoot(); // Generate table footer. !!}
{!! $datatable->close(); // Generate closing </table> tag. !!}
{!! $datatable->pagination(); // Generate pagination links if you are using User::paginate() to create collection !!}
</div>
Static usage
For quick building of datatable in your view, if you already have access to collection, you can create table directly with static method make().
<div class="table-container">
{!! \Bogdanpet\Datatables\Datatable::make($users, ['id, 'name', 'email'] !!}
</div>
Datatable actions
For generating action links I provided custom column 'actions'. For example, if you want to create links to edit and delete user from database, you can add 'actions' to columns and then set actions.
$datatable->setColumns(['id, 'name', 'email', 'actions']);
$datatable->setActions([
[ 'Edit', '/user/edit/{id}', ['class' => 'table-action'] ],
[ 'Delete', '/user/delete/{id}', ['class' => 'table-action'] ]
]);
As you can see, actions are set as array of arrays, each array contains link text as first item, route as second and optional third item which is array of link attributes. {id} is wildcard for laravel routing system, and will be replaced with corresponding user's id from the database. You can use wildcard for any database column like {name} or {email}. Example above will generate two links for each user in actions column.
<a href="/user/edit/23" class="table-action">Edit</a>
<a href="/user/delete/23" class="table-action">Delete</a>
Advanced usage
For complete control over your datatable you can extend main Datatable class. For example, Let's create UsersDatatable class.
<?php namespace App\Datatables;
class UsersDatatable extends \Bogdanpet\Datatables\Datatable
{
/**
* DatatableActions trait allows use of actions.
*/
use \Bogdanpet\Datatables\DatatableActions;
/**
* Define columns to be displayed by datatable.
*/
protected $columns = [
'row_num',
'name',
'email',
'actions'
];
/**
* Define datatable actions.
*/
protected $actions = [
[ 'Edit', '/user/edit/{id}', ['class' => 'table-action'] ],
[ 'Delete', '/user/delete/{id}', ['class' => 'table-action'] ]
]
}
After class is created, it can be used same as main class. Just no need for defining columns and actions. Main data can also be defined, but it is not recommended because of its dynamic behavior.
use App\Datatables\UsersDatatable;
...
public function showUsers(UsersDatatable $users_datatable) {
$users = User::all();
$users_datatable->setData($users);
return view('users', compact('users_datatable'));
}
And, same as in first example, just place the datatable where you want to.
<div class="table-container">
{!! $users_datatable->show(); !!}
</div>
'row_num', like 'actions', is predefined custom column which generate number of row in a table. You can also add your own custom columns or customize existing ones using 'th' and 'td' methods. Let's add new custom column 'registered_at' and customize 'name' column in our UsersDatatable class above.
<?php namespace App\Datatables;
class UsersDatatable extends \Bogdanpet\Datatables\Datatable
{
/**
* DatatableActions trait allows use of actions.
*/
use \Bogdanpet\Datatables\DatatableActions;
/**
* Define columns to be displayed by datatable.
*/
protected $columns = [
'row_num',
'name',
'email',
'registered_at',
'actions'
];
/**
* Define datatable actions.
*/
protected $actions = [
[ 'Edit', '/user/edit/{id}', ['class' => 'table-action'] ],
[ 'Delete', '/user/delete/{id}', ['class' => 'table-action'] ]
]
/**
* Tweaks the heading for column 'name'
*/
public function thName()
{
return $this->th('Name', 'table-heading-large'); // th() generate <th> element, parameters are content, and optional classes.
}
/**
* Tweaks the heading for column 'registered_at'
*/
public function thRegisteredAt()
{
return $this->th('Registered', 'table-heading-small'); // th() generate <th> element, parameters are content, and optional classes.
}
/**
* Tweaks the display of 'name' name column
*/
public function tdName($model)
{
// Customize 'name' column to display names with capitalized letters.
return $this->td( ucwords($model->name), 'table-cell-large'); // td() generate <td> element, parameters are content, and optional classes.
}
/**
* Tweaks the display of 'registered_at' name column
*/
public function tdRegisteredAt($model)
{
// 'registered_at' now displays user's creation date and time in human readable format.
return $this->td( $model->created_at->toDayDateTimeString(), 'table-cell-small');
}
}
bogdanpet/eloquent-datatable 适用场景与选型建议
bogdanpet/eloquent-datatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bogdanpet/eloquent-datatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bogdanpet/eloquent-datatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 40
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-04-30