-
Notifications
You must be signed in to change notification settings - Fork 159
fix(pivot-grid): fix date format based on the localization #17256
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
Changes from 3 commits
f054eb5
e86ce0f
b2b689e
440d496
bf31407
039f036
7595db4
179942e
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 |
|---|---|---|
|
|
@@ -139,13 +139,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 and the user has not provided a custom memberFunction, | ||
| // attach a locale-aware formatter so the leaf date values are displayed in | ||
| // short-date format instead of the raw data string. | ||
| // When the user provides their own memberFunction, the dimension is used as-is | ||
| // (spread-create is skipped) to avoid overriding the user's intended formatting. | ||
| let baseDimension: IPivotDimension = null; | ||
| if (options.fullDate) { | ||
| if (inBaseDimension.memberFunction) { | ||
| // User supplied a custom memberFunction — preserve it without adding a formatter. | ||
| baseDimension = inBaseDimension; | ||
| } else { | ||
| // No custom memberFunction: create a new dimension object with a locale-aware | ||
| // formatter that shows dates in short-date format. | ||
| baseDimension = { | ||
| ...inBaseDimension, | ||
| formatter: (value: any) => { | ||
| const dateValue = (value !== null && value !== undefined && value !== '') | ||
| ? getDateFormatter().createDateFromValue(value) | ||
| : null; | ||
| return dateValue | ||
| ? getDateFormatter().formatDateTime(dateValue, undefined, { dateStyle: 'short' }) | ||
| : value; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
Comment on lines
+147
to
+167
|
||
|
|
||
| const monthDimensionDef: IPivotDimension = { | ||
| memberName: 'Months', | ||
| memberFunction: (rec) => { | ||
| 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; | ||
| return (recordValue != null && recordValue !== '') ? getDateFormatter().formatDateTime(dateValue, undefined, { month: 'long'}) : rec['Months']; | ||
| }, | ||
| enabled: true, | ||
| childLevel: baseDimension | ||
|
|
@@ -156,8 +182,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 +194,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 |
|---|---|---|
|
|
@@ -140,6 +140,22 @@ export interface IPivotDimension { | |
| /** @hidden @internal */ | ||
| autoWidth?: number; | ||
| horizontalSummary? : boolean; | ||
| /** | ||
| * Optional function to format the display value of a dimension cell. | ||
| * 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 | ||
|
Hristo313 marked this conversation as resolved.
|
||
| * // Display dates in a locale-aware short date format. | ||
| * { memberName: 'Date', enabled: true, formatter: (value) => new Date(value).toLocaleDateString() } | ||
| * ``` | ||
| */ | ||
| /* csTreatAsEvent: PivotDimensionFormatterEventHandler */ | ||
| /* blazorOnlyScript */ | ||
| formatter?: (value: any) => string | null | undefined; | ||
|
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. Also naming it just formatter is kinda confusing. This is not a formatter for the dimension cell. I think it should be named something like headerFormatter/columnFormatter.
Contributor
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. Done in bf31407: renamed the new dimension display hook from |
||
| } | ||
|
|
||
| /* marshalByValue */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1245,7 +1245,8 @@ describe('IgxPivotGrid #pivotGrid', () => { | |
| // check rows | ||
| const rows = pivotGrid.rowList.toArray(); | ||
| expect(rows.length).toBe(5); | ||
| const expectedHeaders = ['All Periods', '2021', 'Q4', 'December', '12/08/2021']; | ||
| const formattedDate = Intl.DateTimeFormat(undefined, { dateStyle: 'short' }).format(new Date(2021, 11, 8)); | ||
| const expectedHeaders = ['All Periods', '2021', 'Q4', 'December', formattedDate]; | ||
|
Comment on lines
+1272
to
+1273
|
||
| const rowHeaders = fixture.debugElement.queryAll( | ||
| By.directive(IgxPivotRowDimensionHeaderComponent)); | ||
| const rowDimensionHeaders = rowHeaders.map(x => x.componentInstance.column.header); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.