@angular/cdk/collections package provides data structures and utilities for working with collections, including DataSource for tables and lists.
Installation
DataSource
Abstract class for providing data to CDK components like tables and trees.Basic DataSource
Async DataSource
Paginated DataSource
ArrayDataSource
ConvenienceDataSource implementation for arrays:
SelectionModel
Manage item selection state:Single Selection
Selection Events
UniqueSelectionDispatcher
Coordinate selection across multiple selection models:API Reference
DataSource
SelectionModel
| Property | Type | Description |
|---|---|---|
selected | T[] | Currently selected values |
changed | Observable | Selection change events |
| Method | Returns | Description |
|---|---|---|
select(...values) | void | Select values |
deselect(...values) | void | Deselect values |
toggle(value) | void | Toggle selection |
clear() | void | Clear all selections |
isSelected(value) | boolean | Check if selected |
isEmpty() | boolean | Check if empty |
hasValue() | boolean | Check if has value |
sort(predicate?) | void | Sort selected values |
Practical Examples
Filterable DataSource
Master-Detail Selection
Best Practices
- Clean up - Always implement
disconnect()to complete observables - Efficient updates - Use
BehaviorSubjectfor data updates - Type safety - Use generics with
DataSource<T> - Selection comparison - Provide
compareWithfor object comparison - Memory leaks - Unsubscribe from
changedevents