Skip to content

Commit 7f33caa

Browse files
Add twig to table (#12)
* add twig to table * fix typos in readme * improve readme
1 parent dd3a9b3 commit 7f33caa

File tree

5 files changed

+85
-20
lines changed

5 files changed

+85
-20
lines changed

README.md

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Used bootstrap-table version 1.18.3.
66

77
Inspired by [SgDatatablesBundle](https://github.com/stwe/DatatablesBundle).
88

9-
**The project is currently still under development. It can not be excluded that configuration changes.**
10-
119
## Overview
1210

1311
1. [Features](#features)
@@ -26,25 +24,27 @@ Inspired by [SgDatatablesBundle](https://github.com/stwe/DatatablesBundle).
2624
6. [Configuration](#configuration)
2725
1. [Table Dataset Options](#table-dataset-options)
2826
2. [Table Options](#table-options)
29-
7. [Custom Doctrine Queries](#custom-doctrine-queries)
27+
7. [Common Use-Cases](#common-use-cases)
28+
1. [Custom Doctrine Queries](#custom-doctrine-queries)
29+
2. [Detail View](#detail-view)
30+
8. [Contributing](#contributing)
3031

3132
## Features
3233

3334
* Create bootstrap-tables in PHP
3435
* Twig render function
35-
* global filtering*
36+
* global filtering (server side)
3637
* column based filtering (advanced search)
37-
* column sorting*
38-
* Pagination*
38+
* column sorting (server side)
39+
* Pagination (service side)
3940
* different column types
4041
* bootstrap-table extensions
4142
* [sticky-header](https://bootstrap-table.com/docs/extensions/sticky-header/)
4243
* [export](https://bootstrap-table.com/docs/extensions/export/)
4344
* [page-jump-to](https://bootstrap-table.com/docs/extensions/page-jump-to/)
4445
* [toolbar](https://bootstrap-table.com/docs/extensions/toolbar/) with [advanced-search](https://bootstrap-table.com/docs/extensions/toolbar/#advancedsearch)
45-
* more in progress...
4646

47-
*server-side
47+
4848

4949
## Installation
5050

@@ -592,6 +592,11 @@ Table Dataset are provided directly to the `bootstrap-table` as data-attributes
592592
| show-export | bool | true |
593593
| export-types | string | "['csv', 'txt'', 'excel']" |
594594
| export-options | array | see under table* |
595+
| detail-view | bool | false |
596+
| detail-formatter | string | "" |
597+
| detail-view-align | string | "" |
598+
| detail-view-icon | bool | true |
599+
| detail-view-by-click | bool | false |
595600
| sticky-header | bool | true |
596601
| sticky-header-offset-left | int | 0 |
597602
| sticky-header-offset-right | int | 0 |
@@ -732,11 +737,13 @@ hello_bootstrap_table:
732737
733738
734739
735-
## Custom Doctrine Queries
740+
## Common Use-Cases
741+
742+
### Custom Doctrine Queries
736743
737744
Sometimes you don't want to display all the data in a database table. For this you can "prefilter" the Doctrine query.
738745
739-
### Example
746+
#### Example
740747
741748
```php
742749
/**
@@ -763,11 +770,53 @@ public function index(Request $request, HelloBootstrapTableFactory $tableFactory
763770
}
764771
```
765772

773+
### Detail View
774+
775+
You can expand rows in bootstrap-table. This option is called "detail view" and can be enabled in the datasets (by default this is disabled). For displaying the content of detail-view a formatter is needed (also to be specified in datasets). In the formatter you have access to the data of the table row. For complex representations Twig can also be used. See the example below.
776+
777+
```php
778+
protected function buildColumns(ColumnBuilder $builder, $options)
779+
{
780+
//enable detail-view and set formatter
781+
$this->setTableDataset(array(
782+
'detail-view' => true,
783+
'detail-formatter' => 'detailViewFormatter'
784+
));
785+
786+
$builder
787+
// other columns ...
788+
789+
// detailView is not a database field and can be named as you like.
790+
// but the column should not displayed in the table (HiddenColumn)
791+
->add('detailView', HiddenColumn::class, array(
792+
// override data callback (as attribute you can access the entity that you specified in getEntityClass())
793+
'data' => function (User $user) {
794+
// now you can return everthing you want (twig render included)
795+
// twig is provided by HelloBootstrapTable
796+
return $this->twig->render('user/detail_view.html.twig', array(
797+
'user' => $user
798+
));
799+
}
800+
));
801+
}
802+
```
803+
804+
To display `detailView` as content of the expanded table row a formatter function must be created and `detailView` must be returned. Remember to create the formatter before calling `{{ hello_bootstrap_table_js() }}`.
805+
806+
```javascript
807+
// index => index of row inside table
808+
// row => data object of the row
809+
// element => row DOM element
810+
window.detailViewFormatter = function (index, row, element) {
811+
// detailView matched with the name from add('detailView', HiddenColumn::class). If you use a different name you must changed it here too.
812+
return row.detailView;
813+
};
814+
```
815+
816+
Alternative you can of course create your HTML with JavaScript inside the formatter.
817+
818+
766819

820+
## Contributing
767821

768-
## ToDo's
769-
* Documentation
770-
* Cookie Extension
771-
* More Examples
772-
* Tests
773-
* Unit Tests
822+
Contributions are **welcome** and will be credited.

src/DependencyInjection/HelloBootstrapTableExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public function load(array $configs, ContainerBuilder $container)
2727
}
2828

2929
$definition = $container->getDefinition('hello_sebastian_hello_bootstrap_table.hello_bootstrap_table_factory');
30-
$definition->setArgument(2, $config);
30+
$definition->setArgument(3, $config);
3131
}
3232

33-
3433
}

src/HelloBootstrapTable.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\HttpFoundation\Request;
1414
use Symfony\Component\OptionsResolver\OptionsResolver;
1515
use Symfony\Component\Routing\RouterInterface;
16+
use Twig\Environment;
1617

1718
abstract class HelloBootstrapTable
1819
{
@@ -21,6 +22,11 @@ abstract class HelloBootstrapTable
2122
*/
2223
protected $router;
2324

25+
/**
26+
* @var Environment
27+
*/
28+
protected $twig;
29+
2430
/**
2531
* @var EntityManagerInterface
2632
*/
@@ -67,12 +73,14 @@ abstract class HelloBootstrapTable
6773
*
6874
* @param RouterInterface $router
6975
* @param EntityManagerInterface $em
76+
* @param Environment $twig
7077
* @param array $options
7178
* @param array $defaultOptions
7279
*/
73-
public function __construct(RouterInterface $router, EntityManagerInterface $em, $options, $defaultOptions = array())
80+
public function __construct(RouterInterface $router, EntityManagerInterface $em, Environment $twig, $options, $defaultOptions = array())
7481
{
7582
$this->router = $router;
83+
$this->twig = $twig;
7684
$this->_em = clone $em;
7785
$this->defaultOptions = $defaultOptions;
7886

src/HelloBootstrapTableFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\ORM\EntityManagerInterface;
88
use Symfony\Component\Routing\RouterInterface;
9+
use Twig\Environment;
910

1011
class HelloBootstrapTableFactory
1112
{
@@ -19,17 +20,23 @@ class HelloBootstrapTableFactory
1920
*/
2021
private $em;
2122

23+
/**
24+
* @var Environment
25+
*/
26+
private $twig;
27+
2228
/**
2329
* YAML config options.
2430
*
2531
* @var array
2632
*/
2733
private $defaultConfig;
2834

29-
public function __construct(RouterInterface $router, EntityManagerInterface $em, $defaultConfig = array())
35+
public function __construct(RouterInterface $router, EntityManagerInterface $em, Environment $twig, $defaultConfig = array())
3036
{
3137
$this->router = $router;
3238
$this->em = $em;
39+
$this->twig = $twig;
3340
$this->defaultConfig = $defaultConfig;
3441
}
3542

@@ -55,6 +62,7 @@ public function create($helloTable, $options = array())
5562
return new $helloTable(
5663
$this->router,
5764
$this->em,
65+
$this->twig,
5866
$options,
5967
$this->defaultConfig
6068
);

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class="HelloSebastian\HelloBootstrapTableBundle\HelloBootstrapTableFactory" public="true">
99
<argument type="service" id="router" on-invalid="null"/>
1010
<argument type="service" id="doctrine.orm.entity_manager" on-invalid="null"/>
11+
<argument type="service" id="twig" on-invalid="null"/>
1112
</service>
1213

1314
<service id="HelloSebastian\HelloBootstrapTableBundle\HelloBootstrapTableFactory"

0 commit comments

Comments
 (0)