Skip to content

Commit 4b5139d

Browse files
committed
[update] custom formats moved to working with data
1 parent c92b01c commit 4b5139d

File tree

8 files changed

+105
-163
lines changed

8 files changed

+105
-163
lines changed

docs/api/config/fields-property.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ const table = new pivot.Pivot("#root", {
107107
**Related articles**:
108108

109109
- [Number formatting](/guides/localization/#number-formatting)
110-
- [Custom fields formatting](/guides/custom-formatting)
110+
- [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields)
111111

112112
**Related sample:** [Pivot 2. Defining fields formats](https://snippet.dhtmlx.com/77nc4j8v)

docs/api/events/render-table-event.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The callback of the action takes the `config` object with the following paramete
4848
- `field` - (optional) it's a string which is the id of a field
4949
- `method` (string) - (optional) a method, if defined for a field in this column
5050
- `methods` (array) - (optional) defines methods applied to fields in the hierarchical column in the tree mode
51-
- `format` (string or object) - (required) [date format](/guides/custom-formatting/#custom-date-formatting) or [number format](/guides/custom-formatting/#custom-number-formatting)
51+
- `format` (string or object) - (required) date format or number format (refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields))
5252
- `isNumeric` (boolean) - (optional) defines whether a column contains numeric values
5353
- `isTotal` (boolean) - (optional) defines whether it is a total column
5454
- `area` (string) - (optional) an area where the column is rendered: "rows", "columns", "values"
@@ -59,7 +59,7 @@ The callback of the action takes the `config` object with the following paramete
5959
- `value` (any) - (required) raw value, if a cell belongs to "columns" area
6060
- `field` (string) - (required) a field, which value is displayed, if a cell belongs to "columns" area
6161
- `method` (string) - (required) the field predicate, if a cell belongs to "columns" area and predicate is defined
62-
- `format` (string or object) - [date format](/guides/custom-formatting/#custom-date-formatting) or [number format](/guides/custom-formatting/#custom-number-formatting)
62+
- `format` (string or object) - date format or number format (refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields))
6363
- `footer` - (optional) a header label or an object with footer settings which are the same as the header settings
6464
- `data` - (optional) an array of objects with data for the table; each object represents a row:
6565
- `id` (number) - (required) row id

docs/api/helpers/template.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ For body cells the function takes the next parameters:
4141
- `field` - (optional) it's a string which is the id of a field
4242
- `method` (string) - (optional) a method, if defined for a field in this column
4343
- `methods` (array) - (optional) defines methods applied to fields in the hierarchical column in the tree mode
44-
- `format` (string or object) - (required) [date format](/guides/custom-formatting/#custom-date-formatting) or [number format](/guides/custom-formatting/#custom-number-formatting)
44+
- `format` (string or object) - (required) date format or number format (please refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields))
4545
- `isNumeric` (boolean) - (optional) defines whether a column contains numeric values
4646
- `isTotal` (boolean) - (optional) defines whether it is a total column
4747
- `area` (string) - (optional) an area where the column is rendered: "rows", "columns", "values"
@@ -52,7 +52,7 @@ For body cells the function takes the next parameters:
5252
- `value` (any) - (required) raw value, if a cell belongs to "columns" area
5353
- `field` (string) - (required) a field, which value is displayed, if a cell belongs to "columns" area
5454
- `method` (string) - (required) the field predicate, if a cell belongs to "columns" area and predicate is defined
55-
- `format` (string or object) - [date format](/guides/custom-formatting/#custom-date-formatting) or [number format](/guides/custom-formatting/#custom-number-formatting)
55+
- `format` (string or object) - date format or number format (please refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields))
5656

5757
For header cells the function parameters are the following:
5858

@@ -66,7 +66,7 @@ For header cells the function parameters are the following:
6666
- `value` (any) - (required) raw value, if a cell belongs to "columns" area
6767
- `field` (string) - (required) a field, which value is displayed, if a cell belongs to "columns" area
6868
- `method` (string) - (required) a field predicate, if a cell belongs to "columns" area and predicate is defined
69-
- `format` (string or object) - (required) [date format](/guides/custom-formatting/#custom-date-formatting) or [number format](/guides/custom-formatting/#custom-number-formatting)
69+
- `format` (string or object) - (required) date format or number format (please refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields))
7070
- `column` - (required) an object with column data (the same as for the body cell)
7171

7272
### Example

docs/guides/custom-formatting.md

Lines changed: 0 additions & 147 deletions
This file was deleted.

docs/guides/localization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ const table = new pivot.Pivot("#root", {
232232
});
233233
~~~
234234

235-
In case you need to set a custom format to a specific field, use the `format` parameter of the [`fields`](/api/config/fields-property) property. Refer to [Custom date formatting](/guides/custom-formatting/#custom-date-formatting).
235+
In case you need to set a custom format to a specific field, use the `format` parameter of the [`fields`](/api/config/fields-property) property. Refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields).
236236

237237
## Date and time format specification
238238

@@ -268,7 +268,7 @@ To present the 20th of June, 2024 with the exact time as *2024-09-20 16:47:08.12
268268
## Number formatting
269269

270270
By default, all fields with the number type are localized according to the locale (the value in the `lang` field of the locale). Pivot uses [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) specification. By default the fraction digits are limited to 3 and group separation is applied for the integer part.
271-
In case you do not need to format specific fields with numeric values or need to set a custom format, use the the `format` parameter of the [`fields`](/api/config/fields-property) property. It can be either *false* to cancel formatting or an object with format settings (refer to [Custom number formatting](/guides/custom-formatting/#custom-number-formatting)).
271+
In case you do not need to format specific fields with numeric values or need to set a custom format, use the the `format` parameter of the [`fields`](/api/config/fields-property) property. It can be either *false* to cancel formatting or an object with format settings (refer to [Applying formats to fields](/guides/working-with-data/#applying-formats-to-fields)).
272272

273273
~~~jsx
274274
const fields = [

docs/guides/working-with-data.md

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This page describes how to aggregate data in Pivot. For the instructions about l
1212

1313
Use the [`fields`](/api/config/fields-property) property to add fields to the Pivot table. To add a new field, you should add a new object to the `fields` array.
1414

15-
### Example
15+
Example:
1616

1717
~~~jsx
1818
const table = new pivot.Pivot("#root", {
@@ -28,11 +28,99 @@ const table = new pivot.Pivot("#root", {
2828
});
2929
~~~
3030

31+
## Applying formats to fields
32+
33+
For the description of default formatting of date and numeric fields, which depends on locale, refer to [Date formatting](/guides/localization/#date-formatting) and [Number formatting](/guides/localization/#number-formatting).
34+
35+
In case you need to set a custom format to a specific field, use the `format` parameter of the [`fields`](/api/config/fields-property) property. You can add text before and after numeric values using the `prefix` and `suffix` parameters. For example, to convert the value *12.345* to "12.35 EUR", `format` should contain the " EUR" suffix and maximumFractionDigits of 2:
36+
37+
~~~js
38+
const fields = [
39+
{ id: "sales", type: "number", format: {suffix: " EUR", maximumFractionDigits: 2}},
40+
];
41+
~~~
42+
43+
By default, the format for numeric values limits fraction digits to 3 and applies group separation for the integer part. You can cancel formatting by setting `format` to *false* in the `field` configuration:
44+
45+
~~~js
46+
const fields = [
47+
{ id: "year", label: "Year", type: "number", format: false},
48+
];
49+
~~~
50+
51+
In the example below, fields like marketing, profit, and sales are identified as currency-related. A formatting object is applied to these fields with:
52+
53+
- prefix: "$" to display a dollar sign
54+
- *minimumFractionDigits* and *maximumFractionDigits* set to 2 for consistent decimal formatting
55+
56+
~~~jsx
57+
// Initialize pivot with pre-defined dataset and fields
58+
new pivot.Pivot("#pivot", {
59+
data,
60+
config: {
61+
rows: ["state", "product_type"],
62+
columns: [],
63+
values: [
64+
{ field: "marketing", method: "sum" },
65+
// other values
66+
67+
],
68+
},
69+
fields:[
70+
// Custom format
71+
{ id: "marketing", label: "Marketing", type:"number", format:{
72+
prefix: "$", minimumFractionDigits: 2, maximumFractionDigits: 2 }
73+
}
74+
]
75+
});
76+
~~~
77+
78+
To override the default locale-wide `dateFormat`, apply the `format` parameter of the [`fields`](/api/config/fields-property) property. Date format is a string, for example:
79+
80+
~~~jsx
81+
const fields = [
82+
{ id: "date", type: "date", format: "%M %d, %Y"},
83+
];
84+
~~~
85+
86+
In the example below we set the date format to "%d %M %Y %H:%i" for the "date" field only. The format displays day, full month name, year, hours, and minutes, e.g., "24 April 2025 14:30". In case you need to disable formatting of some fields, set the `format` parameter of the [`fields`](/api/config/fields-property) property to *false*.
87+
88+
~~~jsx
89+
// Convert date strings to Date objects
90+
const dateFields = fields.filter(f => f.type === "date");
91+
dataset.forEach(item => {
92+
dateFields.forEach(f => {
93+
const v = item[f.id];
94+
if (typeof v === "string") {
95+
item[f.id] = new Date(v);
96+
}
97+
});
98+
});
99+
100+
// Initialize Pivot with field-specific date format
101+
new pivot.Pivot("#pivot", {
102+
data,
103+
config: {
104+
rows: ["state"],
105+
columns: ["product_type"],
106+
values: [
107+
{ field: "date", method: "min" },
108+
{ field: "profit", method: "sum" },
109+
{ field: "sales", method: "sum" }
110+
]
111+
},
112+
fields:[
113+
// Custom format: Day Month Year Hour:Minute
114+
{id:"date", label:"Date", type:"date", "%d %M %Y %H:%i"}
115+
]
116+
});
117+
~~~
118+
31119
## Defining Pivot structure
32120

33121
You can create the Pivot structure using the [`config`](/api/config/config-property) property that also defines how data is aggregated. By default, it has no predefined values. You need to specify this property to define the configuration of the Pivot table, namely, which fields should be applied as columns and rows. The property also allows adding data aggregation methods to be applied to the fields. Here you can also add filters. Please, refer to the [`config`](/api/config/config-property) property description for details.
34122

35-
### Example
123+
Example:
36124

37125
~~~jsx {4-18}
38126
const table = new pivot.Pivot("#root", {
@@ -202,7 +290,7 @@ To interrupt data rendering and prevent the component from hanging, you can limi
202290
Limits are used for large dataset. Limits values are approximate values and do not show the exact values of the rows and columns.
203291
:::
204292

205-
### Example
293+
Example:
206294

207295
~~~jsx
208296
const table = new pivot.Pivot("#root", {
@@ -280,7 +368,7 @@ const defaultMethods = {
280368

281369
You can apply default methods using the `values` parameter of the [`config`](/api/config/config-property) property. See [Options for defining values](#options-for-defining-values) below.
282370

283-
### Example
371+
Example:
284372

285373
~~~jsx
286374
const table = new pivot.Pivot("#root", {
@@ -311,7 +399,7 @@ You can define `values`in either of the two equally valid ways:
311399
- option one is a string representing a data field ID: `operation(fieldID)`
312400
- option two is an object containing the field ID and the method for data aggregation (both are required): `{ field: string, method: string }`
313401

314-
### Example
402+
Example:
315403

316404
~~~jsx
317405
values: [
@@ -574,6 +662,8 @@ In this snippet you can see how to apply custom maths operations:
574662
<iframe src="https://snippet.dhtmlx.com/lv90d8q2?mode=result" frameborder="0" class="snippet_iframe" width="100%" height="600"></iframe>
575663

576664
**Related samples:**
577-
- [Pivot 2. Grand total for columns and rows](https://snippet.dhtmlx.com/f0ag0t9t)
665+
578666
- [Pivot 2. Dataset with aliases](https://snippet.dhtmlx.com/7vc68rqd)
667+
- [Pivot 2. Defining fields formats](https://snippet.dhtmlx.com/77nc4j8v)
579668
- [Pivot 2. External filter](https://snippet.dhtmlx.com/s7tc9g4z)
669+
- [Pivot 2. Grand total for columns and rows](https://snippet.dhtmlx.com/f0ag0t9t)

docs/news/whats-new.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Released on ...
1515
### New functionality
1616

1717
- [Ability to freeze columns on the right](/guides/configuration/#freezing-columns-on-the-right)
18-
- [Numbers are formatted](/guides/localization/#number-formatting) according to the current locale with [a new possibility to define custom number formats within templates](/guides/custom-formatting) (for date and numeric fields) via `format` added to the [`fields`](/api/config/fields-property) property
18+
- [Numbers are formatted](/guides/localization/#number-formatting) according to the current locale with [a new possibility to define custom number formats within templates](/guides/working-with-data/#applying-formats-to-fields) (for date and numeric fields) via `format` added to the [`fields`](/api/config/fields-property) property
1919
- [Ability to style header and table cells](/guides/stylization/#cell-style) via the `cellStyle` parameter of the [`tableShape`](/api/config/tableshape-property) and [`headerShape`](/api/config/tableshape-property) properties
2020
- Ability to insert HTML content to header and table cells via the [`pivot.template`](/api/helpers/template) helper by defining a template as a `cell` property of the header and column objects (table customization by intercepting the [render-table](/api/events/render-table-event) event)
2121
- [Excel and CSV export settings enhanced](/guides/exporting-data):

sidebars.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ module.exports = {
196196
items: [
197197
"guides/initialization",
198198
"guides/configuration",
199-
"guides/custom-formatting",
200199
"guides/exporting-data",
201200
"guides/loading-data",
202201
"guides/localization",

0 commit comments

Comments
 (0)