AdvancedFilterInCrud
Overview
This guide outlines the steps to implement advanced filtering functionality for a feature using the framework.
Step 1: Define Backend DTO
Create a new domain DTO object:
- Advanced Filter DTO (
MyFeatureAdvancedFilterDto)- Contains filter criteria properties
- Use nullable types for optional filter fields
Step 2: Update Backend Service Layer
-
Update the service interface (
IMyFeatureSpecificAppService)- Change generic parameter from
PagingFilterFormatDtotoPagingFilterFormatDto<MyFeatureAdvancedFilterDto>
- Change generic parameter from
-
Implement filter specification in the service (
MyFeatureAppService)- Override
SetGetRangeFilterSpecifications()to apply advanced filters - Create
GetMyFeatureAdvancedFilterSpecification()method using the Specification pattern - Build LINQ expressions to filter entities based on filter criteria
- Override
-
Update controller endpoints
- Change parameter type in
GetAll()andGetFile()endpoints to use the generic filter type
- Change parameter type in
Step 3: Create Frontend Filter DTO
Create the TypeScript DTO (MyFeatureAdvancedFilterDto) that mirrors the backend structure:
- Implement a static
hasFilter()method in the Dto class to check if any filters are active
Step 4: Build the Advanced Filter Component
Create a reusable filter component (MyFeatureAdvancedFilterComponent) with:
-
Component class
- Create reactive form with form controls for each filter field
- Implement
onFilter()to emit filter object when submitted - Implement
ngOnChanges()to update form when external filters change - Use
ViewContainerRef.createEmbeddedView()to render template outside component tag
-
Template
- Define all the filters needed as form controls
- Provide Reset and Filter action buttons with loading state
Step 5: Integrate Filter into Index Component
-
Update component imports and declarations
- Import the filter component
- Override
checkhasAdvancedFilter()method to update filter state
-
Update template layout
- Reorganize main container to use flexbox row layout
- Conditionally display filter panel using
@ifcontrol flow - Add filter button to table controller
- Wire up filter events:
(filter)="onFilter($event)"and(closeFilter)="onCloseFilter()"
Extension Pattern
To add more filter criteria:
- Add new property to
MyFeatureAdvancedFilterDto(backend and frontend) - Add specification logic in
GetMyFeatureAdvancedFilterSpecification()method - Add form control and options list to component class (frontend)
- Add filter control to template
Working example
A working example can be found in BIA Demo under PlaneSpecific feature