ANGULAR 20.3.18 - SEARCH AND PAGINATION - CUSTOM FILTER USING PIPE

 

ANGULAR 20.3.18
Employee Search with Pagination 


πŸ”· 1. Overview

This feature allows users to:

✔ Search employees by Name, Designation, or Contact
✔ View results with Pagination
✔ Perform search without calling API again (client-side filtering)


πŸ”· 2. Technologies Used

  • Angular 20 (Standalone Components)
  • Template Driven Forms (ngModel)
  • Custom Pipe (filterEmployees)
  • ngx-pagination



πŸ”· 3. Service Layer (Data Fetching)

πŸ“Œ What?

Fetch employee data from API and store in service.

πŸ“Œ Why?

  • Centralized data storage
  • Avoid multiple API calls

πŸ“Œ Code

getAllEmployees(): void {
this.httpClient.get<any>(environment.apiUrl + 'Employees')
.subscribe({
next: response => {
this.employees = response.$values;
}
});
}

πŸ”· 4. Component Layer (UI Control)

πŸ“Œ What?

  • Calls service
  • Holds search input
  • Displays data

πŸ“Œ Code

searchTerm: string = '';
p: number = 1;
itemsPerPage: number = 5;

ngOnInit(): void {
this.employeesService.getAllEmployees();
}

πŸ”· 5. Custom Pipe (Filtering Logic)

πŸ“Œ What?

Filters employee list based on search text.

πŸ“Œ Why?

  • Reusable logic
  • Keeps component clean
  • Works automatically in template

πŸ“Œ Code

import { Pipe, PipeTransform } from '@angular/core';
import { Employee } from '../models/employee';

@Pipe({
name: 'filterEmployees',
standalone: true
})
export class FilterEmployeesPipe implements PipeTransform {

transform(employees: Employee[], searchTerm: string): Employee[] {

if (!employees || !searchTerm) return employees;

searchTerm = searchTerm.toLowerCase();

return employees.filter(emp =>
emp.EmployeeName.toLowerCase().includes(searchTerm) ||
emp.Designation.toLowerCase().includes(searchTerm) ||
(emp.Contact ?? '').includes(searchTerm)
);
}
}

πŸ”· 6. HTML (View Layer)

πŸ“Œ Search Box

<input type="text"
class="form-control mb-3"
placeholder="Search employee..."
[(ngModel)]="searchTerm"
(ngModelChange)="p = 1">

πŸ“Œ Display Data (Filter + Pagination)

<tr *ngFor="let emp of employeesService.employees
| filterEmployees: searchTerm
| paginate: { itemsPerPage: itemsPerPage, currentPage: p }">

πŸ“Œ Pagination Controls

<pagination-controls (pageChange)="p = $event"></pagination-controls>

πŸ”· 7. How It Works (Flow)

πŸ”„ Step-by-Step Flow

1. Component Loads

2. ngOnInit() calls getAllEmployees()

3. API returns employee list

4. Data stored in:
employeesService.employees

5. User types in search box

6. searchTerm updates (ngModel)

7. Pipe executes automatically:
filterEmployees(employees, searchTerm)

8. Filtered data passed to pagination

9. Paginated results shown in UI

πŸ”· 8. Important Concepts


✅ Why Pipe is Used?

Without PipeWith Pipe
Logic in component            Clean separation
Manual filtering            Automatic
Less reusable                Reusable

✅ Why No API Call for Search?

  • Data already loaded
  • Faster performance
  • Reduces server load

✅ Why Order Matters?

employees | filterEmployees | paginate

✔ First filter
✔ Then paginate

❌ Wrong:

employees | paginate | filterEmployees

πŸ”· 9. Advantages

✔ Fast (client-side filtering)
✔ Clean architecture
✔ Reusable pipe
✔ Works with pagination
✔ Angular 20 compatible


πŸ”· 10. Best Practice 

  • Use client-side filtering for small data
  • Use API filtering for large data

Comments

Popular posts from this blog

Interview Tips: Dot NET Framework vs Net CORE

FREE Webinar: Run Your Own Independent DeepSeek LLM

OOP Concept in real-time scenario: Mobile Device Management Software