Angular PrimeNG Form MultiSelect高级模板和过滤组件

Angular PrimeNG Form MultiSelect高级模板和过滤组件

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,这个框架用于制作响应式网站,非常容易。在这篇文章中,我们将看到如何在Angular PrimeNG中使用Form MultiSelect Advanced with Templating and Filtering Component

多选组件可用于从菜单中选择多个值。模板组件主要用于提供模板,过滤 组件用于从给定列表中过滤选项。

语法:

<p-multiSelect [options]="countries" 
               [(ngModel)]="selectedCountries1" 
               defaultLabel="Select a Country"
               class="multiselect-custom">
    <ng-template let-value pTemplate="selectedItems">
        ...
    </ng-template>
</p-multiSelect>

创建Angular应用程序和模块安装。

  • 使用以下命令创建一个Angular应用程序。
ng new appname
  • 在创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
  • 在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save

项目结构:完成上述步骤后,项目结构将如下所示。

Angular PrimeNG Form MultiSelect高级模板和过滤组件

示例1:本示例演示了Angular PrimeNG中带有模板和过滤组件的Form MultiSelect Advanced的基本用法。

  • app.component.html
<div style="text-align:center">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>
        Angular PrimeNG Form MultiSelect 
        Advanced with Templating and 
        Filtering Component
    </h4>
  
    <h5>Templating</h5>
    <p-multiSelect [options]="countries" 
                   [(ngModel)]="selectedCountries1" 
                   defaultLabel="Select a Country"
                   optionLabel="name"
                   class="multiselect-custom">
        <ng-template let-value pTemplate="selectedItems">
            <div class="country-item country-item-value" 
                 *ngFor="let option of selectedCountries1">
                <div>{{ option.name }}</div>
            </div>
            <div *ngIf=
      "!selectedCountries1 || selectedCountries1.length === 0" 
                 class="country-placeholder">
                Select Countries
            </div>
        </ng-template>
        <ng-template let-country pTemplate="item">
            <div class="country-item">
                <div>{{ country.name }}</div>
            </div>
        </ng-template>
    </p-multiSelect>
</div>
  • app.component.ts
import { Component } from '@angular/core';
import {
    trigger,
    state,
    style,
    transition,
    animate,
} from '@angular/animations';
import { SelectItem, PrimeNGConfig } from 'primeng/api';
import { CountryService } from './countryservice';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    providers: [CountryService],
})
export class AppComponent {
    selectedCities: string[] = [];
    selectedCountries1: string[] = [];
    selectedCountries2: string[] = [];
    items: SelectItem[];
    item: string;
    cities: any[];
    countries: any[];
  
    constructor(
        private countryService: CountryService,
        private primengConfig: PrimeNGConfig
    ) {
        this.items = [];
        this.countryService.getCountries().then((countries) => {
            this.items = countries;
        });
  
        this.countries = [
            { name: 'India', code: 'AU' },
            { name: 'Brazil', code: 'BR' },
            { name: 'China', code: 'CN' },
            { name: 'Egypt', code: 'EG' },
            { name: 'France', code: 'FR' },
            { name: 'Germany', code: 'DE' },
            { name: 'Spain', code: 'ES' },
            { name: 'United States', code: 'US' },
        ];
    }
  
    ngOnInit() {
        this.primengConfig.ripple = true;
    }
}
  • app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { MultiSelectModule } from 'primeng/multiselect';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MultiSelectModule,
        FormsModule,
        HttpClientModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

输出:

Angular PrimeNG Form MultiSelect高级模板和过滤组件

例子2:这是另一个例子,通过禁用输入选项,演示了Angular PrimeNG中带有模板和过滤组件的Form MultiSelect Advanced的使用。

  • app.component.html
<div style="text-align:center">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>A computer science portal for geeks</h3>
    <h4>
        Angular PrimeNG Form MultiSelect 
        Advanced with Templating and 
        Filtering Component
    </h4>
  
    <h5>Templating</h5>
    <p-multiSelect [options]="countries" 
                   [(ngModel)]="selectedCountries1" 
                   defaultLabel="Select a Country"
                   optionLabel="name" 
                   disabled="true"
                   class="multiselect-custom">
        <ng-template let-value pTemplate="selectedItems">
            <div class="country-item country-item-value" 
                 *ngFor="let option of selectedCountries1">
                <div>{{ option.name }}</div>
            </div>
            <div *ngIf=
        "!selectedCountries1 || selectedCountries1.length === 0"
                 class="country-placeholder">
                Select Countries
            </div>
        </ng-template>
        <ng-template let-country pTemplate="item">
            <div class="country-item">
                <div>{{ country.name }}</div>
            </div>
        </ng-template>
    </p-multiSelect>
</div>
  • app.component.ts
import { Component } from '@angular/core';
import {
    trigger,
    state,
    style,
    transition,
    animate,
} from '@angular/animations';
import { SelectItem, PrimeNGConfig } from 'primeng/api';
import { CountryService } from './countryservice';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss'],
    providers: [CountryService],
})
export class AppComponent {
    selectedCities: string[] = [];
    selectedCountries1: string[] = [];
    selectedCountries2: string[] = [];
    items: SelectItem[];
    item: string;
    cities: any[];
    countries: any[];
  
    constructor(
        private countryService: CountryService,
        private primengConfig: PrimeNGConfig
    ) {
        this.items = [];
        this.countryService.getCountries().then((countries) => {
            this.items = countries;
        });
  
        this.countries = [
            { name: 'India', code: 'AU' },
            { name: 'Brazil', code: 'BR' },
            { name: 'China', code: 'CN' },
            { name: 'Egypt', code: 'EG' },
            { name: 'France', code: 'FR' },
            { name: 'Germany', code: 'DE' },
            { name: 'Spain', code: 'ES' },
            { name: 'United States', code: 'US' },
        ];
    }
  
    ngOnInit() {
        this.primengConfig.ripple = true;
    }
}
  • app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { MultiSelectModule } from 'primeng/multiselect';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MultiSelectModule,
        FormsModule,
        HttpClientModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

输出:

Angular PrimeNG Form MultiSelect高级模板和过滤组件

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程