Skip to main content
cngx-src documentation

CngxFilter

DirectivePrimaryv0.1.0

projects/common/data/filter/filter.directive.ts

Import#

import { CngxFilter } from '@cngx/common/data'

Description#

Atom directive that holds one or more named filter predicates.

Supports both uncontrolled (internal state) and controlled modes. In controlled mode the cngxFilter input takes precedence over the internal 'default' predicate - pair with filterChange to keep it in sync.

Multi-filter - use addPredicate(key, fn) / removePredicate(key) to maintain a named stack of predicates. All active predicates are AND-combined across keys: an item must pass every registered predicate to appear in results.

AND vs OR - the directive enforces AND across dimensions (keys). OR logic within a single dimension stays inside the predicate function itself:

// AND across dimensions: location AND role must both match
filter.addPredicate('location', p => selectedLocations.has(p.location));
filter.addPredicate('role',     p => selectedRoles.has(p.role));

// OR within a dimension: London OR Rome - just put a Set inside one predicate
filter.addPredicate('location', p => selectedLocations.has(p.location));

setPredicate / clear operate on the 'default' key and remain fully backward-compatible with single-predicate usage.

Consumer connects this to a list via a computed() - nothing is injected automatically.

https://cngxjs.github.io/cngx/examples/#/common/data/filter-chips/custom-chip-decoration-via-cngxfilterchip https://cngxjs.github.io/cngx/examples/#/common/data/filter-chips/multi-role-filter-wired-to-a-list https://cngxjs.github.io/cngx/examples/#/common/data/smart-data-source/auto-wired https://cngxjs.github.io/cngx/examples/#/common/data/smart-data-source/how-it-works-hostdirectives-inject https://cngxjs.github.io/cngx/examples/#/common/data/smart-data-source/smartdatasource-cngxpaginate-hostdirective

Metadata#

Index#

Outputs#

predicatesChange#Map boolean>

Emitted whenever any named predicate is added or removed.

Instance Properties#

filterChange#unknown
Readonly

Emitted whenever the 'default' predicate changes (backward-compat single-filter output).

output<((value: T) => boolean) | null>()
predicateInput#unknown
Readonly

Controlled predicate. When bound, takes precedence over the 'default' internal predicate.

input<((value: T) => boolean) | null>(null, { alias: 'cngxFilter' })
predicates#unknown
Readonly

All active named predicates. Use addPredicate / removePredicate to manage this map.

this.predicatesState.asReadonly()

Methods#

addPredicate#void
addPredicate(key: string, fn: function)

Adds or replaces the named predicate key. Emits predicatesChange with the updated map.

@paramkeystring
@paramfnfunction
clear#void

Removes all named predicates and emits filterChange with null.

removePredicate#void
removePredicate(key: string)

Removes the named predicate key (no-op if not present). Emits predicatesChange with the updated map.

@paramkeystring
setPredicate#void
setPredicate(fn: unknown | null)

Sets the 'default' predicate and emits filterChange. Pass null to clear it (same as clear()).

@paramfnunknown | null