Formatting
import { Format } from 'cx/util';Cx offers rich support for value formatting.
You may use Format.value(value, format) function to format single values.
Formats are supported in data expressions and string templates.
<div>
<LabelsTopLayout columns={2}>
<NumberField value-bind="$page.value" label="Value" />
<div />
<LabeledContainer label="Max two decimals">
<div text-tpl="{$page.value:n;0;2}" />
</LabeledContainer>
<LabeledContainer label="Compact (1M, 1k)">
<div text-tpl="{$page.value:n;0;4;c}" />
</LabeledContainer>
<LabeledContainer label="Currency (default)">
<div text-tpl="{$page.value:currency}" />
</LabeledContainer>
<LabeledContainer label="Currency USD, two decimals">
<div text-tpl="{$page.value:currency;USD;2}" />
</LabeledContainer>
<LabeledContainer label="Currency EUR, no decimals">
<div text-tpl="{$page.value:currency;EUR;0}" />
</LabeledContainer>
<LabeledContainer label="Accounting format">
<div text-tpl="{$page.value:currency;USD;1;2;+ac}" />
</LabeledContainer>
<LabeledContainer label="Percentage">
<div text-tpl="{$page.value:p;0;2;}" />
</LabeledContainer>
<LabeledContainer label="Percentage sign">
<div text-tpl="{$page.value:ps;0;2;}" />
</LabeledContainer>
</LabelsTopLayout>
<hr />
<LabelsTopLayout columns={2}>
<DateField value-bind="$page.person.dateOfBirth" label="Date of Birth" />
<div />
<LabeledContainer label="MM/dd/yyyy format (default)">
<div text-tpl="{$page.person.dateOfBirth:d}" />
</LabeledContainer>
<LabeledContainer label="M/d/yy format">
<div text-tpl="{$page.person.dateOfBirth:d;yyMd}" />
</LabeledContainer>
<LabeledContainer label="Short day name, M/d/yyyy">
<div text-tpl="{$page.person.dateOfBirth:d;DDDyyyyMd}" />
</LabeledContainer>
<LabeledContainer label="Full day name, MMM dd, yyyy">
<div text-tpl="{$page.person.dateOfBirth:d;DDDDyyyyMMMdd}" />
</LabeledContainer>
<LabeledContainer label="Full day name, MMMM dd, yyyy">
<div text-tpl="{$page.person.dateOfBirth:d;DDDDyyyyMMMMdd}" />
</LabeledContainer>
</LabelsTopLayout>
<hr />
<LabelsTopLayout columns={2} style="margin-top: -10px">
<LabelsTopLayout columns={1}>
<TextField value-bind="$page.person.name" label="Enter your name" maxLength={20} />
<div text-tpl='{[fmt({$page.person.name}, "prefix;Hi :suffix;. Nice to meet you!")]}' />
</LabelsTopLayout>
<LabelsTopLayout columns={1}>
<NumberField value-bind="$page.person.height" label="Enter your height in cm" />
<div text-tpl="Your height is {$page.person.height:suffix; cm.|N/A.}" />
</LabelsTopLayout>
</LabelsTopLayout>
</div>Formatting Rules
Use ; to delimit format parameters.
Use : to use chain multiple formats. Formats are applied from left to right.
Use | to provide null text. Default null text is empty string.
Format Specifiers
| Specifier | Description |
|---|---|
currency | Currency formatting. Formatting is done using the
|
date | Short date format. |
lowercase | Converts text to the lower case. |
number | Number formatting.
|
percentage | Percentage. Please note that the given number will be multiplied with 100. |
string | Default format. Convert any value to string using the |
time | Time of the day formatting. |
uppercase | Converts text to the upper case. |
datetime | Options are used for custom date formatting.
Please check out the intl-io project for more info. |
ellipsis | Shortens long texts.
|
percentSign | Percentage sign. Only percentage sign will be appended to the give number. |
prefix | Prefixes the given value with the provided text.
|
suffix | Appends the provided text to the given value.
|
urlencode | Encodes the given value using the |
wrap | Wraps the given value with into the provided prefix and suffix strings.
|
import { enableCultureSensitiveFormatting } from 'cx/ui';Date, currency and number formats are dependent on an external library and must be enabled before use. This is slightly inconvenient but ensures small bundle sizes for applications that do not use this feature.
enableCultureSensitiveFormatting();Custom Formats
Custom formats may be defined using Format.register and Format.registerFactory methods.
Format.register can be used to register formats which do not need any parameters.
Format.register('brackets', value => `(${value})`);
Format.value('test', 'brackets'); //'(test)'Format.registerFactory can be used to define formats which take parameters.
Format.registerFactory('suffix', (format, suffix) => value => value.toString() + suffix);
Format.value(10, 'suffix; kg'); //'10 kg'