MoneyColumnType
The MoneyColumnType
represents a column with monetary value, appropriately formatted and rendered with currency sign.
Options
currency
- type:
string
orcallable
- any 3-letter ISO 4217 code
Specifies the currency that the money is being specified in. This determines the currency symbol that should be shown in the column.
When using the Intl number formatter, the ISO code will be automatically converted to the appropriate currency sign, for example:
EUR
becomes€
;PLN
becomeszł
;
Please note that the end result is also dependent on the locale used in the application, for example, with value of 1000
:
USD
currency will be rendered as$1,000.00
when using theen
locale;USD
currency will be rendered as1 000,00 USD
when using thepl
locale;
When the Intl formatter is NOT used, given currency is simply rendered after the raw value, e.g. 1000 USD
.
Additionally, the option accepts a callable, which gets a row data as first argument:
$builder
->addColumn('price', MoneyColumnType::class, [
'currency' => fn (Product $product) => $product->getPriceCurrency(),
])
;
divisor
- type:
integer
- default:
1
If you need to divide your starting value by a number before rendering it to the user, you can use the divisor
option. For example if you need to show amounts as integer in order to avoid rounding errors, you can transform values in cents automatically:
$builder
->addColumn('price', MoneyColumnType::class, [
'divisor' => 100,
])
;
In this case, if the price field is set to 9950, then the value 99.5 will actually be rendered to the user.
use_intl_formatter
- type:
bool
- default:
true
ifsymfony/intl
is installed,false
instead
Determines whether the Intl number formatter should be used. Enabling this option will automatically handle the formatting based on the locale set in the application. For example, value 123456.78
will be rendered differently:
123,456.78
when usingen
locale;123 456,78
when usingpl
locale;- etc.
intl_formatter_options
- type:
array
- default:
['attrs' => [], 'style' => 'decimal']
Configures the Intl number formatter if used. For example, to limit decimal places to two:
$builder
->addColumn('price', MoneyColumnType::class, [
'intl_formatter_options' => [
'attrs' => [
// https://www.php.net/manual/en/class.numberformatter.php#numberformatter.constants.max-fraction-digits
'max_fraction_digit' => 2,
],
])
])
;
For more details, see:
Inherited options
label
- type:
null
,string
orSymfony\Component\Translation\TranslatableInterface
- default:
null
Sets the label that will be used in column header and personalization column list.
When value equals null
, a sentence cased column name is used as a label, for example:
Column name | Guessed label |
---|---|
name | Name |
firstName | First name |
header_translation_domain
- type:
false
orstring
- default:
'KreyuDataTable'
Sets the translation domain used when translating the column header.
Setting the option to false
disables its translation.
header_translation_parameters
- type:
array
- default:
[]
Sets the parameters used when translating the column header.
value_translation_domain
- type:
false
orstring
- default: inherited from the data table translation domain
Sets the translation domain used when translating the column value.
Setting the option to false
disables its translation.
property_path
- type:
null
,false
orstring
- default:
null
Sets the property path used by the PropertyAccessor to retrieve column value of each row.
Setting the option to false
disables property accessor.
$builder
->addColumn('category', TextColumnType::class, [
'property_path' => 'category.name',
])
;
When value equals null
, the column name is used as a property path.
getter
- type:
null
orcallable
- default:
null
When provided, this callable will be invoked to read the value from the underlying object that will be used within the column. This disables the usage of the PropertyAccessor, described in the property_path option.
$builder
->addColumn('category', TextColumnType::class, [
'getter' => fn (Product $product) => $product->getCategory(),
])
;
sort
- type:
bool
orstring
- default:
false
- the sortable behavior is disabled
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;false
- disables column sorting;- string - defines sort property path;
block_prefix
- type:
string
- default: value returned by the column type
getBlockPrefix()
method
Allows you to add a custom block prefix and override the block name used 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 some of them, without the need to create a new column type.
formatter
- type:
null
orcallable
- default:
null
Formats the value to the desired string.
$builder
->addColumn('quantity', NumberColumnType::class, [
'formatter' => function (float $value, Product $product, ColumnInterface $column, array $options) {
return number_format($value, 2) . $product->getUnit();
},
])
;
export
- type:
bool
orarray
- default:
[]
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:
$builder
->addColumn('quantity', NumberColumnType::class, [
'label' => 'Quantity',
'translation_domain' => 'product',
'export' => [
'label' => 'Qty',
// rest of the options are inherited, therefore "translation_domain" equals "product", etc.
],
])
;
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.
header_attr
- type:
array
- default:
[]
If you want to add extra attributes to an HTML column header representation (<th>
) you can use the attr option. It's an associative array with HTML attributes as keys. This can be useful when you need to set a custom class for a column:
$builder
->addColumn('quantity', NumberColumnType::class, [
'header_attr' => [
'class' => 'text-end',
],
])
;
value_attr
- type:
array
orcallable
- default:
[]
If you want to add extra attributes to an HTML column value representation (<td>
) you can use the attr option. It's an associative array with HTML attributes as keys. This can be useful when you need to set a custom class for a column:
$builder
->addColumn('quantity', NumberColumnType::class, [
'value_attr' => [
'class' => 'text-end',
],
])
;
You can pass a callable
to perform a dynamic attribute generation:
$builder
->addColumn('quantity', NumberColumnType::class, [
'value_attr' => function (int $quantity, Product $product) {
return [
'class' => $quantity === 0 && !$product->isDisabled() ? 'text-danger' : '',
],
},
])
;
priority
- type:
integer
- default:
0
Columns are rendered in the same order as they are included in the data table. This option changes the column rendering priority, allowing you to display columns earlier or later than their original order.
The higher this priority, the earlier the column will be rendered. Priority can albo be negative and columns with the same priority will keep their original order.
Note: column priority can be changed by the personalization feature.
visible
- type:
bool
- default:
true
Determines whether the column is visible to the user.
Note: column visibility can be changed by the personalization feature.
personalizable
- type:
bool
- default:
true
Determines whether the column is personalizable. The non-personalizable columns are not modifiable by the personalization feature.