How Filtering Works
The filtering system consists of three main components:- FiltersComponent - Provides the UI controls for filtering
- HomeComponent - Orchestrates filter changes and data updates
- Query Parameters - Persist filter state in the URL
Filter Component Architecture
TheFiltersComponent (src/app/features/paginator/components/filters/filters.component.ts:1) uses Reactive Forms to manage filter state:
Key Features
- Reactive Forms: Uses
FormControlfor each filter - Event Emitters: Emits changes to parent component
- Null Handling: Properly handles empty state selections
- Type Safety: Strong typing with TypeScript
Implementing Filters
Add the Filters Component
In your template, add the filters component with required inputs and outputs:
Handle State Changes
Implement the event handlers that update query parameters (
home.component.ts:94):Watch Query Parameters
Subscribe to query parameter changes to fetch filtered data (
home.component.ts:53):Filter Initialization from Query Params
TheinitFromQueryParams method (filters.component.ts:34) synchronizes the UI with URL state:
The
{ emitEvent: false } option prevents triggering change events during initialization, avoiding unnecessary API calls.Filter Data Flow
Here’s how data flows through the filtering system:Filter Types
TheCityFilters interface (types/location.ts:37) defines available filters:
State Filter
- Type: String or null
- Purpose: Filter cities by state
- Default: null (shows all states)
- UI Control: Select dropdown
Page Size Filter
- Type: Number
- Purpose: Control number of items per page
- Default: 10
- Options: 10, 20
- UI Control: Select dropdown
Service Integration
TheLocationsService (locations.service.ts:21) converts filters to HTTP parameters:
Best Practices
Reset Pagination on Filter Change
Reset Pagination on Filter Change
Always reset to page 1 when filters change to avoid showing empty results:
Use Query Parameter Merging
Use Query Parameter Merging
Use
queryParamsHandling: 'merge' to preserve other parameters:Avoid Event Loops
Avoid Event Loops
Use
{ emitEvent: false } when programmatically setting form values:Handle Null Values
Handle Null Values
Always handle null/empty filter values gracefully:
Next Steps
Pagination
Learn how pagination integrates with filters
Query Parameters
Deep dive into URL state management