[英]PrimeNg AutoComplete
Well I'm following the documentation of primeNg and I can't get the autocomplete to show the suggestions好吧,我正在关注 primeNg 的文档,但我无法获得自动完成功能来显示建议
import { AutoCompleteModule } from 'primeng/autocomplete';
import { AutoCompleteModule } from 'primeng/autocomplete';
imports: [CommonModule, FormsModule, AutoCompleteModule],
imports: [CommonModule, FormsModule, AutoCompleteModule],
my file .html shows this
<div fxLayout="column">
<div>
<h1>Buscador de Héroes</h1>
<p-divider></p-divider>
</div>
<div>
<p-autoComplete [(ngModel)]="selectedHero" [suggestions]="cambiar()" (completeMethod)="filterHeros($event)" field="name" [minLength]="1"></p-autoComplete>
</div>
</div>
my file component show this
import { Component, OnInit } from '@angular/core';
import { Heroe } from '../../interface/heroes.interface';
import { HeroesService } from '../../services/heroes.service';
@Component({
selector: 'app-buscar',
templateUrl: './buscar.component.html',
styles: [
]
})
export class BuscarComponent implements OnInit {
constructor(private heroesService: HeroesService) { }
ngOnInit(): void {
this.heroesService.getHeroes()
.subscribe(heroes => this.heroes = heroes );
}
selectedHero!: Heroe;
heroes:Heroe[] = [];
filteredHeros:Heroe[] = [];
filterHeros(event:any){
let filtered : Heroe[]= [];
let query = event.query;
for (let i = 0; i < this.heroes.length; i++) {
let heroe = this.heroes[i];
if (heroe.superhero.toLowerCase().indexOf(query.toLowerCase()) == 0) {
filtered.push(heroe);
}
}
this.filteredHeros = filtered;
console.log(this.filteredHeros); // When I print this to the console I can see the
// search results in the console, however I can't get them to show up in the autocomplete
}
cambiar(){
let mostrar:any[] = [];
for (let i = 0; i < this.filteredHeros.length; i++){
mostrar[i] = this.filteredHeros[i].superhero
}
return mostrar;
}
}
and my service
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { Heroe } from '../interface/heroes.interface';
@Injectable({
providedIn: 'root'
})
export class HeroesService {
private baseUrl: string = environment.baseUrl;
constructor(private http: HttpClient) { }
getHeroes():Observable<Heroe[]> {
return this.http.get<Heroe[]>(`${ this.baseUrl }/heroes`)
}
getHeroesPorId(id:string):Observable<Heroe> {
return this.http.get<Heroe>(`${ this.baseUrl }/heroes/${id}`);
}
}
In the primeNg documentation it appears as在 primeNg 文档中它显示为
name:suggestions Type:array Default:null Description:An array of suggestions to display.名称:建议类型:数组默认值:null 描述:要显示的建议数组。
I have tried to send the array as type string[] and as any[] but without success.我试图将数组作为类型 string[] 和任何 [] 发送,但没有成功。 I hope you can help me thank you very much
我希望你能帮助我,非常感谢
try using cambiar method inside the filterHeros method, like this,尝试在 filterHeros 方法中使用 cambiar 方法,就像这样,
filterHeros(event:any){
let filtered : Heroe[]= [];
let query = event.query;
for (let i = 0; i < this.heroes.length; i++) {
let heroe = this.heroes[i];
if (heroe.superhero.toLowerCase().indexOf(query.toLowerCase()) == 0) {
filtered.push(heroe);
}
}
this.filteredHeros = filtered;
this.cambiar();
}
Because it is updated only on load, we have updated on (completeMethod) event also to get back filtered array因为它只在加载时更新,我们也更新了 (completeMethod) 事件以获取过滤后的数组
Issue 1第 1 期
Since you are filtering by superhero
property,由于您正在按
superhero
属性进行过滤,
Incorrect:不正确:
<p-autoComplete
...
field="name">
</p-autoComplete>
Solution for Issue 1问题 1 的解决方案
Change as below:更改如下:
<p-autoComplete
...
field="superhero">
</p-autoComplete>
Issue 2第 2 期
While in this function, you are returning a string array.在这个 function 中,您返回一个字符串数组。 While in HTML, you apply
field="name"
, which this field
attribute is worked with object array (Refer to PrimeNG | AutoComplete (Object section) ).而在 HTML 中,您应用
field="name"
,该field
属性与 object 数组一起使用(请参阅PrimeNG | AutoComplete(对象部分) )。
cambiar(){
let mostrar:any[] = [];
for (let i = 0; i < this.filteredHeros.length; i++){
mostrar[i] = this.filteredHeros[i].superhero
}
return mostrar;
}
Solution 1 for Issue 2问题 2 的解决方案 1
Return filteredHeros
array返回
filteredHeros
后的英雄数组
cambiar() {
return this.filteredHeros;
}
OR或者
Solution 2 for Issue 2问题 2 的解决方案 2
Pass filteredHeros
to [suggestions]
. filteredHeros
传递给[suggestions]
。
<p-autoComplete
...
[suggestions]="filteredHeros">
</p-autoComplete>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.