-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathapp.ts
More file actions
143 lines (133 loc) · 3.76 KB
/
Copy pathapp.ts
File metadata and controls
143 lines (133 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { Component, computed, signal } from '@angular/core'
import {
FlexRender,
columnOrderingFeature,
columnPinningFeature,
columnResizingFeature,
columnSizingFeature,
columnVisibilityFeature,
createColumnHelper,
injectTable,
tableFeatures,
} from '@tanstack/angular-table'
import { injectTanStackTableDevtools } from '@tanstack/angular-table-devtools'
import { faker } from '@faker-js/faker'
import { makeData } from './makeData'
import type { Person } from './makeData'
import type {
Column,
ColumnOrderState,
ColumnPinningState,
ColumnVisibilityState,
} from '@tanstack/angular-table'
const _features = tableFeatures({
columnOrderingFeature,
columnPinningFeature,
columnResizingFeature,
columnSizingFeature,
columnVisibilityFeature,
})
const columnHelper = createColumnHelper<typeof _features, Person>()
const defaultColumns = columnHelper.columns([
columnHelper.accessor('firstName', {
id: 'firstName',
header: 'First Name',
cell: (info) => info.getValue(),
footer: (props) => props.column.id,
size: 180,
}),
columnHelper.accessor('lastName', {
id: 'lastName',
cell: (info) => info.getValue(),
header: () => 'Last Name',
footer: (props) => props.column.id,
size: 180,
}),
columnHelper.accessor('age', {
id: 'age',
header: 'Age',
footer: (props) => props.column.id,
size: 180,
}),
columnHelper.accessor('visits', {
id: 'visits',
header: 'Visits',
footer: (props) => props.column.id,
size: 180,
}),
columnHelper.accessor('status', {
id: 'status',
header: 'Status',
footer: (props) => props.column.id,
size: 180,
}),
columnHelper.accessor('progress', {
id: 'progress',
header: 'Profile Progress',
footer: (props) => props.column.id,
size: 180,
}),
])
@Component({
selector: 'app-root',
imports: [FlexRender],
templateUrl: './app.html',
})
export class App {
readonly columns = signal([...defaultColumns])
readonly data = signal<Array<Person>>(makeData(20))
readonly columnVisibility = signal<ColumnVisibilityState>({})
readonly columnOrder = signal<ColumnOrderState>([])
readonly columnPinning = signal<ColumnPinningState>({
left: [],
right: [],
})
readonly split = signal(false)
table = injectTable(() => ({
_features,
columns: this.columns(),
data: this.data(),
debugTable: true,
debugHeaders: true,
debugColumns: true,
columnResizeMode: 'onChange' as const,
}))
stringifiedColumnPinning = computed(() => {
return JSON.stringify(this.table.store.state.columnPinning)
})
readonly getCommonPinningStyles = (
column: Column<any, Person>,
): Record<string, any> => {
const isPinned = column.getIsPinned()
const isLastLeftPinnedColumn =
isPinned === 'left' && column.getIsLastColumn('left')
const isFirstRightPinnedColumn =
isPinned === 'right' && column.getIsFirstColumn('right')
return {
boxShadow: isLastLeftPinnedColumn
? '-4px 0 4px -4px gray inset'
: isFirstRightPinnedColumn
? '4px 0 4px -4px gray inset'
: undefined,
left: isPinned === 'left' ? `${column.getStart('left')}px` : undefined,
right: isPinned === 'right' ? `${column.getAfter('right')}px` : undefined,
opacity: isPinned ? 0.95 : 1,
position: isPinned ? 'sticky' : 'relative',
width: `${column.getSize()}px`,
zIndex: isPinned ? 1 : 0,
}
}
randomizeColumns() {
this.table.setColumnOrder(
faker.helpers.shuffle(this.table.getAllLeafColumns().map((d) => d.id)),
)
}
refreshData = () => this.data.set(makeData(20))
stressTest = () => this.data.set(makeData(1_000))
constructor() {
injectTanStackTableDevtools(() => ({
table: this.table,
name: 'column-pinning-sticky',
}))
}
}