Skip to content

Commit

Permalink
Fixed #3081
Browse files Browse the repository at this point in the history
  • Loading branch information
Çağatay Çivici committed Jun 13, 2017
1 parent b584ea1 commit 35e209d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/app/components/dropdown/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ export class Dropdown implements OnInit,AfterViewInit,AfterContentInit,AfterView
public itemClick: boolean;

public hoveredItem: any;

public selectedOptionUpdated: boolean;

public filterValue: string;

public selectedOptionUpdated: boolean;

constructor(public el: ElementRef, public domHandler: DomHandler, public renderer: Renderer2, private cd: ChangeDetectorRef, public objectUtils: ObjectUtils) {}

Expand Down Expand Up @@ -199,6 +199,10 @@ export class Dropdown implements OnInit,AfterViewInit,AfterContentInit,AfterView
this.optionsToDisplay = this._options;
this.updateSelectedOption(this.value);
this.optionsChanged = true;

if(this.filterValue && this.filterValue.length) {
this.activateFilter();
}
}

ngAfterViewInit() {
Expand Down Expand Up @@ -484,15 +488,23 @@ export class Dropdown implements OnInit,AfterViewInit,AfterContentInit,AfterView
}

onFilter(event): void {
this.filterValue = event.target.value.toLowerCase();
this.optionsToDisplay = [];
this.activateFilter();
let inputValue = event.target.value.toLowerCase();
if(inputValue && inputValue.length) {
this.filterValue = inputValue;
this.activateFilter();
}
else {
this.filterValue = null;
this.optionsToDisplay = this.options;
}

this.optionsChanged = true;
}

activateFilter() {
let searchFields: string[] = this.filterBy.split(',');
if(this.options && this.options.length) {
this.optionsToDisplay = this.objectUtils.filter(this.options,searchFields,this.filterValue);
this.optionsToDisplay = this.objectUtils.filter(this.options, searchFields, this.filterValue);
this.optionsChanged = true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/app/showcase/components/dropdown/dropdowndemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ <h3>Model Driven Forms</h3>
</pre>

<h3>Filtering</h3>
<p>Options can be filtered using an input field in the overlay by enabling the filter property.
Defining filterBy property let search for label,value and subfields of value(Accepts multiple fields with a comma).</p>
<p>Options can be filtered using an input field in the overlay by enabling the filter property. By default filtering is done against
label of the SelectItem and filterBy property is available to choose one or more properties of the SelectItem API.</p>
<pre>
<code class="language-markup" pCode ngNonBindable>
&lt;p-dropdown [options]="cities" [(ngModel)]="selectedCity" [filter]="true"&gt;&lt;/p-dropdown&gt;
&lt;p-dropdown [options]="cities" [(ngModel)]="selectedCity" [filter]="true" filterBy="label,value.name"&gt;&lt;/p-dropdown&gt;
</code>
</pre>
Expand Down

0 comments on commit 35e209d

Please sign in to comment.