src/app/shared/components/select-dropdown/select-dropdown.component.ts
selector | crm-select-dropdown |
styleUrls | select-dropdown.component.scss |
templateUrl | ./select-dropdown.component.html |
Properties |
Methods |
|
Inputs |
Outputs |
constructor(formBuilder: FormBuilder)
|
||||||||
Parameters :
|
items
|
Type:
Default value: |
right
|
Type:
Default value: |
select
|
$event type: EventEmitter
|
Private filterItems | ||||||||
filterItems(text: string)
|
||||||||
Parameters :
Returns :
any
|
Private initForm |
initForm()
|
Returns :
void
|
ngOnChanges | ||||||||
ngOnChanges(simpleChanges: SimpleChanges)
|
||||||||
Parameters :
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
onSelect | ||||||||
onSelect(item: SelectItem)
|
||||||||
Parameters :
Returns :
void
|
filteredItems |
filteredItems:
|
Type : SelectItem[]
|
Default value : []
|
searchForm |
searchForm:
|
Type : FormGroup
|
searchFormChangeSubscription |
searchFormChangeSubscription:
|
Type : Subscription
|
import {
Component, OnInit, OnDestroy, Input, Output, EventEmitter, ViewChild, OnChanges,
SimpleChanges
} from "@angular/core";
import {FormGroup, FormBuilder} from "@angular/forms";
import {Subscription} from "rxjs";
import {SelectItem} from "../../models/select-item";
@Component({
selector: 'crm-select-dropdown',
templateUrl: './select-dropdown.component.html',
styleUrls: ['./select-dropdown.component.scss']
})
export class SelectDropdownComponent implements OnInit, OnDestroy, OnChanges {
searchForm: FormGroup;
searchFormChangeSubscription: Subscription;
@Input() items: SelectItem[] = [];
@Input() right: boolean = false;
@Output() select = new EventEmitter<SelectItem>();
constructor(private formBuilder: FormBuilder) {
}
filteredItems: SelectItem[] = [];
ngOnInit() {
this.initForm();
this.filteredItems = this.filterItems('');
this.searchFormChangeSubscription = this.searchForm.valueChanges.debounceTime(200)
.subscribe(value => this.filteredItems = this.filterItems(value.text))
}
ngOnChanges(simpleChanges: SimpleChanges) {
if(simpleChanges.hasOwnProperty('items')) {
this.filteredItems = this.filterItems('');
}
}
ngOnDestroy() {
this.searchFormChangeSubscription.unsubscribe();
}
onSelect(item: SelectItem) {
this.select.emit(item);
}
private initForm() {
this.searchForm = this.formBuilder.group({
text: ['']
})
}
private filterItems(text: string) {
let filteredItems = [];
if (text.length > 0) {
this.items.forEach(item => {
let span = document.createElement("span");
span.innerHTML = item.text;
if (span.innerText.toLowerCase().includes(text.toLowerCase())) {
filteredItems.push(item)
}
})
} else {
filteredItems.push(...this.items);
}
return filteredItems.splice(0, 10);
}
}
<div class="btn-group" role="group" dropdown>
<!--<button class="btn btn-link text-muted" dropdownToggle>-->
<!--<span class="fa fa-group fa-lg"></span>-->
<!--</button>-->
<div dropdownToggle class="text-muted">
<ng-content></ng-content>
</div>
<div class="dropdown-menu search-dropdown" [class.dropdown-menu-right]="right" bs-dropdown-menu>
<form [formGroup]="searchForm" class="form-horizontal">
<input type="text"
class="form-control"
placeholder="Start typing here for search..."
formControlName="text"
(click)="$event.stopPropagation()"
>
</form>
<button class="dropdown-item btn btn-link" *ngFor="let item of filteredItems" (click)="onSelect(item)">
<p [innerHTML]="item.text" class="select-item"></p>
</button>
</div>
</div>