Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 4a3af96

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
refactor(formatter): fix some type warnings & add demo
- use latest version of SlickGrid to have latest fixes including some needed for this PR - fix some problems with the export and make it return empty string when value is null
1 parent c8808f4 commit 4a3af96

File tree

11 files changed

+57
-20
lines changed

11 files changed

+57
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"lodash.isequal": "^4.5.0",
7070
"moment-mini": "^2.22.1",
7171
"rxjs": "^6.3.3",
72-
"slickgrid": "github:6pac/SlickGrid",
72+
"slickgrid": "^2.4.3",
7373
"text-encoding-utf-8": "^1.0.2",
7474
"tslib": "^1.9.3",
7575
"vinyl-paths": "^2.1.0"

src/app/app.component.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
.body-content {
22
margin-top: 50px;
33
}
4+
.lightblue {
5+
color: lightblue;
6+
}
47
.red {
58
color: red;
69
}

src/app/examples/grid-formatter.component.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { Component, OnInit } from '@angular/core';
22
import { Column, FieldType, Formatter, Formatters, GridOption } from './../modules/angular-slickgrid';
33

44
// create my custom Formatter with the Formatter type
5-
const myCustomCheckmarkFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) =>
6-
value ? `<i class="fa fa-fire" aria-hidden="true"></i>` : '<i class="fa fa-snowflake-o" aria-hidden="true"></i>';
5+
const myCustomCheckmarkFormatter: Formatter = (row, cell, value, columnDef, dataContext) => {
6+
// you can return a string of a object (of type FormatterResultObject), the 2 types are shown below
7+
return value ? `<i class="fa fa-fire red" aria-hidden="true"></i>` : { text: '<i class="fa fa-snowflake-o" aria-hidden="true"></i>', addClasses: 'lightblue', toolTip: 'Freezing' };
8+
};
79

810
@Component({
911
templateUrl: './grid-formatter.component.html'
@@ -13,6 +15,7 @@ export class GridFormatterComponent implements OnInit {
1315
subTitle = `
1416
Grid with Custom and/or included Slickgrid Formatters (<a href="https://github.com/ghiscoding/Angular-Slickgrid/wiki/Formatters" target="_blank">Wiki docs</a>).
1517
<ul>
18+
<li>Last column is a Custom Formatter</li>
1619
<li>
1720
Support Excel Copy Buffer (SlickGrid Copy Manager Plugin), you can use it by simply enabling "enableExcelCopyBuffer" flag.
1821
Note that it will only evaluate Formatter when the "exportWithFormatter" flag is enabled (through "ExportOptions" or the column definition)
@@ -28,7 +31,7 @@ export class GridFormatterComponent implements OnInit {
2831
this.columnDefinitions = [
2932
{ id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string, width: 70 },
3033
{ id: 'phone', name: 'Phone Number using mask', field: 'phone', sortable: true, type: FieldType.number, minWidth: 100, formatter: Formatters.mask, params: { mask: '(000) 000-0000' } },
31-
{ id: 'duration', name: 'Duration (days)', field: 'duration', formatter: Formatters.decimal, params: { minDecimalPlaces: 1, maxDecimalPlaces: 2 }, sortable: true, type: FieldType.number, minWidth: 90 },
34+
{ id: 'duration', name: 'Duration (days)', field: 'duration', formatter: Formatters.decimal, params: { minDecimalPlaces: 1, maxDecimalPlaces: 2 }, sortable: true, type: FieldType.number, minWidth: 90, exportWithFormatter: true },
3235
{ id: 'complete', name: '% Complete', field: 'percentComplete', formatter: Formatters.percentCompleteBar, type: FieldType.number, sortable: true, minWidth: 100 },
3336
{ id: 'percent2', name: '% Complete', field: 'percentComplete2', formatter: Formatters.progressBar, type: FieldType.number, sortable: true, minWidth: 100 },
3437
{ id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, sortable: true, type: FieldType.date, minWidth: 90, exportWithFormatter: true },

src/app/modules/angular-slickgrid/extensions/cellExternalCopyManagerExtension.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ export class CellExternalCopyManagerExtension implements Extension {
4040
if (columnDef.formatter && isEvaluatingFormatter) {
4141
const formattedOutput = columnDef.formatter(0, 0, item[columnDef.field], columnDef, item, this.sharedService.grid);
4242
if (columnDef.sanitizeDataExport || (this.sharedService.gridOptions.exportOptions && this.sharedService.gridOptions.exportOptions.sanitizeDataExport)) {
43-
const outputString = typeof formattedOutput === 'string' ? formattedOutput : formattedOutput.text;
43+
let outputString = formattedOutput as string;
44+
if (formattedOutput && typeof formattedOutput === 'object' && formattedOutput.hasOwnProperty('text')) {
45+
outputString = formattedOutput.text;
46+
}
47+
if (outputString === null) {
48+
outputString = '';
49+
}
4450
return sanitizeHtmlToText(outputString);
4551
}
4652
return formattedOutput;

src/app/modules/angular-slickgrid/formatters/decimalFormatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { decimalFormatted } from './../services/utilities';
33

44
export const decimalFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
55
const params = columnDef.params || {};
6-
const minDecimalPlaces = params.minDecimalPlaces || params.decimalPlaces || 2;
7-
const maxDecimalPlaces = params.maxDecimalPlaces || 2;
6+
const minDecimalPlaces = (params.minDecimalPlaces !== null && params.minDecimalPlaces) || (params.decimalPlaces !== null && params.decimalPlaces) || 2;
7+
const maxDecimalPlaces = (params.maxDecimalPlaces !== null && params.maxDecimalPlaces) || 2;
88
const isNumber = (value === null || value === undefined) ? false : !isNaN(+value);
99

1010
return !isNumber ? value : `${decimalFormatted(value, minDecimalPlaces, maxDecimalPlaces)}`;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { Column } from './column.interface';
2-
import { FormatterResultObject } from './formatterResultObject';
2+
import { FormatterResultObject } from './formatterResultObject.interface';
33

44
export declare type Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any, grid?: any) => string | FormatterResultObject;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export interface FormatterResultObject {
2+
/** Optional CSS classes to add to the cell div */
3+
addClasses?: string;
4+
5+
/** Optional CSS classes to remove from the cell div */
6+
removeClasses?: string;
7+
8+
/** Text to be displayed in the cell, basically the formatter output */
9+
text: string;
10+
11+
/** Optional tooltip text */
12+
toolTip?: string;
13+
}

src/app/modules/angular-slickgrid/models/formatterResultObject.ts

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

src/app/modules/angular-slickgrid/models/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export * from './filterConditionOption.interface';
4545
export * from './filterMultiplePassType.enum';
4646
export * from './filterMultiplePassTypeString';
4747
export * from './formatter.interface';
48-
export * from './formatterResultObject';
48+
export * from './formatterResultObject.interface';
4949
export * from './graphqlDatasetFilter.interface';
5050
export * from './graphqlCursorPaginationOption.interface';
5151
export * from './graphqlFilteringOption.interface';

src/app/modules/angular-slickgrid/services/export.service.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,29 @@ export class ExportService {
215215

216216
let itemData = '';
217217

218-
if (exportCustomFormatter) {
219-
const customFormattedData = exportCustomFormatter(row, col, itemObj[fieldId], columnDef, itemObj, this._grid);
220-
itemData = typeof customFormattedData === 'string' ? customFormattedData : customFormattedData.text;
221-
} else if (isEvaluatingFormatter && !!columnDef.formatter) {
218+
if (itemObj && itemObj[fieldId] && exportCustomFormatter !== undefined && exportCustomFormatter !== null) {
219+
const formattedData = exportCustomFormatter(row, col, itemObj[fieldId], columnDef, itemObj, this._grid);
220+
itemData = formattedData as string;
221+
if (formattedData && typeof formattedData === 'object' && formattedData.hasOwnProperty('text')) {
222+
itemData = formattedData.text;
223+
}
224+
if (itemData === null) {
225+
itemData = '';
226+
}
227+
} else if (isEvaluatingFormatter && columnDef.formatter !== undefined && columnDef.formatter !== null) {
222228
const formattedData = columnDef.formatter(row, col, itemObj[fieldId], columnDef, itemObj, this._grid);
223-
itemData = typeof formattedData === 'string' ? formattedData : formattedData.text;
229+
itemData = formattedData as string;
230+
if (formattedData && typeof formattedData === 'object' && formattedData.hasOwnProperty('text')) {
231+
itemData = formattedData.text;
232+
}
233+
if (itemData === null) {
234+
itemData = '';
235+
}
224236
} else {
225237
itemData = (itemObj[fieldId] === null || itemObj[fieldId] === undefined) ? '' : itemObj[fieldId];
238+
if (itemData === null) {
239+
itemData = '';
240+
}
226241
}
227242

228243
// does the user want to sanitize the output data (remove HTML tags)?

0 commit comments

Comments
 (0)