Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
fix(favorite-column): Favorite column is not modifying the datasource…
Browse files Browse the repository at this point in the history
… anymore.

Fixes #540
  • Loading branch information
yngrdyn authored and tomheller committed Feb 13, 2020
1 parent 1cf6639 commit f286db9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
1 change: 0 additions & 1 deletion components/table/src/simple-columns/favorite-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class DtFavoriteColumn<T> extends DtSimpleColumnBase<T> {
* datasource using the predefined name.
*/
_toggleFavorite(data: T): void {
data[this.name] = !data[this.name];
this.favoriteToggled.emit(data);
}
}
13 changes: 13 additions & 0 deletions components/table/src/simple-columns/simple-column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ describe('DtTable SimpleColumns', () => {
<dt-favorite-column
name="favorite"
[sortable]="isSortable"
(favoriteToggled)="toggleFavorite($event)"
></dt-favorite-column>
<dt-simple-text-column name="host"></dt-simple-text-column>
<dt-simple-number-column name="cpu" label="Cpu"></dt-simple-number-column>
Expand Down Expand Up @@ -877,6 +878,18 @@ class TestSimpleColumnsApp implements AfterViewInit {
return 'warning';
}
}

toggleFavorite(toggledRow: any): void {
// Modify a data clone and assign the changed state at the end
// to notify change detection about the dataChange in an array.
const modifiedData = [...this.data];
for (const row of modifiedData) {
if (row === toggledRow) {
row.favorite = !row.favorite;
}
}
this.data = modifiedData;
}
}

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<dt-favorite-column
name="favorite"
label="Favorite"
(change)="doSomething()"
(favoriteToggled)="toggleFavorite($event)"
></dt-favorite-column>
<dt-simple-text-column name="host" label="Host"></dt-simple-text-column>
<ng-container dtColumnDef="memory" dtColumnAlign="number">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import { Component, OnInit, ViewChild } from '@angular/core';

import { DtSort, DtTableDataSource } from '@dynatrace/barista-components/table';

interface HostRow {
favorite: boolean;
host: string;
memoryPerc: number;
memoryTotal: number;
}

@Component({
selector: 'dt-example-table-favorite-column',
templateUrl: './table-favorite-column-example.html',
})
export class DtExampleTableFavoriteColumn implements OnInit {
data: Array<{
favorite: boolean;
host: string;
memoryPerc: number;
memoryTotal: number;
}> = [
data: Array<HostRow> = [
{
favorite: true,
host: 'et-demo-2-win4',
Expand Down Expand Up @@ -81,7 +83,15 @@ export class DtExampleTableFavoriteColumn implements OnInit {
this.dataSource.sort = this.sortable;
}

doSomething(): void {
// noop
toggleFavorite(toggledRow: HostRow): void {
// Modify a data clone and assign the changed state at the end
// to notify change detection about the dataChange in an array.
const modifiedData = [...this.data];
for (const row of modifiedData) {
if (row === toggledRow) {
row.favorite = !row.favorite;
}
}
this.data = modifiedData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<dt-favorite-column
name="favorite"
sortable="false"
(change)="doSomething($event)"
(favoriteToggled)="toggleFavorite($event)"
></dt-favorite-column>
<dt-simple-text-column
name="host"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ import { Component } from '@angular/core';

import { DtTableDataSource } from '@dynatrace/barista-components/table';

interface HostRow {
favorite: boolean;
host: string;
memoryPerc: number;
memoryTotal: number;
}

@Component({
selector: 'dt-example-table-favorite-column-no-header',
templateUrl: './table-favorite-column-no-header-example.html',
})
export class DtExampleTableFavoriteColumnNoHeader {
data: Array<{
favorite: boolean;
host: string;
memoryPerc: number;
memoryTotal: number;
}> = [
data: Array<HostRow> = [
{
favorite: true,
host: 'et-demo-2-win4',
Expand Down Expand Up @@ -73,7 +75,15 @@ export class DtExampleTableFavoriteColumnNoHeader {
);
}

doSomething(_event: Event): void {
// noop
toggleFavorite(toggledRow: HostRow): void {
// Modify a data clone and assign the changed state at the end
// to notify change detection about the dataChange in an array.
const modifiedData = [...this.data];
for (const row of modifiedData) {
if (row === toggledRow) {
row.favorite = !row.favorite;
}
}
this.data = modifiedData;
}
}

0 comments on commit f286db9

Please sign in to comment.