-
Notifications
You must be signed in to change notification settings - Fork 159
fix(pivot-grid): fix date format based on the localization #17246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2953430
7eb51a8
118206a
c5c8154
6e07821
859a146
df731dc
71b8391
1d40629
e92647b
8a93e81
b664a7a
368595c
d97c5d3
2c27c90
2eddc5d
505fad5
fdb6228
37a8649
4d01b26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,13 @@ export class IgxPivotDateDimension implements IPivotDimension { | |
| /** @hidden @internal */ | ||
| public memberName = 'AllPeriods'; | ||
| public displayName: string; | ||
| /** | ||
| * Gets/Sets the locale used for date dimension member formatting (e.g. month names). | ||
| * When set, overrides the global I18nManager locale for this dimension. | ||
| * Set automatically by the pivot grid row pipe when the grid locale changes. | ||
| * @hidden @internal | ||
| */ | ||
| public locale: string; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be marked optional, if it can be undefined. |
||
| private _resourceStrings: IGridResourceStrings = null; | ||
| private _baseDimension: IPivotDimension; | ||
| private _options: IPivotDateDimensionOptions = {}; | ||
|
|
@@ -139,13 +146,39 @@ export class IgxPivotDateDimension implements IPivotDimension { | |
| this.enabled = inBaseDimension.enabled; | ||
| this.displayName = inBaseDimension.displayName || this.resourceStrings.igx_grid_pivot_date_dimension_total; | ||
|
|
||
| const baseDimension = options.fullDate ? inBaseDimension : null; | ||
| // When fullDate is enabled, attach a locale-aware formatter unless the user has | ||
| // already supplied their own headerFormatter on inBaseDimension. | ||
| // The memberFunction (if any) is kept intact via the spread — the formatter is | ||
| // a separate display-only concern and should not be gated on memberFunction. | ||
| let baseDimension: IPivotDimension = null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this in a separate method since it's balloons out the |
||
| if (options.fullDate) { | ||
| if (inBaseDimension.headerFormatter) { | ||
| // User supplied their own formatter — use the dimension as-is. | ||
| baseDimension = inBaseDimension; | ||
| } else { | ||
| const dateFormatter = getDateFormatter(); | ||
| // No user-supplied formatter: create a new dimension object with a locale-aware | ||
| // formatter that shows dates in short-date format. | ||
| baseDimension = { | ||
| ...inBaseDimension, | ||
| headerFormatter: (value: any) => { | ||
| const hasValue = value !== null && value !== undefined && value !== ''; | ||
| const dateValue = hasValue ? dateFormatter.createDateFromValue(value) : null; | ||
| if (dateValue) { | ||
| return dateFormatter.formatDateTime(dateValue, this.locale, { dateStyle: 'short' }); | ||
| } | ||
| return hasValue ? String(value) : ''; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const monthDimensionDef: IPivotDimension = { | ||
| memberName: 'Months', | ||
| memberFunction: (rec) => { | ||
|
Comment on lines
176
to
178
|
||
| const recordValue = PivotUtil.extractValueFromDimension(inBaseDimension, rec); | ||
| const dateValue = recordValue ? getDateFormatter().createDateFromValue(recordValue) : null; | ||
| return recordValue ? getDateFormatter().formatDateTime(dateValue, undefined, { month: 'long'}) : rec['Months']; | ||
| const dateValue = (recordValue != null && recordValue !== '') ? getDateFormatter().createDateFromValue(recordValue) : null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move the more complex check for |
||
| return (recordValue != null && recordValue !== '') ? getDateFormatter().formatDateTime(dateValue, this.locale, { month: 'long'}) : rec['Months']; | ||
| }, | ||
| enabled: true, | ||
| childLevel: baseDimension | ||
|
|
@@ -156,8 +189,8 @@ export class IgxPivotDateDimension implements IPivotDimension { | |
| memberName: 'Quarters', | ||
| memberFunction: (rec) => { | ||
| const recordValue = PivotUtil.extractValueFromDimension(inBaseDimension, rec); | ||
| const dateValue = recordValue ? getDateFormatter().createDateFromValue(recordValue) : null; | ||
| return recordValue ? `Q` + Math.ceil((dateValue.getMonth() + 1) / 3) : rec['Quarters']; | ||
| const dateValue = (recordValue != null && recordValue !== '') ? getDateFormatter().createDateFromValue(recordValue) : null; | ||
| return (recordValue != null && recordValue !== '') ? `Q` + Math.ceil((dateValue.getMonth() + 1) / 3) : rec['Quarters']; | ||
| }, | ||
| enabled: true, | ||
| childLevel: monthDimension | ||
|
|
@@ -168,8 +201,8 @@ export class IgxPivotDateDimension implements IPivotDimension { | |
| memberName: 'Years', | ||
| memberFunction: (rec) => { | ||
| const recordValue = PivotUtil.extractValueFromDimension(inBaseDimension, rec); | ||
| const dateValue = recordValue ? getDateFormatter().createDateFromValue(recordValue) : null; | ||
| return recordValue ? dateValue.getFullYear().toString() : rec['Years']; | ||
| const dateValue = (recordValue != null && recordValue !== '') ? getDateFormatter().createDateFromValue(recordValue) : null; | ||
| return (recordValue != null && recordValue !== '') ? dateValue.getFullYear().toString() : rec['Years']; | ||
| }, | ||
| enabled: true, | ||
| childLevel: quarterDimension | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,6 +141,22 @@ export interface IPivotDimension { | |
| /** @hidden @internal */ | ||
| autoWidth?: number; | ||
| horizontalSummary? : boolean; | ||
| /** | ||
| * Optional function to format the display value of a dimension header. | ||
| * Unlike `memberFunction`, this does not affect the data key used for grouping or sorting — | ||
| * it is applied when rendering the dimension header text (both row and column dimension headers). | ||
| * When set, the return value of this function is shown instead of the raw dimension value. | ||
| * Return `null` or `undefined` to fall back to the raw value. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // Display dates in a locale-aware short date format. | ||
| * { memberName: 'Date', enabled: true, headerFormatter: (value) => new Date(value).toLocaleDateString() } | ||
| * ``` | ||
| */ | ||
|
Hristo313 marked this conversation as resolved.
|
||
| /* csTreatAsEvent: PivotDimensionFormatterEventHandler */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually put these tag the top, so they can be easily seen. That's just for consistency. |
||
| /* blazorOnlyScript */ | ||
| headerFormatter?: (value: any, dimension?: IPivotDimension, rowData?: IPivotGridGroupRecord) => string | null | undefined; | ||
| } | ||
|
|
||
| /* marshalByValue */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { | |
| IPivotGridRecord, | ||
| IPivotKeys, | ||
| IPivotValue, | ||
| IgxPivotDateDimension, | ||
| PivotColumnDimensionsStrategy, | ||
| PivotGridType, | ||
| PivotRowDimensionsStrategy, | ||
|
|
@@ -51,6 +52,14 @@ export class IgxPivotRowPipe implements PipeTransform { | |
| // nothing to group and aggregate by ... | ||
| return []; | ||
| } | ||
| const locale = this.grid?.locale; | ||
| if (locale) { | ||
| for (const dim of enabledRows) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IgxPivotDateDimension can be set as columns as well. |
||
| if (dim instanceof IgxPivotDateDimension) { | ||
| dim.locale = locale; | ||
| } | ||
| } | ||
| } | ||
| const rowStrategy = config.rowStrategy || PivotRowDimensionsStrategy.instance(); | ||
| const data = cloneArray(collection, true); | ||
| return rowStrategy.process(data, enabledRows, config.values, cloneStrategy, pivotKeys); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes in general are not listed in the Changelog.