CngxSort
projects/common/data/sort/sort.directive.ts
Import#
import { CngxSort } from '@cngx/common/data'
Description#
Atom directive that tracks sort state (active field + direction).
Supports both uncontrolled (internal state) and controlled modes.
In controlled mode the cngxSortActive / cngxSortDirection inputs take
precedence over the internal state - pair with sortChange to keep them
in sync.
An uncontrolled sort may declare a starting column + direction via
[cngxSortInitial]: it seeds the internal state exactly once on init, then
user clicks take over. Distinct from the controlled cngxSortActive /
cngxSortDirection pins - the seed is skipped when a controlled pin is bound.
When multiSort is true, holding Shift while clicking a sort header
adds it as a secondary (tertiary, …) sort key instead of replacing the
primary one. Shift-clicking an active column cycles it asc → desc → removed.
Multi-sort state is always uncontrolled; use sortsChange to read it.
While a controlled pin is bound, additive clicks degrade to the
single-sort cycle.
Consumer connects this to a table or list via a computed() - nothing is
injected automatically.
Metadata#
Index#
Outputs
Inputs#
string | undefinedControlled active column. When bound, takes precedence over internal state (single-sort only).
undefined, { alias: 'cngxSortActive' }'asc' | 'desc' | undefinedControlled direction. When bound, takes precedence over internal state (single-sort only).
undefined, { alias: 'cngxSortDirection', }SortEntry | undefinedUncontrolled starting sort. Seeds the internal state once on init if no sort
is set yet and no controlled cngxSortActive pin is bound; user clicks own
it thereafter. Ignored in controlled mode.
undefined, { alias: 'cngxSortInitial' }Outputs#
SortEntry | undefinedEmitted when the primary sort state changes. Emits undefined on clear.
SortEntry[]Emitted whenever the sort stack changes (including removals and full clears).
Always reflects the full sorts array at the time of emission.
Methods#
setSort(field: string, additive: unknown)Sets or toggles the sort for field.
additive = false (default, plain click):
- Same field → cycle
asc→desc→ cleared (a third click removes the sort) - Different field → replace stack with
{ field, asc }
additive = true (Shift+click when multiSort is enabled on the header):
- Field not in stack → append as asc
- Field in stack as asc → change to desc
- Field in stack as desc → remove from stack
With a controlled cngxSortActive pin bound, the sort is event-only: the
cycle is computed off the effective entry and emitted, the internal state
stays untouched (nothing stale surfaces if the pin unbinds later). Since
multi-sort is always uncontrolled, an additive call degrades to the
non-additive controlled cycle while a pin is bound.
unknown= false