Skip to content
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

fix(grid): use formattter function for boolean columns #8978 #10154

Merged
merged 13 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ const testMerging = () => {
describe('Test merging', () => {
it('Should merge add transactions correctly', () => {
const data = SampleTestData.personIDNameData();
const addRow4 = { ID: 4, Name: 'Peter' };
const addRow5 = { ID: 5, Name: 'Mimi' };
const addRow6 = { ID: 6, Name: 'Pedro' };
const addRow4 = { ID: 4, IsEmployed: true, Name: 'Peter' };
const addRow5 = { ID: 5, IsEmployed: true, Name: 'Mimi' };
const addRow6 = { ID: 6, IsEmployed: false, Name: 'Pedro' };
const transactions: Transaction[] = [
{ id: addRow4.ID, newValue: addRow4, type: TransactionType.ADD },
{ id: addRow5.ID, newValue: addRow5, type: TransactionType.ADD },
Expand Down
7 changes: 4 additions & 3 deletions projects/igniteui-angular/src/lib/grids/cell.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
</ng-template>
<ng-template #defaultCell>
<div *ngIf="column.dataType !== 'boolean'"
<div *ngIf="column.dataType !== 'boolean' || (column.dataType === 'boolean' && this.formatter)"
igxTextHighlight
class="igx-grid__td-text"
style="pointer-events: none;"
Expand Down Expand Up @@ -46,13 +46,14 @@
: value
}}</div>
<igx-icon
*ngIf="column.dataType === 'boolean'"
*ngIf="column.dataType === 'boolean' && !this.formatter"
[ngClass]="{ 'igx-icon--success': value, 'igx-icon--error': !value }"
>{{ value ? "check" : "close" }}</igx-icon
>
</ng-template>
<ng-template #addRowCell let-cell="cell">
<div igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
<div *ngIf="column.dataType !== 'boolean' || (column.dataType === 'boolean' && this.formatter)"
igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
[cssClass]="highlightClass"
[activeCssClass]="activeHighlightClass"
[groupName]="gridID"
Expand Down
13 changes: 9 additions & 4 deletions projects/igniteui-angular/src/lib/grids/grid/column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,16 @@ describe('IgxGrid - Column properties #grid', () => {
const grid = fix.componentInstance.grid;
const formatter = fix.componentInstance.multiplier;

const boolFormatter = fix.componentInstance.boolFormatter;

expect(grid.columnList.first.formatter).toBeDefined();

for (let i = 0; i < 3; i++) {
const cell = grid.gridAPI.get_cell_by_index(i, 'ID');
expect(cell.nativeElement.textContent).toMatch(formatter(cell.value));

const cellBool = grid.gridAPI.get_cell_by_index(i, 'IsEmployed');
expect(cellBool.nativeElement.textContent).toMatch(boolFormatter(cellBool.value));
}
});

Expand Down Expand Up @@ -161,18 +166,18 @@ describe('IgxGrid - Column properties #grid', () => {

headers = fix.debugElement.queryAll(By.css(COLUMN_HEADER_CLASS));
expect(headers[0].nativeElement.textContent).toMatch('ID');
expect(headers[1].nativeElement.textContent).toMatch('Name');
expect(headers[2].nativeElement.textContent).toMatch('Name');

// Swap columns
grid.moveColumn(grid.columnList.first, grid.columnList.last);
fix.detectChanges();

expect(grid.columnList.first.field).toMatch('Name');
expect(grid.columnList.first.field).toMatch('IsEmployed');
expect(grid.columnList.last.field).toMatch('ID');

headers = fix.debugElement.queryAll(By.css(COLUMN_HEADER_CLASS));
expect(headers[0].nativeElement.textContent).toMatch('Name');
expect(headers[1].nativeElement.textContent).toMatch('ID');
expect(headers[0].nativeElement.textContent).toMatch('IsEmployed');
expect(headers[1].nativeElement.textContent).toMatch('Name');
});

it('should support adding and removing columns through a declared iterable', fakeAsync(/** columnList.changes rAF */() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<igx-chip *ngIf="displayPinnedChip" class="igx-grid__td--pinned-chip" [disabled]="true" [displayDensity]="'compact'">{{ grid.resourceStrings.igx_grid_pinned_row_indicator }}</igx-chip>
</ng-template>
<ng-template #defaultCell>
<div igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
<div *ngIf="column.dataType !== 'boolean' || (column.dataType === 'boolean' && this.formatter)"
igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
[cssClass]="highlightClass"
[activeCssClass]="activeHighlightClass"
[groupName]="gridID"
Expand All @@ -13,18 +14,27 @@
? (value | date:column.pipeArgs.format:column.pipeArgs.timezone:grid.locale)
: column.dataType === 'currency'
? (value | currency:currencyCode:column.pipeArgs.display:column.pipeArgs.digitsInfo:grid.locale)
: column.dataType === 'percent' ? (value | percent:column.pipeArgs.digitsInfo:grid.locale) : value"
: column.dataType === 'percent'
? (value | percent:column.pipeArgs.digitsInfo:grid.locale)
: value"
[row]="rowData"
[column]="this.column.field"
[containerClass]="'igx-grid__td-text'"
[metadata]="searchMetadata">{{ formatter ? (value | columnFormatter:formatter:rowData) : column.dataType === "number"
? (value | number:column.pipeArgs.digitsInfo:grid.locale) : (column.dataType === 'date' || column.dataType === 'time' || column.dataType === 'dateTime')
? (value | date:column.pipeArgs.format:column.pipeArgs.timezone:grid.locale) : column.dataType === 'currency'
? (value | currency:currencyCode:column.pipeArgs.display:column.pipeArgs.digitsInfo:grid.locale) : column.dataType === 'percent'
? (value | percent:column.pipeArgs.digitsInfo:grid.locale) : value }}</div>
? (value | percent:column.pipeArgs.digitsInfo:grid.locale) : value}}</div>

<igx-icon
*ngIf="column.dataType === 'boolean' && !this.formatter"
[ngClass]="{ 'igx-icon--success': value, 'igx-icon--error': !value }"
>{{ value ? "check" : "close" }}</igx-icon
>
</ng-template>
<ng-template #addRowCell let-cell="cell">
<div igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
<div *ngIf="column.dataType !== 'boolean' || (column.dataType === 'boolean' && this.formatter)"
igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
[cssClass]="highlightClass"
[activeCssClass]="activeHighlightClass"
[groupName]="gridID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
</ng-template>
<ng-template #defaultCell>
<div *ngIf="column.dataType !== 'boolean'"
<div *ngIf="column.dataType !== 'boolean' || (column.dataType === 'boolean' && this.formatter)"
igxTextHighlight
class="igx-grid__td-text"
style="pointer-events: none;"
Expand Down Expand Up @@ -46,13 +46,15 @@
: value
}}</div>
<igx-icon
*ngIf="column.dataType === 'boolean'"
*ngIf="column.dataType === 'boolean' && !this.formatter"
[ngClass]="{ 'igx-icon--success': value, 'igx-icon--error': !value }"
>{{ value ? "check" : "close" }}</igx-icon
>
</ng-template>
<ng-template #addRowCell let-cell="cell">
<div igxTextHighlight class="igx-grid__td-text" style="pointer-events: none"
<div *ngIf="column.dataType !== 'boolean' || (column.dataType === 'boolean' && this.formatter)"
igxTextHighlight class="igx-grid__td-text"
style="pointer-events: none"
[cssClass]="highlightClass"
[activeCssClass]="activeHighlightClass"
[groupName]="gridID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ export class ColumnCellFormatterComponent extends BasicGridComponent {
public containsY(_: number, data: { ID: number; Name: string }) {
return data.Name.includes('y') ? 'true' : 'false';
}

public boolFormatter(value: boolean): string {
return value ? 'check' : 'close';
}
}

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ export class SampleTestData {

/* Data fields: ID: number, Name: string; 3 items. */
public static personIDNameData = () => ([
{ ID: 1, Name: 'Johny' },
{ ID: 2, Name: 'Sally' },
{ ID: 3, Name: 'Tim' }
{ ID: 1, IsEmployed: true, Name: 'Johny' },
{ ID: 2, IsEmployed: true, Name: 'Sally' },
{ ID: 3, IsEmployed: false, Name: 'Tim' },
]);

/* Data fields: FirstName: string, LastName: string, age:number; 3 items. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export class ColumnDefinitions {

public static idNameFormatter = `
<igx-column field="ID" [formatter]="multiplier"></igx-column>
<igx-column field="IsEmployed" [dataType]="'boolean'"></igx-column>
<igx-column field="Name"></igx-column>
`;

Expand Down
2 changes: 1 addition & 1 deletion src/app/grid-formatting/grid-formatting.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h3>Grid with local data</h3>
</igx-column>
<igx-column width="200px" field="OrderDate" header="Unbound column" [filterable]="true" [hasSummary]="true" [sortable]="true">
<ng-template igxCell let-cell="cell" let-val>
{{ cell.rowData.UnitsInStock }}
{{ getVal(cell) }}
</ng-template>
</igx-column>
<igx-column field="ReorderLevel" width="200px" [sortable]="true" [filterable]="true" [dataType]="'number'" [hasSummary]="false">
Expand Down