PrimeNG之Input(一)
Input之AutoComplete
--自动完成功能是输入组件,提供实时的建议当打字。
Import
import {AutoCompleteModule} from 'primeng/primeng';
demo code
<h3 class="first">Basic</h3> <p-autoComplete [(ngModel)]="country" [suggestions]="filteredCountriesSingle" (completeMethod)="filterCountrySingle($event)" field="name" [size]="30" placeholder="Countries" [minLength]="1"></p-autoComplete> <span style="margin-left:10px">Country: {{country ? country.name||country : 'none'}}</span> <h3>Advanced</h3> <p-autoComplete [(ngModel)]="brand" [suggestions]="filteredBrands" (completeMethod)="filterBrands($event)" [size]="30" [minLength]="1" placeholder="Hint: type 'v' or 'f'" [dropdown]="true" (onDropdownClick)="handleDropdownClick($event)"> <ng-template let-brand pTemplate="item"> <div class="ui-helper-clearfix" style="border-bottom:1px solid #D5D5D5"> <img src="showcase/resources/demo/images/car/{{brand}}.gif" style="width:32px;display:inline-block;margin:5px 0 2px 5px"/> <div style="font-size:18px;float:right;margin:10px 10px 0 0">{{brand}}</div> </div> </ng-template> </p-autoComplete> <span style="margin-left:50px">Brand: {{brand||'none'}}</span> <h3>Multiple</h3> <p-autoComplete [(ngModel)]="countries" [suggestions]="filteredCountriesMultiple" (completeMethod)="filterCountryMultiple($event)" styleClass="wid100" [minLength]="1" placeholder="Countries" field="name" [multiple]="true"> </p-autoComplete> <ul> <li *ngFor="let c of countries">{{c.name}}</li> </ul>
export class AutoCompleteDemo { country: any; countries: any[]; filteredCountriesSingle: any[]; filteredCountriesMultiple: any[]; brands: string[] = ['Audi','BMW','Fiat','Ford','Honda','Jaguar','Mercedes','Renault','Volvo','VW']; filteredBrands: any[]; brand: string; constructor(private countryService: CountryService) { } filterCountrySingle(event) { let query = event.query; this.countryService.getCountries().then(countries => { this.filteredCountriesSingle = this.filterCountry(query, countries); }); } filterCountryMultiple(event) { let query = event.query; this.countryService.getCountries().then(countries => { this.filteredCountriesMultiple = this.filterCountry(query, countries); }); } filterCountry(query, countries: any[]):any[] { //in a real application, make a request to a remote url with the query and return filtered results, for demo we filter at client side let filtered : any[] = []; for(let i = 0; i < countries.length; i++) { let country = countries[i]; if(country.name.toLowerCase().indexOf(query.toLowerCase()) == 0) { filtered.push(country); } } return filtered; } filterBrands(event) { this.filteredBrands = []; for(let i = 0; i < this.brands.length; i++) { let brand = this.brands[i]; if(brand.toLowerCase().indexOf(event.query.toLowerCase()) == 0) { this.filteredBrands.push(brand); } } } handleDropdownClick() { this.filteredBrands = []; //mimic remote call setTimeout(() => { this.filteredBrands = this.brands; }, 100) } }
@Injectable() export class CountryService { constructor(private http: Http) {} getCountries() { return this.http.get('showcase/resources/data/countries.json') .toPromise() .then(res => <any[]> res.json().data) .then(data => { return data; }); } }
参考:
作者:地平线很远
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。