Skip to content

Commit

Permalink
feat(angular-table): Support Tanstack v9 alpha and Angular 19, fix ex…
Browse files Browse the repository at this point in the history
…amples (#5817)

* angular v19 table adapter

* angular v19 table adapter

* fix types
  • Loading branch information
riccardoperra authored Nov 24, 2024
1 parent bd735ba commit 525fbcb
Show file tree
Hide file tree
Showing 31 changed files with 616 additions and 195 deletions.
2 changes: 1 addition & 1 deletion examples/angular/basic/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<tbody>
@for (row of table.getRowModel().rows; track row.id) {
<tr>
@for (cell of row.getVisibleCells(); track cell.id) {
@for (cell of row.getAllCells(); track cell.id) {
<td>
<ng-container
*flexRender="
Expand Down
13 changes: 8 additions & 5 deletions examples/angular/basic/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core'
import { RouterOutlet } from '@angular/router'
import {
FlexRenderDirective,
createCoreRowModel,
injectTable,
tableFeatures,
} from '@tanstack/angular-table'
import type { ColumnDef } from '@tanstack/angular-table'

Expand Down Expand Up @@ -81,18 +80,22 @@ const defaultColumns: Array<ColumnDef<any, Person>> = [
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, FlexRenderDirective],
imports: [FlexRenderDirective],
templateUrl: './app.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
data = signal<Array<Person>>(defaultData)
readonly data = signal<Array<Person>>(defaultData)

readonly tableFeatures = tableFeatures({})

table = injectTable(() => ({
_features: this.tableFeatures, // new required option in V9. Tell the table which features you are importing and using (better tree-shaking)
_rowModels: {}, // `Core` row model is now included by default, but you can still override it here
data: this.data(),
columns: defaultColumns,
getCoreRowModel: createCoreRowModel(),
debugTable: true,
// other options here
}))

rerender() {
Expand Down
7 changes: 6 additions & 1 deletion examples/angular/column-ordering/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
} from '@angular/core'
import {
FlexRenderDirective,
columnOrderingFeature,
columnVisibilityFeature,
createCoreRowModel,
injectTable,
} from '@tanstack/angular-table'
Expand Down Expand Up @@ -84,12 +86,15 @@ export class AppComponent {

readonly table = injectTable(() => ({
data: this.data(),
_features: {
columnVisibilityFeature,
columnOrderingFeature,
},
columns: defaultColumns,
state: {
columnOrder: this.columnOrder(),
columnVisibility: this.columnVisibility(),
},
getCoreRowModel: createCoreRowModel(),
onColumnVisibilityChange: (updaterOrValue) => {
typeof updaterOrValue === 'function'
? this.columnVisibility.update(updaterOrValue)
Expand Down
22 changes: 18 additions & 4 deletions examples/angular/column-pinning-sticky/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Component, computed, signal } from '@angular/core'
import {
FlexRenderDirective,
columnOrderingFeature,
columnPinningFeature,
columnResizingFeature,
columnSizingFeature,
columnVisibilityFeature,
createCoreRowModel,
injectTable,
} from '@tanstack/angular-table'
Expand Down Expand Up @@ -74,25 +79,34 @@ const defaultColumns: Array<ColumnDef<any, Person>> = [
@Component({
selector: 'app-root',
standalone: true,
imports: [FlexRenderDirective, SlicePipe, NgTemplateOutlet, NgStyle],
imports: [FlexRenderDirective, NgStyle],
templateUrl: './app.component.html',
})
export class AppComponent {
readonly columns = signal([...defaultColumns])
readonly data = signal<Array<Person>>(makeData(30))
readonly columnVisibility = signal<ColumnVisibilityState>({})
readonly columnOrder = signal<ColumnOrderState>([])
readonly columnPinning = signal<ColumnPinningState>({})
readonly columnPinning = signal<ColumnPinningState>({
left: [],
right: [],
})
readonly split = signal(false)

table = injectTable(() => ({
data: this.data(),
_features: {
columnPinningFeature,
columnVisibilityFeature,
columnSizingFeature,
columnOrderingFeature,
columnResizingFeature,
},
columns: this.columns(),
getCoreRowModel: createCoreRowModel(),
debugTable: true,
debugHeaders: true,
debugColumns: true,
columnResizeMode: 'onChange',
columnResizeMode: 'onChange' as const,
}))

stringifiedColumnPinning = computed(() => {
Expand Down
15 changes: 13 additions & 2 deletions examples/angular/column-pinning/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import {
} from '@angular/core'
import {
FlexRenderDirective,
columnOrderingFeature,
columnPinningFeature,
columnVisibilityFeature,
createCoreRowModel,
injectTable,
rowPinningFeature,
} from '@tanstack/angular-table'
import { faker } from '@faker-js/faker'
import { NgTemplateOutlet, SlicePipe } from '@angular/common'
Expand Down Expand Up @@ -91,11 +95,19 @@ export class AppComponent {
readonly data = signal<Array<Person>>(makeData(5000))
readonly columnVisibility = signal<ColumnVisibilityState>({})
readonly columnOrder = signal<ColumnOrderState>([])
readonly columnPinning = signal<ColumnPinningState>({})
readonly columnPinning = signal<ColumnPinningState>({
left: [],
right: [],
})
readonly split = signal(false)

table = injectTable(() => ({
data: this.data(),
_features: {
columnPinningFeature,
columnOrderingFeature,
columnVisibilityFeature,
},
columns: defaultColumns,
state: {
columnVisibility: this.columnVisibility(),
Expand All @@ -117,7 +129,6 @@ export class AppComponent {
? this.columnPinning.update(updaterOrValue)
: this.columnPinning.set(updaterOrValue)
},
getCoreRowModel: createCoreRowModel(),
debugTable: true,
debugHeaders: true,
debugColumns: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/angular/grouping/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<tbody>
@for (row of table.getRowModel().rows; track row.id) {
<tr>
@for (cell of row.getVisibleCells(); track cell.id) {
@for (cell of row.getAllCells(); track cell.id) {
<td
[style.background]="
cell.getIsGrouped()
Expand Down
63 changes: 37 additions & 26 deletions examples/angular/grouping/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import {
} from '@angular/core'
import {
FlexRenderDirective,
columnFilteringFeature,
columnGroupingFeature,
createCoreRowModel,
createExpandedRowModel,
createFilteredRowModel,
createGroupedRowModel,
createPaginatedRowModel,
injectTable,
rowExpandingFeature,
rowPaginationFeature,
tableOptions,
} from '@tanstack/angular-table'
import { columns } from './columns'
Expand All @@ -28,35 +32,42 @@ import type { GroupingState, Updater } from '@tanstack/angular-table'
})
export class AppComponent {
title = 'grouping'
data = signal(makeData(10000))
grouping = signal<GroupingState>([])
readonly data = signal(makeData(10000))
readonly grouping = signal<GroupingState>([])

stringifiedGrouping = computed(() => JSON.stringify(this.grouping(), null, 2))

tableOptions = computed(() =>
tableOptions({
data: this.data(),
columns: columns,
state: {
grouping: this.grouping(),
},
onGroupingChange: (updaterOrValue: Updater<GroupingState>) => {
const groupingState =
typeof updaterOrValue === 'function'
? updaterOrValue([...this.grouping()])
: updaterOrValue
this.grouping.set(groupingState)
},
getExpandedRowModel: createExpandedRowModel(),
getGroupedRowModel: createGroupedRowModel(),
getCoreRowModel: createCoreRowModel(),
getPaginatedRowModel: createPaginatedRowModel(),
getFilteredRowModel: createFilteredRowModel(),
debugTable: true,
}),
readonly stringifiedGrouping = computed(() =>
JSON.stringify(this.grouping(), null, 2),
)

table = injectTable(this.tableOptions)
readonly table = injectTable(() => ({
data: this.data(),
columns: columns,
initialState: {
pagination: { pageSize: 20, pageIndex: 0 },
},
state: {
grouping: this.grouping(),
},
_features: {
columnGroupingFeature,
rowPaginationFeature,
columnFilteringFeature,
rowExpandingFeature,
},
_rowModels: {
groupedRowModel: createGroupedRowModel(),
expandedRowModel: createExpandedRowModel(),
paginatedRowModel: createPaginatedRowModel(),
filteredRowModel: createFilteredRowModel(),
},
onGroupingChange: (updaterOrValue: Updater<GroupingState>) => {
const groupingState =
typeof updaterOrValue === 'function'
? updaterOrValue([...this.grouping()])
: updaterOrValue
this.grouping.set(groupingState)
},
}))

onPageInputChange(event: any): void {
const page = event.target.value ? Number(event.target.value) - 1 : 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

@if (header.column.getCanFilter()) {
<div>
<!-- TODO fix type -->
<app-table-filter
[column]="header.column"
[column]="$any(header.column)"
[table]="table"
/>
</div>
Expand Down
18 changes: 14 additions & 4 deletions examples/angular/row-selection-signal/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
} from '@angular/core'
import {
FlexRenderDirective,
createCoreRowModel,
columnFilteringFeature,
columnVisibilityFeature,
createFilteredRowModel,
createPaginatedRowModel,
injectTable,
rowPaginationFeature,
rowSelectionFeature,
} from '@tanstack/angular-table'
import { FilterComponent } from './filter'
import { makeData } from './makeData'
Expand Down Expand Up @@ -97,6 +100,16 @@ export class AppComponent {

table = injectTable(() => ({
data: this.data(),
_features: {
rowSelectionFeature,
rowPaginationFeature,
columnFilteringFeature,
columnVisibilityFeature,
},
_rowModels: {
filteredRowModel: createFilteredRowModel(),
paginatedRowModel: createPaginatedRowModel(),
},
columns: this.columns,
state: {
rowSelection: this.rowSelection(),
Expand All @@ -110,9 +123,6 @@ export class AppComponent {
: updaterOrValue,
)
},
getCoreRowModel: createCoreRowModel(),
getFilteredRowModel: createFilteredRowModel(),
getPaginatedRowModel: createPaginatedRowModel(),
debugTable: true,
}))

Expand Down
6 changes: 3 additions & 3 deletions examples/angular/row-selection-signal/src/app/filter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommonModule } from '@angular/common'
import { Component, input } from '@angular/core'
import type { OnInit } from '@angular/core'
import type { Column, Table } from '@tanstack/angular-table'
import type { Column, RowData, Table } from '@tanstack/angular-table'

@Component({
selector: 'app-table-filter',
Expand Down Expand Up @@ -39,10 +39,10 @@ import type { Column, Table } from '@tanstack/angular-table'
standalone: true,
imports: [CommonModule],
})
export class FilterComponent<T> implements OnInit {
export class FilterComponent<T extends RowData> implements OnInit {
column = input.required<Column<any, any>>()

table = input.required<Table<T>>()
table = input.required<Table<any, T>>()

columnType!: string

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeDetectionStrategy, Component, input } from '@angular/core'
import type { Row, Table } from '@tanstack/angular-table'
import type { Row, RowData, Table } from '@tanstack/angular-table'

@Component({
template: `
Expand All @@ -16,7 +16,7 @@ import type { Row, Table } from '@tanstack/angular-table'
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableHeadSelectionComponent<T> {
export class TableHeadSelectionComponent<T extends RowData> {
// Your component should also reflect the fields you use as props in flexRenderer directive.
// Define the fields as input you want to use in your component
// ie. In this case, you are passing HeaderContext object as props in flexRenderer directive.
Expand All @@ -25,7 +25,7 @@ export class TableHeadSelectionComponent<T> {

// column = input.required<Column<T, unknown>>()
// header = input.required<Header<T, unknown>>()
table = input.required<Table<T>>()
table = input.required<Table<any, T>>()
}

@Component({
Expand All @@ -42,6 +42,6 @@ export class TableHeadSelectionComponent<T> {
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TableRowSelectionComponent<T> {
row = input.required<Row<T>>()
export class TableRowSelectionComponent<T extends RowData> {
row = input.required<Row<any, T>>()
}
5 changes: 3 additions & 2 deletions examples/angular/row-selection/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

@if (header.column.getCanFilter()) {
<div>
<!-- TODO: fix type -->
<app-table-filter
[column]="header.column"
[column]="$any(header.column)"
[table]="table"
/>
</div>
Expand All @@ -35,7 +36,7 @@
<tbody>
@for (row of table.getRowModel().rows; track row.id) {
<tr>
@for (cell of row.getVisibleCells(); track cell.id) {
@for (cell of row.getAllCells(); track cell.id) {
<td>
<ng-container
*flexRender="
Expand Down
Loading

0 comments on commit 525fbcb

Please sign in to comment.