项目作者: warslett

项目描述 :
table building, table abstraction, table rendering
高级语言: PHP
项目地址: git://github.com/warslett/table-builder.git
创建时间: 2020-11-21T17:55:23Z
项目社区:https://github.com/warslett/table-builder

开源协议:MIT License

下载


Table Builder

Latest Stable Version
Build Status
codecov
Mutation testing badge
Psalm coverage
Total Downloads
License: MIT

Table builder provides table abstraction, table building and table rendering. Allowing you to configure your tables,
load your data into them and then render them in a variety of ways. The package can help you implement functionality
common to most table actions in CRUD applications including pagination, sorting, row actions, conditional formatting,
and exporting the table to csv.

Installation

composer require warslett/table-builder

If you are using symfony there is an optional bundle that will configure the services:

composer require warslett/table-builder-bundle warslett/table-builder

Requirements

PHP 7.4, 8.0 or 8.1.

Documentation

Full documentation available here.

Overview

Table Building

Configure your tables using a variety of column types or implement your own column types. Then load data into the table
using one of our data adapters or implement your own. Handle a request to apply sorting and pagination using one of our
request adapters or implement your own.

  1. // Configure the table structure with a range of out the box column types
  2. $tableBuilder = $this->tableBuilderFactory->createTableBuilder()
  3. ->rowsPerPageOptions([10, 20, 50])
  4. ->defaultRowsPerPage(10)
  5. ->add(TextColumn::withProperty('email')
  6. ->sortable())
  7. ->add(DateTimeColumn::withProperty('last_login')
  8. ->format('Y-m-d H:i:s')
  9. ->sortable())
  10. ->add(ActionGroupColumn::withName('actions')
  11. ->add(ActionBuilder::withName('update')
  12. ->route('user_update', ['id' => 'id'])) // map 'id' parameter to property path 'id'
  13. ->add(ActionBuilder::withName('delete')
  14. ->route('user_delete', ['id' => 'id'])
  15. ->attribute('extra_classes', ['btn-danger'])));
  16. // Build the table object
  17. $table = $tableBuilder->buildTable('users');
  18. // Configure how data will be loaded into the table
  19. $queryBuilder = $this->entityManager->createQueryBuilder()
  20. ->select('u')
  21. ->from(User::class, 'u');
  22. $dataAdapter = DoctrineOrmAdapter::withQueryBuilder($queryBuilder)
  23. ->mapSortToggle('email', 'u.email')
  24. ->mapSortToggle('last_login', 'u.lastLogin');
  25. $table->setDataAdapter($dataAdapter);
  26. // Uses parameters on the request to load data into the table with sorting and pagination
  27. $table->handleSymfonyRequest($request);
  28. // OR with a Psr7 Request
  29. $table->handlePsr7Request($request);

Table Rendering

Modeling tables in an abstract way allows us to provide a variety of generic renderers for rendering them.

For example, with the TwigRendererExtension registered you can render the table in a twig template like this:

  1. <div class="container">
  2. {{ table(table) }}
  3. </div>

Or if you aren’t using twig you can use the PhtmlRenderer which uses plain old php templates and has 0 third party
dependencies:

  1. use WArslett\TableBuilder\Renderer\Html\PhtmlRenderer;
  2. $renderer = new PhtmlRenderer();
  3. echo $renderer->renderTable($table);

Both of the above renderers are themeable and are available with a standard theme and bootstrap4 theme out the box.

rendered table

You can also render tables as CSV documents:

  1. use League\Csv\Writer;
  2. use WArslett\TableBuilder\Renderer\Csv\CsvRenderer;
  3. $csvRenderer = new CsvRenderer();
  4. $csvRenderer->renderTable($table, Writer::createFromPath('/tmp/mycsv.csv'));

Single Page Applications

Tables also implement JsonSerializable so they can be encoded as json in a response and consumed by a single page
application.

  1. // GET /users/table
  2. return new JsonResponse($table);

Dependencies" class="reference-link">Dependencies

Table builder has minimal core dependencies however some optional features have additional dependencies.

  • CsvRenderer and related classes depends on league/csv
  • TwigRenderer and related classes depends on twig/twig
  • DoctrineORMAdapter data adapter depends on doctrine/orm
  • SymfonyHttpAdapter response adapter depends on symfony/http-foundation
  • Psr7Adapter response adapter depends on psr/http-message
  • SymfonyRoutingAdapter route generator adapter depends on symfony/routing