uvoelkel/serverside-datatables-bundle
Composer 安装命令:
composer require uvoelkel/serverside-datatables-bundle
包简介
DataTables integration for Symfony
关键字:
README 文档
README
What it does
The DataTablesBundle let's you easily create (sortable and filterable) serverSide DataTables from Doctrine entities.
License
This bundle is under the MIT license. See the complete license in the bundle:
LICENSE
Installation
Enable the bundle by adding the following line in the app/AppKernel.php
file of your project:
# app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Voelkel\DataTablesBundle\VoelkelDataTablesBundle(), ); // ... } // ... }
Configuration
After installing the bundle, make sure you add this route to your routing:
# app/config/routing.yml
datatables:
resource: "@VoelkelDataTablesBundle/Resources/config/routing.xml"
Localization
# app/config/config.yml
voelkel_data_tables:
localization:
locale: "%locale%"
data:
true: "Jepp"
false: "Nope"
datetime: "Y-m-d H:i:s"
Usage
Create a Table definition
# AppBundle/DataTable/CustomerTable.php <?php namespace AppBundle\DataTable; use Voelkel\DataTablesBundle\Table\AbstractDataTable; use Voelkel\DataTablesBundle\Table\TableBuilderInterface; use Voelkel\DataTablesBundle\Table\TableOptions; use Voelkel\DataTablesBundle\Table\TableSettings; use Voelkel\DataTablesBundle\Table\Column\Column; use Voelkel\DataTablesBundle\Table\Column\UnboundColumn; use Voelkel\DataTablesBundle\Table\Column\CallbackColumn; use Voelkel\DataTablesBundle\Table\Column\EntityColumn; use Voelkel\DataTablesBundle\Table\Column\EntitiesColumn; class CustomerTable extends AbstractDataTable { protected function configure(TableSettings $settings, TableOptions $options) { $settings->setName('customer'); $settings->setEntity('App\Entity\Customer'); $options['stateSave'] = true; } protected function build(TableBuilderInterface $builder) { $builder ->add('id') ->add('firstname') ->add('lastname', Column::class, [ 'label' => 'Lastname' ]) ->add('opening', UnboundColumn::class, [ 'callback' => function(Customer $customer) { return 'Dear ' . ('f' === $customer->getGender() ? 'Madam' : 'Sir'); } ]) ->add('status', CallbackColumn::class, [ 'callback' => function($status) { switch ($status) { case 1: return 'something'; break; case 2: return 'something else'; break; default: return 'invalid'; break; } ]) ->add('group.name')) // customer has one group ->addColumn(new EntityColumn('state', 'city.state', 'name')) // customer has one city. city has one state ->addColumn(new EntitiesColumn('orders', 'orders', 'number')) // customer has many orders ->addColumn(new EntitiesCountColumn('addresses_count', 'addresses')) // customer has many addresses ->addColumn('actions', ActionsColumn::class, [ 'actions' => [ 'edit' => [ 'title' => 'edit customer', 'label' => '<i class="fa fa-edit"></i>', 'callback' => function(Customer $customer, \Symfony\Component\Routing\RouterInterface $router) { return $router->generate('customer_edit', ['id' => $customer->getId()]); }, ], ], 'dropdown' => true, 'dropdown_label' => 'Actions', ]) ; } }
In your CustomerController
# AppBundle/Controller/CustomerController.php use AppBundle\DataTable\CustomerTable; class CustomerController extends Controller { public function indexAction() { return $this->render('AppBundle:Customer:index.html.twig', [ 'table' => new CustomerTable(), ]); } }
And in your index template
# AppBundle/Resources/views/Customer/index.html.twig
{% extends '::base.html.twig' %}
{% block body %}
{{ datatables_html(table) }}
{% endblock %}
{% block javascripts %}
<script>
{{ datatables_js(table) }}
// access the table instance
{{ table.name }}_table.on('dblclick', 'tbody tr', function () {
alert('dblclick');
});
</script>
{% endblock %}
Access DI container
use Voelkel\DataTablesBundle\Table\AbstractDataTable; class CustomerTable extends AbstractDataTable { protected function build() { $service = $this->container->get('service'); // or short $service = $this->get('service'); // ... ->addColumn(new Column('id', 'id', [ 'format_data_callback' => function($data, $object, Column $column) { $router = $this->container->get('router'); // or $router = $this->get('router'); }, ])) // ... } }
Table definition as a service
Define the service
# AppBundle/Resources/config/services.xml
<service id="app.table.customer" class="AppBundle\DataTable\CustomerTable">
<argument type="service" id="my.awesome.service" />
</service>
Set the service id in the table constructor
# AppBundle/DataTable/CustomerTable.php private $myAwesomeService; public function __construct($myAwesomeService) { $this->myAwesomeService = $myAwesomeService; } protected function configure(TableSettings $settings, TableOptions $options) { $settings->setName('customer'); $settings->setEntity('AppBundle\Entity\Customer'); $settings->setServiceId('app.table.customer'); }
In your controller
# AppBundle/Controller/CustomerController.php public function indexAction() { return $this->render('AppBundle:Customer:index.html.twig', [ 'table' => $this->get('app.table.customer'), ]); }
Column filter
# AppBundle/DataTable/CustomerTable.php // ... class CustomerTable extends AbstractDataTable { // ... protected function build() { $this // ... ->add('gender', Column:class, [ 'filter' => 'select', 'filter_choices' => [ 'm' => 'male', 'f' => 'female', ], ]) ->add('lastname', Column::class, [ 'filter' => 'text', ]) ; } }
Table options
$default = [ 'stateSave' => false, 'stateDuration' => 7200, ];
- 'stateDuration': -1 sessionStorage. 0 or greater localStorage. 0 infinite. > 0 duration in seconds
class CustomerTable extends AbstractTableDefinition { // ... protected function configure(TableSettings $settings, TableOptions $options) { $options['stateSave'] = true; $options['stateDuration'] = 120; } }
Column options
$default = [ 'sortable' => true, 'searchable' => true, 'filter' => false, // false|'text'|'select' 'filter_choices' => [], 'filter_empty' => false, // add a checkbox to filter empty resp. null values 'multiple' => false, 'expanded' => false, 'format_data_callback' => null, // function ($data, $object, Column $column) {} 'unbound' => false, 'order' => null, // null|'asc'|'desc' 'label' => null, // null|string|false 'abbr' => null, // null|string ];
- 'filter' != false implies 'searchable' = true
- 'multiple' has no effect if filter != 'select'
- 'expanded' has no effect if filter != 'select'
uvoelkel/serverside-datatables-bundle 适用场景与选型建议
uvoelkel/serverside-datatables-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.71k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「datatables」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 uvoelkel/serverside-datatables-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 uvoelkel/serverside-datatables-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 uvoelkel/serverside-datatables-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table.
Laravel DataTables plugin to support Mongodb
Plug-ins for DataTables
Vanilla Components Integration with Laravel
Views for the package MedicOneSystems Livewire Datatables with Bootstrap 4
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/
统计信息
- 总下载量: 2.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-16