import { HttpClient, HttpParams } from '@angular/common/http' import { ChangeDetectionStrategy, Component, inject, linkedSignal, signal, } from '@angular/core' import { ReactiveFormsModule } from '@angular/forms' import { FlexRender, columnFilteringFeature, createColumnHelper, globalFilteringFeature, injectTable, rowPaginationFeature, rowSortingFeature, tableFeatures, } from '@tanstack/angular-table' import { map } from 'rxjs' import { rxResource } from '@angular/core/rxjs-interop' import type { ColumnDef, PaginationState, SortingState, } from '@tanstack/angular-table' import type { WritableSignal } from '@angular/core' export type Todo = { userId: number id: number title: string completed: boolean } const features = tableFeatures({ rowPaginationFeature, columnFilteringFeature, globalFilteringFeature, rowSortingFeature, }) const columnHelper = createColumnHelper() type TodoResponse = { items: Array; totalCount: number } @Component({ selector: 'app-root', imports: [FlexRender, ReactiveFormsModule], templateUrl: './app.html', changeDetection: ChangeDetectionStrategy.OnPush, }) export class App { readonly client = inject(HttpClient) readonly pagination = signal({ pageSize: 10, pageIndex: 0, }) readonly sorting = signal([{ id: 'id', desc: false }]) readonly globalFilter = signal(null) readonly data = rxResource({ params: () => ({ page: this.pagination(), globalFilter: this.globalFilter(), sorting: this.sorting(), }), stream: ({ params: { page, globalFilter, sorting } }) => { let httpParams = new HttpParams({ fromObject: { _page: page.pageIndex + 1, _limit: page.pageSize, }, }) if (globalFilter) { httpParams = httpParams.set('title_like', globalFilter) } if (sorting.length) { const keys: Array = [] const orders: Array = [] for (const sort of sorting) { keys.push(sort.id) orders.push(sort.desc ? 'desc' : 'asc') } httpParams = httpParams .set('_sort', keys.join(',')) .set('_order', orders.join(',')) } return this.client .get>(`https://jsonplaceholder.typicode.com/todos`, { params: httpParams, observe: 'response', }) .pipe( map((response) => { return { items: response.body ?? [], totalCount: Number(response.headers.get('X-Total-Count')), } satisfies TodoResponse }), ) }, }) readonly columns = [ columnHelper.accessor('id', { id: 'id', cell: (info) => info.getValue(), header: () => 'Id', footer: (props) => props.column.id, }), columnHelper.accessor('title', { id: 'title', cell: (info) => info.getValue(), header: () => 'Title', footer: (props) => props.column.id, }), columnHelper.accessor('completed', { id: 'completed', cell: (info) => (info.getValue() ? `✅` : `❌`), header: () => 'Completed', footer: (props) => props.column.id, }), ] as Array> // Keep previous value readonly dataWithLatest: WritableSignal = linkedSignal({ source: () => ({ value: this.data.value(), status: this.data.status(), }), computation: (source, previous) => { if (previous && source.status === 'loading') return previous.value return source.value ?? { items: [], totalCount: 0 } }, }) readonly table = injectTable(() => { const data = this.dataWithLatest() return { debugTable: true, features, data: data.items, columns: this.columns, state: { pagination: this.pagination(), globalFilter: this.globalFilter(), sorting: this.sorting(), }, manualFiltering: true, manualPagination: true, manualSorting: true, rowCount: data.totalCount, onPaginationChange: (updater) => typeof updater === 'function' ? this.pagination.update(updater) : this.pagination.set(updater), onSortingChange: (updater) => { typeof updater === 'function' ? this.sorting.update(updater) : this.sorting.set(updater) }, onGlobalFilterChange: (updater) => { typeof updater === 'function' ? this.globalFilter.update(updater) : this.globalFilter.set(updater) this.pagination.update((page) => ({ ...page, pageIndex: 0, })) }, } }) onPageInputChange(event: Event): void { const inputElement = event.target as HTMLInputElement const page = inputElement.value ? Number(inputElement.value) - 1 : 0 this.table.setPageIndex(page) } onPageSizeChange(event: any): void { this.table.setPageSize(Number(event.target.value)) } }