Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropdown filter demo update #14887

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions src/app/showcase/doc/dropdown/customfilterdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { Component, OnInit } from '@angular/core';
import { Code } from '../../domain/code';
import { DropdownFilterOptions } from 'primeng/dropdown';

interface City {
name: string;
code: string;
}

@Component({
selector: 'dropdown-custom-filter-demo',
template: `
<app-docsectiontext>
<p>Custom filter can be applied with the <i>filterTemplate</i>.</p>
</app-docsectiontext>
<div class="card flex justify-content-center">
<p-dropdown [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" [filter]="true" filterBy="name" [showClear]="true" placeholder="Select a Country">
<ng-template pTemplate="filter" let-options="options">
<div class="flex gap-1">
<div class="p-inputgroup" (click)="$event.stopPropagation()">
<span class="p-inputgroup-addon"><i class="pi pi-search"></i></span>
<input type="text" pInputText placeholder="Filter" [(ngModel)]="filterValue" (keyup)="customFilterFunction($event, options)" />
</div>
<button pButton icon="pi pi-times" (click)="resetFunction(options)"></button>
</div>
</ng-template>
<ng-template pTemplate="selectedItem" let-selectedOption>
<div class="flex align-items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + selectedCountry.code.toLowerCase()" style="width: 18px" />
<div>{{ selectedOption.name }}</div>
</div>
</ng-template>
<ng-template let-country pTemplate="item">
<div class="flex align-items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + country.code.toLowerCase()" style="width: 18px" />
<div>{{ country.name }}</div>
</div>
</ng-template>
</p-dropdown>
</div>
<app-code [code]="code" selector="dropdown-custom-filter-demo"></app-code>
`
})
export class CustomFilterDoc implements OnInit {
countries: City[] | undefined;

selectedCountry: string | undefined;

filterValue: string | undefined = '';

ngOnInit() {
this.countries = [
{ name: 'Australia', code: 'AU' },
{ name: 'Brazil', code: 'BR' },
{ name: 'China', code: 'CN' },
{ name: 'Egypt', code: 'EG' },
{ name: 'France', code: 'FR' },
{ name: 'Germany', code: 'DE' },
{ name: 'India', code: 'IN' },
{ name: 'Japan', code: 'JP' },
{ name: 'Spain', code: 'ES' },
{ name: 'United States', code: 'US' }
];
}

resetFunction(options: DropdownFilterOptions) {
options.reset();
this.filterValue = '';
}

customFilterFunction(event: KeyboardEvent, options: DropdownFilterOptions) {
options.filter(event);
}

code: Code = {
basic: `<p-dropdown [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" [filter]="true" filterBy="name" [showClear]="true" placeholder="Select a Country" styleClass="w-20rem">
<ng-template pTemplate="filter" let-options="options">
<div class="flex gap-1">
<div class="p-inputgroup" (click)="$event.stopPropagation()">
<span class="p-inputgroup-addon"><i class="pi pi-search"></i></span>
<input type="text" pInputText placeholder="Filter" [(ngModel)]="filterValue" (keyup)="customFilterFunction($event, options)" />
</div>
<button pButton icon="pi pi-times" (click)="resetFunction(options)"></button>
</div>
</ng-template>
<ng-template pTemplate="selectedItem" let-selectedOption>
<div class="flex align-items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + selectedCountry.code.toLowerCase()" style="width: 18px" />
<div>{{ selectedOption.name }}</div>
</div>
</ng-template>
<ng-template let-country pTemplate="item">
<div class="flex align-items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + country.code.toLowerCase()" style="width: 18px" />
<div>{{ country.name }}</div>
</div>
</ng-template>
</p-dropdown>`,

html: `<div class="card flex justify-content-center">
<p-dropdown [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" [filter]="true" filterBy="name" [showClear]="true" placeholder="Select a Country" styleClass="w-20rem">
<ng-template pTemplate="filter" let-options="options">
<div class="flex gap-1">
<div class="p-inputgroup" (click)="$event.stopPropagation()">
<span class="p-inputgroup-addon"><i class="pi pi-search"></i></span>
<input type="text" pInputText placeholder="Filter" [(ngModel)]="filterValue" (keyup)="customFilterFunction($event, options)" />
</div>
<button pButton icon="pi pi-times" (click)="resetFunction(options)"></button>
</div>
</ng-template>
<ng-template pTemplate="selectedItem" let-selectedOption>
<div class="flex align-items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + selectedCountry.code.toLowerCase()" style="width: 18px" />
<div>{{ selectedOption.name }}</div>
</div>
</ng-template>
<ng-template let-country pTemplate="item">
<div class="flex align-items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + country.code.toLowerCase()" style="width: 18px" />
<div>{{ country.name }}</div>
</div>
</ng-template>
</p-dropdown>
</div>`,

typescript: `
import { Component, OnInit } from '@angular/core';
import { DropdownFilterOptions } from 'primeng/dropdown';

interface City {
name: string;
code: string;
}
@Component({
selector: 'dropdown-custom-filter-demo',
templateUrl: './dropdown-custom-filter-demo.html'
})
export class DropdownCustomFilterDemo implements OnInit {
countries: City[] | undefined;

selectedCountry: string | undefined;

filterValue: string | undefined = '';

ngOnInit() {
this.countries = [
{ name: 'Australia', code: 'AU' },
{ name: 'Brazil', code: 'BR' },
{ name: 'China', code: 'CN' },
{ name: 'Egypt', code: 'EG' },
{ name: 'France', code: 'FR' },
{ name: 'Germany', code: 'DE' },
{ name: 'India', code: 'IN' },
{ name: 'Japan', code: 'JP' },
{ name: 'Spain', code: 'ES' },
{ name: 'United States', code: 'US' }
];
}

resetFunction(options: DropdownFilterOptions) {
options.reset();
this.filterValue = '';
}

customFilterFunction(event: KeyboardEvent, options: DropdownFilterOptions) {
options.filter(event);
}
}`
};
}
7 changes: 5 additions & 2 deletions src/app/showcase/doc/dropdown/dropdowndoc.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import { InvalidDoc } from './invaliddoc';
import { VirtualScrollDoc } from './virtualscrolldoc';
import { LazyVirtualScrollDoc } from './lazyvirtualscrolldoc';
import { FilterDoc } from './filterdoc';
import { CustomFilterDoc } from './customfilterdoc';
import { FloatLabelDoc } from './floatlabeldoc';
import { StyleDoc } from './styledoc';
import { AccessibilityDoc } from './accessibilitydoc';
import { ReactiveFormsDoc } from './reactiveformsdoc';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
@NgModule({
imports: [CommonModule, RouterModule, AppCodeModule, AppDocModule, FormsModule, ReactiveFormsModule, DropdownModule],
imports: [CommonModule, RouterModule, AppCodeModule, AppDocModule, FormsModule, ReactiveFormsModule, DropdownModule, ButtonModule, InputTextModule],
exports: [AppDocModule],
declarations: [ImportDoc, BasicDoc, EditableDoc, GroupDoc, TemplateDoc, DisabledDoc, InvalidDoc, VirtualScrollDoc, LazyVirtualScrollDoc, FilterDoc, FloatLabelDoc, StyleDoc, AccessibilityDoc, ReactiveFormsDoc]
declarations: [ImportDoc, BasicDoc, EditableDoc, GroupDoc, TemplateDoc, DisabledDoc, InvalidDoc, VirtualScrollDoc, LazyVirtualScrollDoc,CustomFilterDoc, FilterDoc, FloatLabelDoc, StyleDoc, AccessibilityDoc, ReactiveFormsDoc]
})
export class DropdownDocModule {}
15 changes: 14 additions & 1 deletion src/app/showcase/pages/dropdown/dropdowndemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { AccessibilityDoc } from '../../doc/dropdown/accessibilitydoc';
import { ReactiveFormsDoc } from '../../doc/dropdown/reactiveformsdoc';
import { LazyVirtualScrollDoc } from '../../doc/dropdown/lazyvirtualscrolldoc';
import { InvalidDoc } from '../../doc/dropdown/invaliddoc';
import { CustomFilterDoc } from '../../doc/dropdown/customfilterdoc';

@Component({
templateUrl: './dropdowndemo.html',
Expand Down Expand Up @@ -53,8 +54,20 @@ export class DropdownDemo {
{
id: 'filter',
label: 'Filter',
component: FilterDoc
children: [
{
id: 'filterbasic',
label: 'Basic',
component: FilterDoc
},
{
id: 'customfilter',
label: 'Custom Filter',
component: CustomFilterDoc
}
]
},

{
id: 'virtualscroll',
label: 'Virtual Scroll',
Expand Down
Loading