html代码
<nz-select formControlName="no" [nzSize]="'default'" (nzOnSearch)="customerFilter($event)"
[nzNotFoundContent]="'无法找到'" nzShowSearch allowClear="true">
<nz-option *ngFor="let a of tempList" [nzLabel]="a.label" [nzValue]="a.value"></nz-option>
</nz-select>
ts代码
qryAll() {//查询所有,获取list,初始化到选择框
const params = {xxx: 'xxx'}
this.service.getAllCustomer(params).subscribe(_data => {
this.customerList = _data['retList'];
this.tempList = _data['retList'];
})
}
//搜索
tempList = [];
customerFilter(no) {
this.tempList = [];
this.tempList.splice(0, this.tempList.length);
this.customerList.forEach(element => {
if (element.customerName.indexOf(no) !== -1)
this.tempList.push(element);
});
}
要把qryAll()放到onInit()中,刚加载时就执行。
扩展:splice()方法使用
splice() 方法用于添加或删除数组中的元素。
语法:array.splice(index,howmany,item1,…,itemX)
index 必输(number型,开始插入或删除的数组元素的下标)
howmany 选输(number型,表示个数,输则表示删除)
item1,itemX 选输(要添加到数组的新元素)