zoukankan      html  css  js  c++  java
  • 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="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>
    html
    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)
        }
    }
    ts
    @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; });
        }
    }
    services

    参考:

    https://www.primefaces.org/primeng/#/autocomplete

  • 相关阅读:
    Android Camera子系统之Linux C应用开发人员View
    【Android】把外部文件拷贝的AVD安卓模拟器上的sdcard上,而且在AVD中浏览sdcard的文件
    HDU 2196 Computer(求树上每一个节点到其他点的最远距离)
    HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树
    scala 变量定义,基本操作符
    mybatis or
    Nginx
    hessian协议原理
    同一台电脑上装两个或两个以上的tomcat服务器
    端口号
  • 原文地址:https://www.cnblogs.com/wdtzms/p/6766208.html
Copyright © 2011-2022 走看看