Skip to content

Commit 450eda2

Browse files
committed
Improve documentation and tests
1 parent eeef82e commit 450eda2

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

docs/source/index.html.md

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Recommended way of installing this library is through [Composer](https://getcomp
2525

2626
<code>composer require omines/datatables-bundle</code>
2727

28-
Please ensure you are using Symfony 3.3 or later. Symfony Flex bindings are on their way.
28+
Please ensure you are using Symfony 3.3 or later. If you are using Symfony Flex a recipe is included in the contrib
29+
repository, providing automatic installation and configuration.
2930

3031
```php?start_inline=true
3132
public function registerBundles()
@@ -215,7 +216,7 @@ tables in your site.
215216

216217
TBD.
217218

218-
## Implementing your own
219+
## Implementing custom adapters
219220

220221
TBD.
221222

@@ -224,19 +225,74 @@ TBD.
224225
Column classes derive from `AbstractColumn`, and implement the transformations required to convert
225226
raw data into output ready for rendering in a DataTable.
226227

228+
A number of standard columns are provided for common use cases, but you can easily add your own column
229+
types for application specific purposes.
230+
231+
### Common options
232+
233+
```php?start_inline=1
234+
# Some example columns
235+
$table
236+
->add('firstName', TextColumn::class, ['label' => 'customer.name', 'className' => 'bold'])
237+
->add('lastName', TextColumn::class, ['render' => '<strong>%s</strong>', 'raw' => true])
238+
->add('email', TextColumn::class, ['render' => function($value, $context) {
239+
return sprintf('<a href="%s">%s</a>', $value, $value);
240+
})
241+
;
242+
```
243+
244+
All column types have the following options:
245+
246+
Option | Type | Description
247+
------ | ---- | -----------
248+
label | string | Basic translation label shown in the header of the table. Defaults to the name of the column.
249+
data | string/callable/`null` | The default value if a `null` value is encountered, or a callable function to transform data.
250+
field | string/`null` | A field mapping to be used by adapters to fill data.
251+
propertyPath | string/`null` | A property path to be applied to the raw adapter row data.
252+
visible | bool | Whether the column will be visible. Default true.
253+
orderable | bool/`null` | Whether the column can be sorted upon. Defaults to the presence of the `orderField`.
254+
orderField | string/`null` | The field to order by when the column is sorted. Defaults to the value of `field`.
255+
searchable | bool/`null` | Whether the column can be searched upon. Defaults to the presence of `field`.
256+
globalSearchable | bool/`null` | Whether the column participates in global searches. Defaults to the presence of `field`.
257+
className | string/`null` | A CSS class to be applied to all cells in this column.
258+
render | string/callable/`null` | Either a [`sprintf` compatible format string](http://php.net/manual/en/function.sprintf.php), or a callable function providing rendering conversion, or default `null`.
259+
227260
## TextColumn
228261

229-
TBD.
262+
```php?start_inline=1
263+
$table->add('customerName', TextColumn::class, ['field' => 'customer.name']);
264+
```
265+
266+
Text columns are the most frequently used column type, as they can be used to display any kind of data
267+
that is eventually rendered as plain text.
268+
269+
The `TextColumn` type exposes a single option on top of its ancestor `AbstractColumn`:
270+
271+
Option | Type | Description
272+
------ | ---- | -----------
273+
raw | bool | Do not escape cell content to be safe for use in HTML. Default `false`.
230274

231275
## DateTimeColumn
232276

233-
TBD.
277+
```php?start_inline=1
278+
$table->add('registrationDate', DateTimeColumn::class, ['format' => 'd-m-Y']);
279+
```
280+
281+
DateTime columns render a `\DateTimeInterface` implementing class, such as `\DateTime`, to a string
282+
result. If data of other types is encountered automatic conversion is attempted following [common PHP formats](http://php.net/manual/en/datetime.formats.php).
283+
284+
Option | Type | Description
285+
------ | ---- | -----------
286+
format | string | A date format string as accepted by the [`date()`](http://php.net/manual/en/function.date.php) function. Default `'c'`.
287+
288+
## Implementing custom columns
289+
234290

235291
# DataTable Types
236292

237293
```php?start_inline=1
238-
$table = $this->createDataTableFromType(PresidentsTableType::class)
239-
->handleRequest($request);
294+
$table = $this->createDataTableFromType(PresidentsTableType::class)
295+
->handleRequest($request);
240296
```
241297

242298
Having the table configuration in your controller is convenient, but not practical for reusable or
@@ -253,3 +309,13 @@ dynamically. When using Symfony's autoconfiguration the tag will be applied auto
253309
Of course you can modify the base type to fit the controller's specific needs before calling
254310
`handleRequest`. Secondly, the `createDataTableFromType` function accepts an array as a second
255311
argument which is passed to the type class for parametrized instantiation.
312+
313+
# Javascript
314+
315+
TBD.
316+
317+
# Legal
318+
319+
This software was developed for internal use at [Omines Full Service Internetbureau](https://www.omines.nl/)
320+
in Eindhoven, the Netherlands. It is shared with the general public under the permissive MIT license, without
321+
any guarantee of fitness for any particular purpose. Refer to the included `LICENSE` file for more details.

tests/Fixtures/AppBundle/DataTable/Type/CustomQueryTableType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@ public function configure(DataTable $dataTable, array $options)
5353
},
5454
])
5555
;
56+
57+
/** @var ORMAdapter $adapter */
58+
$adapter = $dataTable->getAdapter();
59+
$adapter->addCriteriaProcessor(function() { return Criteria::create()->where(new Comparison('firstName', Comparison::CONTAINS, '3')); });
5660
}
5761
}

tests/Unit/ColumnTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public function testTextColumn()
3737
$this->assertSame('foo', $column->getDataTable()->getName());
3838
}
3939

40-
public function testColumnWithClosureRenderer()
40+
public function testColumnWithClosures()
4141
{
4242
$column = new TextColumn('test', 1, [
43-
'data' => 'bar',
43+
'data' => function ($context, $value) {
44+
return 'bar';
45+
},
4446
'render' => function ($value) {
4547
return mb_strtoupper($value);
4648
},

0 commit comments

Comments
 (0)