Skip to content

Commit

Permalink
fix: Clear all selected values before new model write action
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Sep 27, 2017
1 parent 90eb354 commit c76d410
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/demo/app/select-multi.component.ts
Expand Up @@ -4,23 +4,24 @@ import { NgOption } from '@ng-select/ng-select';
@Component({
template: `
<label>Select multiple elements</label>
<ng-select
[items]="companies"
bindLabel="name"
[multiple]="true"
[(ngModel)]="selectedCompany">
<ng-select
[items]="companies"
bindLabel="name"
[multiple]="true"
[(ngModel)]="selectedCompanies">
</ng-select>
<p>
Selected value: {{selectedCompany | json}}
Selected value: {{selectedCompanies | json}} <br>
<button (click)="clearModel()" class="btn btn-secondary btn-sm">Clear model</button>
</p>
<label>Disabled multiple elements</label>
<ng-select
[items]="companies2"
bindLabel="name"
[multiple]="true"
[disabled]="disable"
[(ngModel)]="selectedCompanyDisabled">
<ng-select
[items]="companies2"
bindLabel="name"
[multiple]="true"
[disabled]="disable"
[(ngModel)]="selectedCompaniesDisabled">
</ng-select>
<br>
<button class="btn btn-secondary btn-sm" (click)="disable = !disable">Toggle disabled</button>
Expand All @@ -30,8 +31,8 @@ export class SelectMultiComponent {

companies: any[] = [];
companies2: any[] = [];
selectedCompany: any;
selectedCompanyDisabled: any;
selectedCompanies: any;
selectedCompaniesDisabled: any;
disable = true;

/* tslint:disable */
Expand All @@ -44,7 +45,11 @@ export class SelectMultiComponent {
this.companies2.push({ id: i, name: c });
});

this.selectedCompanyDisabled = [{ id: 0, name: 'Miškas' }, { id: 1, name: 'Žalias' }]
this.selectedCompaniesDisabled = [{ id: 0, name: 'Miškas' }, { id: 1, name: 'Žalias' }]
}

clearModel() {
this.selectedCompanies = [];
}
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/src/items-list.ts
Expand Up @@ -40,6 +40,7 @@ export class ItemsList {
clearSelected() {
this._selected.forEach((item) => {
item.selected = false;
item.marked = false;
});
this._selected = [];
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/src/ng-select.component.ts
Expand Up @@ -179,6 +179,7 @@ export class NgSelectComponent implements OnInit, ControlValueAccessor {
}

writeValue(value: any): void {
this.itemsList.clearSelected();
if (value) {
if (this.multiple) {
value.forEach(item => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/src/ng-select.types.ts
Expand Up @@ -2,6 +2,7 @@ export interface NgOption {
[name: string]: any;
selected?: boolean;
disabled?: boolean;
marked?: boolean;
label?: string;
value?: string;
}
Expand Down

0 comments on commit c76d410

Please sign in to comment.