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(module:cascader): fix nz-cascader don't refresh when nzOptions binding data changed (#219) #221

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions src/components/cascader/nz-cascader.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,10 @@ export class NzCascaderComponent implements OnInit, OnDestroy, OnChanges, AfterV

/** clear the input box and selected options */
_clearSelection(event: Event): void {
event.preventDefault();
event.stopPropagation();
if (event) {
event.preventDefault();
event.stopPropagation();
}

this._displayLabel = '';
this._displayLabelIsTemplate = false;
Expand Down Expand Up @@ -1031,6 +1033,16 @@ export class NzCascaderComponent implements OnInit, OnDestroy, OnChanges, AfterV
this._removeHostClass(`${this._prefixCls}-picker-disabled`);
}
}

const nzOptions = changes['nzOptions'];
if (nzOptions && !nzOptions.isFirstChange()) {
this._nzColumns.splice(0);
const newOptions: CascaderOption[] = nzOptions.currentValue;
if (newOptions && newOptions.length) {
this._nzColumns.push(newOptions);
this._clearSelection(null);
}
}
}

ngAfterViewInit(): void {
Expand Down
58 changes: 54 additions & 4 deletions src/showcase/nz-demo-cascader/nz-demo-cascader-basic.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component, OnInit} from '@angular/core';

const options = [{
const init_options = [{
value: 'zhejiang',
label: 'Zhejiang',
children: [{
Expand All @@ -26,6 +26,32 @@ const options = [{
}],
}];

const other_options = [{
value: 'fujian',
label: 'Fujian',
children: [{
value: 'xiamen',
label: 'Xiamen',
children: [{
value: 'Kulangsu',
label: 'Kulangsu',
isLeaf: true
}],
}],
}, {
value: 'guangxi',
label: 'Guangxi',
children: [{
value: 'guilin',
label: 'Guilin',
children: [{
value: 'Lijiang',
label: 'Li Jiang River',
isLeaf: true
}],
}],
}];


@Component({
selector: 'nz-demo-cascader-basic',
Expand All @@ -36,12 +62,24 @@ const options = [{
[(ngModel)]="_value"
(ngModelChange)="_console($event)"
(nzChange)="_console($event)">
</nz-cascader>`,
styles : []
</nz-cascader>
<a href="javascript:;" (click)="_changeNzOptions()" class="change-options">
Change Options
</a>
`,
styles : [
`
.change-options {
display: inline-block;
font-size: 12px;
margin-top: 8px;
}
`
]
})
export class NzDemoCascaderBasicComponent implements OnInit {
/** init data */
_options = options;
_options = null;

_value: any[] = null;

Expand All @@ -53,6 +91,18 @@ export class NzDemoCascaderBasicComponent implements OnInit {
}

ngOnInit() {
// let's set nzOptions in a asynchronous way
setTimeout(() => {
this._options = init_options;
}, 100);
}

_changeNzOptions(): void {
if (this._options === init_options) {
this._options = other_options;
} else {
this._options = init_options;
}
}
}