Skip to content

Commit 3f6e6f9

Browse files
committed
Fix error ExpressionChangedAfterItHasBeenCheckedError
1 parent 093b8e9 commit 3f6e6f9

File tree

15 files changed

+47
-31
lines changed

15 files changed

+47
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,9 @@ export class TreeviewConfig {
155155
- Upgrade to Angular 10.
156156
- Upgrade Bootstrap to 4.5.1
157157
- Remove fontawesome
158+
159+
# [10.0.2](https://www.npmjs.com/package/ngx-treeview) (2020-07-07)
160+
161+
### Enhancement:
162+
163+
- Fix error ExpressionChangedAfterItHasBeenCheckedError

projects/ngx-treeview/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-treeview",
3-
"version": "10.0.1",
3+
"version": "10.0.2",
44
"license": "MIT",
55
"description": "An Angular treeview component with checkbox",
66
"keywords": [

projects/ngx-treeview/src/lib/components/dropdown-treeview/dropdown-treeview.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="dropdown" ngxDropdown>
22
<button class="btn" [ngClass]="buttonClass" type="button" role="button" ngxDropdownToggle>
3-
{{getText()}}
3+
{{buttonLabel}}
44
</button>
55
<div ngxDropdownMenu aria-labelledby="dropdownMenu" (click)="$event.stopPropagation()">
66
<div class="dropdown-container">

projects/ngx-treeview/src/lib/components/dropdown-treeview/dropdown-treeview.component.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class DropdownTreeviewComponent {
1919
@Input() config: TreeviewConfig;
2020
@Output() selectedChange = new EventEmitter<any[]>(true);
2121
@Output() filterChange = new EventEmitter<string>();
22-
@ViewChild(TreeviewComponent) treeviewComponent: TreeviewComponent;
22+
@ViewChild(TreeviewComponent, { static: false }) treeviewComponent: TreeviewComponent;
23+
buttonLabel: string;
2324

2425
constructor(
2526
public i18n: TreeviewI18n,
@@ -28,11 +29,8 @@ export class DropdownTreeviewComponent {
2829
this.config = this.defaultConfig;
2930
}
3031

31-
getText(): string {
32-
return this.treeviewComponent ? this.i18n.getText(this.treeviewComponent.selection) : '';
33-
}
34-
3532
onSelectedChange(values: any[]): void {
33+
this.buttonLabel = this.i18n.getText(this.treeviewComponent.selection);
3634
this.selectedChange.emit(values);
3735
}
3836

projects/ngx-treeview/src/lib/components/treeview/treeview.component.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, Output, EventEmitter, SimpleChanges, OnChanges, TemplateRef } from '@angular/core';
1+
import { Component, Input, Output, EventEmitter, SimpleChanges, OnChanges, TemplateRef, OnInit } from '@angular/core';
22
import { isNil, includes } from 'lodash';
33
import { TreeviewI18n } from '../../models/treeview-i18n';
44
import { TreeviewItem, TreeviewSelection } from '../../models/treeview-item';
@@ -47,7 +47,7 @@ class FilterTreeviewItem extends TreeviewItem {
4747
templateUrl: './treeview.component.html',
4848
styleUrls: ['./treeview.component.scss']
4949
})
50-
export class TreeviewComponent implements OnChanges {
50+
export class TreeviewComponent implements OnChanges, OnInit {
5151
@Input() headerTemplate: TemplateRef<TreeviewHeaderTemplateContext>;
5252
@Input() itemTemplate: TemplateRef<TreeviewItemTemplateContext>;
5353
@Input() items: TreeviewItem[];
@@ -67,7 +67,6 @@ export class TreeviewComponent implements OnChanges {
6767
) {
6868
this.config = this.defaultConfig;
6969
this.allItem = new TreeviewItem({ text: 'All', value: undefined });
70-
this.createHeaderTemplateContext();
7170
}
7271

7372
get hasFilterItems(): boolean {
@@ -78,16 +77,18 @@ export class TreeviewComponent implements OnChanges {
7877
return `${this.config.maxHeight}`;
7978
}
8079

80+
ngOnInit(): void {
81+
this.createHeaderTemplateContext();
82+
this.generateSelection();
83+
}
84+
8185
ngOnChanges(changes: SimpleChanges): void {
8286
const itemsSimpleChange = changes.items;
83-
if (!isNil(itemsSimpleChange)) {
84-
if (!isNil(this.items)) {
85-
this.updateFilterItems();
86-
this.updateCollapsedOfAll();
87-
this.raiseSelectedChange();
88-
}
87+
if (!isNil(itemsSimpleChange) && !isNil(this.items)) {
88+
this.updateFilterItems();
89+
this.updateCollapsedOfAll();
90+
this.raiseSelectedChange();
8991
}
90-
this.createHeaderTemplateContext();
9192
}
9293

9394
onAllCollapseExpand(): void {
@@ -125,7 +126,9 @@ export class TreeviewComponent implements OnChanges {
125126
raiseSelectedChange(): void {
126127
this.generateSelection();
127128
const values = this.eventParser.getSelectedChange(this);
128-
this.selectedChange.emit(values);
129+
setTimeout(() => {
130+
this.selectedChange.emit(values);
131+
});
129132
}
130133

131134
private createHeaderTemplateContext(): void {

projects/ngx-treeview/src/lib/models/treeview-i18n.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export abstract class TreeviewI18n {
1414
export class DefaultTreeviewI18n extends TreeviewI18n {
1515
getText(selection: TreeviewSelection): string {
1616
if (selection.uncheckedItems.length === 0) {
17-
return this.getAllCheckboxText();
17+
if (selection.checkedItems.length > 0) {
18+
return this.getAllCheckboxText();
19+
} else {
20+
return '';
21+
}
1822
}
1923

2024
switch (selection.checkedItems.length) {

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<a class="navbar-brand" href="#">ngx-treeview</a>
33
<div class="navbar-nav mr-auto"></div>
44
<a class="github-button" href="https://github.com/leovo2708/ngx-treeview" data-icon="octicon-star" data-size="large"
5-
aria-label="Star leovo2708/ngx-treeview on GitHub">Star</a>
5+
data-show-count="true" aria-label="Star leovo2708/ngx-treeview on GitHub">Star</a>
66
</header>
77
<div class="container-fluid">
88
<div class="row">

src/app/city/city.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { CityTreeviewI18n } from './city-treeview-i18n';
1212
]
1313
})
1414
export class CityComponent implements OnInit {
15-
@ViewChild(DropdownTreeviewComponent) dropdownTreeviewComponent: DropdownTreeviewComponent;
15+
@ViewChild(DropdownTreeviewComponent, { static: false }) dropdownTreeviewComponent: DropdownTreeviewComponent;
1616
cities: City[];
1717
selectedCities: City[];
1818
unselectedCities: City[];

src/app/default-treeview-i18n.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export class DefaultTreeviewI18n extends TreeviewI18n {
1212

1313
getText(selection: TreeviewSelection): string {
1414
if (selection.uncheckedItems.length === 0) {
15-
return this.i18n.language === 'en' ? 'All' : 'Tất cả';
15+
if (selection.checkedItems.length > 0) {
16+
return this.i18n.language === 'en' ? 'All' : 'Tất cả';
17+
} else {
18+
return '';
19+
}
1620
}
1721

1822
switch (selection.checkedItems.length) {

src/app/dropdown-treeview-select/dropdown-treeview-select.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class DropdownTreeviewSelectComponent implements OnChanges {
1818
@Input() items: TreeviewItem[];
1919
@Input() value: any;
2020
@Output() valueChange = new EventEmitter<any>();
21-
@ViewChild(DropdownTreeviewComponent) dropdownTreeviewComponent: DropdownTreeviewComponent;
21+
@ViewChild(DropdownTreeviewComponent, { static: false }) dropdownTreeviewComponent: DropdownTreeviewComponent;
2222
filterText: string;
2323
private dropdownTreeviewSelectI18n: DropdownTreeviewSelectI18n;
2424

src/app/product/product.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class ProductTreeviewConfig extends TreeviewConfig {
2525
]
2626
})
2727
export class ProductComponent implements OnInit {
28-
@ViewChild(TreeviewComponent) treeviewComponent: TreeviewComponent;
28+
@ViewChild(TreeviewComponent, { static: false }) treeviewComponent: TreeviewComponent;
2929
items: TreeviewItem[];
3030
rows: string[];
3131

@@ -58,7 +58,7 @@ export class ProductComponent implements OnInit {
5858
let isRemoved = false;
5959
for (const tmpItem of this.items) {
6060
if (tmpItem === item) {
61-
remove(this.items, item);
61+
this.items = remove(this.items, item);
6262
} else {
6363
isRemoved = TreeviewHelper.removeItem(tmpItem, item);
6464
if (isRemoved) {

src/app/room/room.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h3>Performance (
1313
<form class="d-inline-block">
1414
<label class="col-form-label mr-sm-2">Item category</label>
1515
<div class="d-inline-block">
16-
<ngx-dropdown-treeview [config]="config" [items]="items" (selectedChange)="values = $event">
16+
<ngx-dropdown-treeview [config]="config" [items]="rooms" (selectedChange)="values = $event">
1717
</ngx-dropdown-treeview>
1818
</div>
1919
</form>

src/app/room/room.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { RoomService } from './room.service';
1010
]
1111
})
1212
export class RoomComponent implements OnInit {
13-
items: TreeviewItem[];
13+
rooms: TreeviewItem[];
1414
values: any[];
1515
config = TreeviewConfig.create({
1616
hasAllCheckBox: true,
@@ -24,6 +24,6 @@ export class RoomComponent implements OnInit {
2424
) { }
2525

2626
ngOnInit(): void {
27-
this.items = this.service.getRooms();
27+
this.service.getRooms().subscribe(rooms => this.rooms = rooms);
2828
}
2929
}

src/app/room/room.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Injectable } from '@angular/core';
22
import { TreeviewItem } from 'ngx-treeview';
3+
import { Observable, of } from 'rxjs';
34

45
@Injectable()
56
export class RoomService {
6-
getRooms(): TreeviewItem[] {
7+
getRooms(): Observable<TreeviewItem[]> {
78
const items: TreeviewItem[] = [];
89
for (let i = 0; i < 1000; i++) {
910
const value: any = i === 0 ? { name: `${i}` } : i;
@@ -16,6 +17,6 @@ export class RoomService {
1617
items.push(item);
1718
}
1819

19-
return items;
20+
return of(items);
2021
}
2122
}

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<body>
1212
<ngx-app>Loading...</ngx-app>
13-
<script async defer src="https://buttons.github.io/buttons.js"></script>
13+
<script src="https://buttons.github.io/buttons.js"></script>
1414
</body>
1515

1616
</html>

0 commit comments

Comments
 (0)