Skip to content

Commit

Permalink
[Feature] Better and more intuitive column options (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu authored Mar 6, 2023
1 parent 84fa4da commit 9abad53
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 113 deletions.
4 changes: 2 additions & 2 deletions docs/reference/columns/types/boolean.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ The [BooleanType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Colum

### `label_true`

**type**: `string` or `TranslatableMessage` **default**: `'Yes'`
**type**: `string` or `Symfony\Component\Translation\TranslatableMessage` **default**: `'Yes'`

Sets the value that will be displayed if row value is true.

### `label_false`

**type**: `string` or `TranslatableMessage` **default**: `'No'`
**type**: `string` or `Symfony\Component\Translation\TranslatableMessage` **default**: `'No'`

Sets the value that will be displayed if row value is false.

Expand Down
84 changes: 69 additions & 15 deletions docs/reference/columns/types/column.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The [ColumnType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Column

### `label`

**type**: `string` or `TranslatableMessage` **default**: the label is "guessed" from the column name
**type**: `string` or `Symfony\Component\Translation\TranslatableMessage` **default**: the label is "guessed" from the column name

Sets the label that will be used when rendering the column header.

Expand Down Expand Up @@ -34,13 +34,14 @@ Setting the option to `false` disables property accessor (for situations, where

**type**: `bool` or `string` **default**: `false` - the sortable behavior is disabled

Sets the sort field used by the sortable behavior.
Sets the sort field used by the sortable behavior.

Setting the option to `true` enables column sorting and uses the column name as a sort field name.
Setting the option to `false` disables column sorting.

### `block_name`

**type**: `string` **default**: `kreyu_data_table_column_` + column type block prefix
**type**: `string` **default**: `kreyu_data_table_column_` + column type block prefix

Allows you to add a custom block name to the ones used by default to render the column type.
Useful for example if you have multiple instances of the same column type, and you need to personalize the rendering of the columns individually.
Expand All @@ -56,36 +57,89 @@ Useful for example if you have multiple instances of the same column type, and y

### `formatter`

**type**: `callable` **default**: `null`
**type**: `null` or `callable` **default**: `null`

Formats the value to the desired string.

Formats the value retrieved by the property accessor to string:
The value passed as the argument can come either from the `value` option,
or the property accessor after the extraction using the `property_path` option.

```php
$builder
->addColumn('ean', TextType::class, [
'value' => fn (Product $product) => $product->getEan(),
'formatter' => fn (string $value) => trim($value),
])
->addColumn('quantity', TextType::class, [
->addColumn('quantity', NumberType::class, [
// no value specified, so property accessor will retrieve the value of the product "quantity"
'formatter' => fn (float $value) => number_format($value, 2) . 'kg',
])
->addColumn('name', TextType::class, [
// the option accepts callables, not only closures
'formatter' => 'trim',
])
;
```

If you disabled property accessor by setting the `property_path` option to `false`, this is a way to retrieve a value manually:
### `export`

**type**: `bool` or `array` **default**: `[]` with some exceptions on built-in types (e.g. [ActionsType](actions.md))

Determines whether the column should be included in the exports.

This option accepts an array of options available for the column type.
It is used to differentiate options for regular rendering, and excel rendering.

For example, if you wish to display quantity column with "Quantity" label, but export with a "Qty" header:

```php
$builder
->addColumn('fullName', TextType::class, [
'property_path' => false,
'formatter' => fn (User $value) => implode(' ', [$user->name, $user->surname]),
$columns
->add('quantity', NumberType::class, [
'label' => 'Quantity',
'translation_domain' => 'product',
'export' => [
'label' => 'Qty',
// rest of the options are inherited, therefore "translation_domain" equals "product", etc.
],
])
;
```

Because property accessor is not called, the value passed as the first argument is a "raw" row value (and for most cases it will be an entity).
Rest of the options are inherited from the column options.

Setting this option to `true` automatically copies the column options as the export column options.
Setting this option to `false` excludes the column from the exports.

### `non_resolvable_options`

### `exportable`
**type**: `array` **default**: `[]`

Because some column options can be an instance of `\Closure`, the bundle will automatically
call them, passing column value, data, whole column object and array of options, as the closure arguments.

This process is called "resolving", and the [formatter](#formatter) and [value](#value) options are excluded from the process.
Because it may be possible, that the user does **not** want to get an option resolved (not call the closure at all),
it is possible to pass the option name to this array, to exclude it from the resolving process.

For example:

```php
$columns
->add('id', CustomType::class, [
'uniqid' => function (string $prefix) {
return uniqid($prefix);
},
'non_resolvable_options' => [
'uniqid',
],
])
;
```

**type**: `bool` **default**: `true`
The `uniqid` option will be available in the column views as a callable. For example, in templates:

If this value is true, the column will be included in the export results.
```twig
{% block kreyu_data_table_column_custom %}
{{ value }} ({{ uniqid('product_') }})
{% endblock %}
```
9 changes: 4 additions & 5 deletions docs/reference/columns/types/link.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ The [LinkType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Column/T

### `href`

**type**: `string` or `callable` **default**: `'#'`
**type**: `string` or `\Closure` **default**: `'#'`

Sets the value that will be used as a link `href` attribute (see [href attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-href)).
Callable can be used to provide an option value based on a row value, which is passed as a first argument.
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

```php
$columns
->add('category', LinkType::class, [
'property_path' => false,
'value' => function (Category $category): string {
return $category->getName(),
},
Expand All @@ -29,10 +28,10 @@ $columns

### `target`

**type**: `string` or `callable` **default**: `'_self'`
**type**: `string` or `\Closure` **default**: `'_self'`

Sets the value that will be used as an anchor `target` attribute (see [target attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target)).
Callable can be used to provide an option value based on a row value, which is passed as a first argument.
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

### `display_icon`

Expand Down
8 changes: 4 additions & 4 deletions docs/reference/columns/types/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ The [TemplateType](https://github.com/Kreyu/data-table-bundle/blob/main/src/Colu

### `template_path`

**type**: `string` or `callable`
**type**: `string` or `\Closure`

Sets the path to the template that should be rendered.
Callable can be used to provide an option value based on a row value, which is passed as a first argument.
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

### `template_vars`

**type**: `string` or `callable` **default**: `'#'`
**type**: `string` or `\Closure` **default**: `'#'`

Sets the variables used within the template.
Callable can be used to provide an option value based on a row value, which is passed as a first argument.
Closure can be used to provide an option value based on a row value, which is passed as a first argument.

## Inherited options

Expand Down
8 changes: 4 additions & 4 deletions src/Bridge/PhpSpreadsheet/Exporter/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ protected function createSpreadsheet(DataTableView $view, array $options = []):
$headersRow = $view->vars['headers_row'];

$columns = array_filter($headersRow->vars['columns'], function (ColumnView $view) {
return $view->vars['exportable'] ?? true;
return false !== $view->vars['export'];
});

$this->appendRow(
$worksheet,
array_map(function (ColumnView $column) {
return $column->vars['label'];
return $column->vars['export']['label'];
}, $columns),
);
}

foreach ($view->vars['values_rows'] as $valuesRow) {
$columns = array_filter($valuesRow->vars['columns'], function (ColumnView $view) {
return $view->vars['exportable'] ?? true;
return false !== $view->vars['export'];
});

$this->appendRow(
$worksheet,
array_map(function (ColumnView $column) {
return $column->vars['exportable_value'];
return $column->vars['export']['value'];
}, $columns),
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Column/Type/ActionsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'exportable' => false,
'export' => false,
'property_path' => false,
'display_personalization_button' => true,
'actions' => function (OptionsResolver $resolver) {
Expand All @@ -24,8 +24,8 @@ public function configureOptions(OptionsResolver $resolver): void
->setDefaults([
'template_vars' => [],
])
->setAllowedTypes('template_path', ['string', 'callable'])
->setAllowedTypes('template_vars', ['array', 'callable'])
->setAllowedTypes('template_path', ['string', \Closure::class])
->setAllowedTypes('template_vars', ['array', \Closure::class])
;
},
])
Expand Down
2 changes: 1 addition & 1 deletion src/Column/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function configureOptions(OptionsResolver $resolver): void
'entry_type' => TextType::class,
'entry_options' => [],
'separator' => ',',
'non_normalizable_options' => [
'non_resolvable_options' => [
'entry_options',
],
])
Expand Down
Loading

0 comments on commit 9abad53

Please sign in to comment.