|
| 1 | +# ARIA Grid with CDK Table Integration |
| 2 | + |
| 3 | +This module provides ARIA grid directives that work seamlessly with Angular CDK Table, solving the DI tree issue that occurs when using templates and portals. |
| 4 | + |
| 5 | +## The Problem |
| 6 | + |
| 7 | +When using ARIA grid directives with CDK table, the `ngGridCell` directive fails to find the `GRID_ROW` provider because CDK table renders cells through templates and portals, which breaks the normal Angular DI tree. |
| 8 | + |
| 9 | +## The Solution |
| 10 | + |
| 11 | +This implementation provides multiple approaches to solve the DI issue: |
| 12 | + |
| 13 | +### 1. Simple Row Provider (Recommended) |
| 14 | + |
| 15 | +Use the `cdkGridRowProvider` directive on your table rows: |
| 16 | + |
| 17 | +```typescript |
| 18 | +@Component({ |
| 19 | + imports: [CdkTableModule, Grid, GridRow, GridCell, CdkGridRowProvider], |
| 20 | + template: ` |
| 21 | + <table ngGrid cdk-table [dataSource]="data"> |
| 22 | + @for (column of columns; track column) { |
| 23 | + <ng-container [cdkColumnDef]="column"> |
| 24 | + <th ngGridCell cdk-header-cell *cdkHeaderCellDef>{{ column }}</th> |
| 25 | + <td ngGridCell cdk-cell *cdkCellDef="let row">{{ row[column] }}</td> |
| 26 | + </ng-container> |
| 27 | + } |
| 28 | + <tr ngGridRow cdkGridRowProvider cdk-header-row *cdkHeaderRowDef="columns"></tr> |
| 29 | + <tr ngGridRow cdkGridRowProvider cdk-row *cdkRowDef="let row; columns: columns"></tr> |
| 30 | + </table> |
| 31 | + ` |
| 32 | +}) |
| 33 | +export class MyTable { |
| 34 | + data = [ |
| 35 | + { name: 'John', age: 30 }, |
| 36 | + { name: 'Jane', age: 25 } |
| 37 | + ]; |
| 38 | + columns = ['name', 'age']; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### 2. DOM-based Fallback |
| 43 | + |
| 44 | +The `GridCell` directive automatically falls back to DOM traversal when DI fails, so it works without additional configuration in most cases. |
| 45 | + |
| 46 | +## Key Features |
| 47 | + |
| 48 | +- **Automatic DI Fallback**: GridCell automatically searches the DOM hierarchy when DI fails |
| 49 | +- **CDK Table Integration**: Seamless integration with existing CDK table implementations |
| 50 | +- **Minimal Code Changes**: Only requires adding `cdkGridRowProvider` to row elements |
| 51 | +- **Performance Optimized**: Uses efficient DOM traversal and caching |
| 52 | +- **Type Safe**: Full TypeScript support with proper typing |
| 53 | + |
| 54 | +## API Reference |
| 55 | + |
| 56 | +### Directives |
| 57 | + |
| 58 | +- `Grid` - Main grid container (`[ngGrid]`) |
| 59 | +- `GridRow` - Grid row (`[ngGridRow]`) |
| 60 | +- `GridCell` - Grid cell (`[ngGridCell]`) |
| 61 | +- `CdkGridRowProvider` - CDK table row provider (`[cdkGridRowProvider]`) |
| 62 | + |
| 63 | +### Tokens |
| 64 | + |
| 65 | +- `GRID_ROW` - Injection token for grid row instances |
| 66 | + |
| 67 | +## Migration Guide |
| 68 | + |
| 69 | +To migrate existing CDK tables to use ARIA grid: |
| 70 | + |
| 71 | +1. Add `ngGrid` to your table element |
| 72 | +2. Add `ngGridRow` to your row templates |
| 73 | +3. Add `ngGridCell` to your cell templates |
| 74 | +4. Add `cdkGridRowProvider` to CDK row templates |
| 75 | +5. Import the required directives in your component |
| 76 | + |
| 77 | +The solution is backward compatible and doesn't affect existing CDK table functionality. |
0 commit comments