Skip to content

Commit e0f7aff

Browse files
committed
[update] examples for templates in table and header changed
1 parent 3522ac2 commit e0f7aff

File tree

3 files changed

+84
-77
lines changed

3 files changed

+84
-77
lines changed

docs/api/config/headershape-property.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,37 @@ template: (label, id, subLabel) =>
4545

4646
## Example
4747

48-
~~~jsx {18-21}
49-
const table = new pivot.Pivot("#root", {
50-
fields,
51-
data: dataset,
48+
In the example below for the **values** fields the header will display the label, the method name (subLabel) and converts the result to lowercase (e.g., *profit (sum)*):
49+
50+
~~~jsx {3-6}
51+
new pivot.Pivot("#pivot", {
52+
data,
53+
headerShape: {
54+
// a custom template for header text
55+
template: (label, id, subLabel) => (label + (subLabel ? ` (${subLabel})` : "")).toLowerCase(),
56+
},
5257
config: {
53-
rows: ["studio", "genre"],
58+
rows: ["state", "product_type"],
5459
columns: [],
5560
values: [
5661
{
57-
field: "title",
58-
method: "count"
62+
field: "profit",
63+
method: "sum"
5964
},
6065
{
61-
field: "score",
62-
method: "max"
63-
}
64-
]
66+
field: "sales",
67+
method: "sum"
68+
},
69+
// other values
70+
],
6571
},
66-
headerShape: {
67-
vertical: true,
68-
template: (label, field, subLabel) => field + (subLabel ? ` (${subLabel})` : ""),
69-
}
72+
fields,
7073
});
7174
~~~
7275

7376
**Related samples**:
7477
- [Pivot 2. Vertical orientation of text in grid headers](https://snippet.dhtmlx.com/4qroi8ka)
7578
- [Pivot 2. Collapsible columns](https://snippet.dhtmlx.com/pt2ljmcm)
76-
- [Pivot 2. Headers template](https://snippet.dhtmlx.com/g89r9ryw)
7779
- [Pivot 2. Pivot 2: Adding сustom CSS for table and header cells](https://snippet.dhtmlx.com/nfdcs4i2)
7880

7981
**Related articles**:

docs/api/config/tableshape-property.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,42 @@ By default, `tableShape` is undefined, implying that no total row, no total colu
7979

8080
## Example
8181

82-
In the example below we apply the template to the *score* values to display 2 digits after the decimal point for these values and we add the "€" sign to the *price* values.
82+
In the example below we apply the template to the *state* cells to show the combined name of a state (the full name and abbreviation).
8383

84-
~~~jsx {5-8}
85-
const templates = { price: (v) => (v ? "" + v : v),
86-
score: (v) => (v ? parseFloat(v).toFixed(2) : v) };
84+
~~~jsx {10-15}
85+
const states = {
86+
"California": "CA",
87+
"Colorado": "CO",
88+
"Connecticut": "CT",
89+
"Florida": "FL",
90+
// other values,
91+
};
8792

8893
const table = new pivot.Pivot("#root", {
8994
tableShape: {
90-
tree: true,
91-
templates
95+
templates: {
96+
// set a template to customize values of "state" cells
97+
state: v => v+ ` (${states[v]})`,
98+
}
9299
},
93100
fields,
94101
data,
95102
config: {
96-
rows: ["studio", "genre"],
103+
rows: ["state", "product_type"],
97104
columns: [],
98105
values: [
99106
{
100-
field: "title",
101-
method: "count"
107+
field: "profit",
108+
method: "sum"
102109
},
103110
{
104-
field: "score",
105-
method: "max"
111+
field: "sales",
112+
method: "sum"
106113
},
107-
{
108-
field: "price",
109-
method: "count"
110-
}
111-
]
112-
}
114+
// other values
115+
],
116+
},
117+
fields,
113118
});
114119
~~~
115120

@@ -119,7 +124,7 @@ const table = new pivot.Pivot("#root", {
119124
- [Pivot 2. Frozen (fixed) columns](https://snippet.dhtmlx.com/lahf729o)
120125
- [Pivot 2. Set row, header, footer height and all columns width](https://snippet.dhtmlx.com/x46uyfy9)
121126
- [Pivot 2. Clean rows](https://snippet.dhtmlx.com/rwwhgv2w?tag=pivot)
122-
- [Pivot 2. Pivot 2: Adding сustom CSS for table and header cells](https://snippet.dhtmlx.com/nfdcs4i2)
127+
- [Pivot 2. Adding сustom CSS for table and header cells](https://snippet.dhtmlx.com/nfdcs4i2)
123128

124129
**Related articles**:
125130
- [Configuration](/guides/configuration)

docs/guides/configuration.md

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -133,38 +133,42 @@ const table = new pivot.Pivot("#root", {
133133

134134
To set a template to cells, use the `templates` parameter of the [`tableShape`](/api/config/tableshape-property) property. It's an object where each key is a field id and the value is a function that returns a string. All columns based on the specified field will have the related template applied.
135135

136-
In the example below we apply the template to the *score* values to display 2 digits after the decimal point for these values and we add the "€" sign to the *price* values.
137-
138-
~~~jsx {1-4,8}
139-
const templates = {
140-
price: (v) => (v ? "" + v : v),
141-
score: (v) => (v ? parseFloat(v).toFixed(2) : v)
136+
In the example below we apply the template to the *state* cells to show the combined name of a state (the full name and abbreviation).
137+
138+
~~~jsx {10-15}
139+
const states = {
140+
"California": "CA",
141+
"Colorado": "CO",
142+
"Connecticut": "CT",
143+
"Florida": "FL",
144+
// other values,
142145
};
143146

144147
const table = new pivot.Pivot("#root", {
145148
tableShape: {
146-
templates
149+
templates: {
150+
// set a template to customize values of "state" cells
151+
state: v => v+ ` (${states[v]})`,
152+
}
147153
},
148154
fields,
149155
data,
150156
config: {
151-
rows: ["studio", "genre"],
157+
rows: ["state", "product_type"],
152158
columns: [],
153159
values: [
154160
{
155-
field: "title",
156-
method: "count"
161+
field: "profit",
162+
method: "sum"
157163
},
158164
{
159-
field: "score",
160-
method: "max"
165+
field: "sales",
166+
method: "sum"
161167
},
162-
{
163-
field: "price",
164-
method: "count"
165-
}
166-
]
167-
}
168+
// other values
169+
],
170+
},
171+
fields,
168172
});
169173
~~~
170174

@@ -221,31 +225,31 @@ To define the format of text in headers, apply the `template` parameter of the [
221225
A default template is as follows: *template: (label, id, subLabel) => label + (subLabel ? `(${subLabel})` : "")*. By default, for the fields applied as values the label and method are shown (e.g., *Oil(count)*).
222226
If no other template is applied to columns, the value of the `label` parameter is displayed. If any [`predicates`](/api/config/predicates-property) template is applied, it will override the template of the `headerShape` property.
223227
224-
In the example below for the **values** fields the header will display the method name (subLabel) and the label:
228+
In the example below for the **values** fields the header will display the label, the method name (subLabel) and converts the result to lowercase (e.g., *profit (sum)*):
225229
226-
~~~jsx {19-22}
227-
const table = new pivot.Pivot("#root", {
228-
fields,
230+
~~~jsx {3-6}
231+
new pivot.Pivot("#pivot", {
229232
data,
233+
headerShape: {
234+
// a custom template for header text
235+
template: (label, id, subLabel) => (label + (subLabel ? ` (${subLabel})` : "")).toLowerCase(),
236+
},
230237
config: {
231-
rows: ["studio", "genre"],
238+
rows: ["state", "product_type"],
232239
columns: [],
233240
values: [
234241
{
235-
field: "title",
236-
method: "count"
242+
field: "profit",
243+
method: "sum"
237244
},
238245
{
239-
field: "score",
240-
method: "max"
241-
}
242-
]
246+
field: "sales",
247+
method: "sum"
248+
},
249+
// other values
250+
],
243251
},
244-
245-
headerShape: {
246-
vertical: true,
247-
template: (label, id, subLabel) => id + (subLabel ? ` (${subLabel})` : ""),
248-
}
252+
fields,
249253
});
250254
~~~
251255
@@ -668,7 +672,7 @@ table.api.exec("show-config-panel", {
668672
669673
You can block toggling the visibility of the Configuration panel on the button click via the [`api.intercept()`](/api/internal/intercept-method) method (by listening to the [`show-config-panel`](/api/events/show-config-panel-event) event and returning *false*).
670674
671-
### Example
675+
Example:
672676
673677
~~~jsx {20-22}
674678
const table = new pivot.Pivot("#root", {
@@ -706,15 +710,11 @@ In the Configuration panel it's possible to perform the next operations with fie
706710
- [update-field](/api/events/update-value-event)
707711
- [move-field](/api/events/move-field-event)
708712
709-
## Example
710-
711-
In this snippet you can see how to apply templates to the Pivot cells:
712-
713-
<iframe src="https://snippet.dhtmlx.com/n9ylp6b2?mode=result" frameborder="0" class="snippet_iframe" width="100%" height="600"></iframe>
714-
715713
**Related samples:**
716-
- [Pivot 2. Sorting](https://snippet.dhtmlx.com/j7vtief6)
714+
- [Pivot 2. Adding text templates for table and header cells](https://snippet.dhtmlx.com/n9ylp6b2)
715+
- [Pivot 2. Custom frozen (fixed) columns (your number)](https://snippet.dhtmlx.com/53erlmgp)
717716
- [Pivot 2. Expand and collapse all rows](https://snippet.dhtmlx.com/i4mi6ejn)
718717
- [Pivot 2. Frozen(fixed) columns on the left and right](https://snippet.dhtmlx.com/lahf729o)
719-
- [Pivot 2. Custom frozen (fixed) columns (your number)](https://snippet.dhtmlx.com/53erlmgp)
718+
- [Pivot 2. Sorting](https://snippet.dhtmlx.com/j7vtief6)
719+
720720

0 commit comments

Comments
 (0)