Angular PrimeNG Form 多选择模板组件
Angular PrimeNG是一个开源框架,它有丰富的原生Angular UI组件,可以用来做很好的造型,这个框架用来做响应式网站非常容易。在这篇文章中,我们将看到Angular PrimeNG中的表格多选模板组件,同时了解提供的各种模板及其语法,在代码示例中会用到。
MultiSelect组件用于从菜单中选择多个值。Angular PrimeNG表格提供了各种模板,以便以一种结构化的方式组织起来,并可以进行分组,以便对表格进行分类。
Angular PrimeNG Form MultiSelect Templates Component:
- item。它用于定义菜单上的项目。
- group。它用于定义菜单的组别。
- selectedItems。它用于定义菜单的选定项目。
- header(标题)。它用于定义菜单的页眉。
- filter。它用于定义菜单的过滤器。
- empty。它用于定义一个空项目。
- emptyfilter。它用于过滤空菜单。
- footer:它用于定义菜单的页脚。
- loader。它用于定义菜单的加载器。
创建Angular应用程序和模块安装。
- 使用以下命令创建一个Angular应用程序。
ng new appname
- 在创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
- 在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构:安装成功后,将出现以下项目结构。
实例1:本实例演示了Angular PrimeNG中Form MultiSelect Templates Component的基本使用。
- 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
Templates Component
</h4>
<h5>Basic</h5>
<p-multiSelect [options]="cities"
[(ngModel)]="selectedCities1"
defaultLabel="Select a City"
optionLabel="name">
<ng-template pTemplate="header">
Header Content
</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.cities = [
{ name: "Nallasopara", code: "Nallasopara" },
{ name: "Naigaon", code: "Naigaon" },
{ name: "Mira road", code: "MR" },
{ name: "Vasai", code: "VASAI" },
{ name: "Virar", code: "virar" }
];
}
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 { }
输出:
例子2:这是另一个例子,演示了Angular PrimeNG中Form MultiSelect Templates Component的用法。
- 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
Templates Component
</h4>
<h5>Basic</h5>
<p-multiSelect [options]="cities"
[(ngModel)]="selectedCities1"
defaultLabel="Select a City"
optionLabel="name">
<ng-template pTemplate="footer">
footer Content
</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.cities = [
{ name: "Nallasopara", code: "Nallasopara" },
{ name: "Naigaon", code: "Naigaon" },
{ name: "Mira road", code: "MR" },
{ name: "Vasai", code: "VASAI" },
{ name: "Virar", code: "virar" }
];
}
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 { }
输出: