@nanahoshi/mona-ui 0.26.2 → 0.26.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,569 @@
1
+ # Server Binding & Virtualization
2
+
3
+ > Load pages from a server, or render only the visible rows of a large data set.
4
+
5
+ Entry point: `@nanahoshi/mona-ui/grid`
6
+
7
+ **Selectors:** `mona-grid[monaGridServerBinding]`, `mona-grid[monaGridVirtualScroll]`
8
+
9
+ Two ways to show more rows than a browser should hold. `monaGridServerBinding` hands paging, sorting and filtering to your server and shows the page it returns. `monaGridVirtualScroll` keeps every row in memory but renders only the ones in view, and can ask for more as people scroll.
10
+
11
+ ## Overview
12
+
13
+ | | Server binding | Virtual scrolling |
14
+ | :--- | :--- | :--- |
15
+ | Rows in memory | One page | All loaded rows |
16
+ | Who sorts, filters and pages | Your server | The grid |
17
+ | Navigation | The pager | Scrolling, with no pager |
18
+ | Loading more | `dataStateChange` asks for a page | `scrollEnd` near the bottom, if you want it |
19
+ | Row height | Fits the content | One fixed height |
20
+
21
+ The two can't be combined: enabling both throws an error. Server binding loads pages, not a remote virtual list.
22
+
23
+ **Use something else when:**
24
+
25
+ - The data set is a few thousand rows or fewer and fits in memory. The grid pages it fine without either directive.
26
+
27
+ ## Import & Quick Start
28
+
29
+ ```typescript
30
+ import { GridComponent, GridServerBindingDirective, type GridDataState } from "@nanahoshi/mona-ui/grid";
31
+ ```
32
+
33
+ ```html
34
+ <mona-grid
35
+ aria-label="Customers"
36
+ monaGridServerBinding
37
+ [data]="page().data"
38
+ [total]="page().total"
39
+ [skip]="state().skip"
40
+ [pageSize]="state().take"
41
+ [loading]="loading()"
42
+ (dataStateChange)="load($event)">
43
+ ...
44
+ </mona-grid>
45
+ ```
46
+
47
+ ## Examples
48
+
49
+ ### Server Paging, Sorting, and Filtering
50
+
51
+ Apply `monaGridServerBinding` when `data` contains only the page returned by your server. Bind the server's matching record count to `total`, the page's zero-based offset to `skip`, and its size to the grid's `pageSize`.
52
+
53
+ `server-example.component.ts`
54
+
55
+ ```typescript
56
+ import { Component, DestroyRef, inject, type OnInit, signal } from "@angular/core";
57
+ import {
58
+ GridColumnComponent,
59
+ GridComponent,
60
+ type GridDataState,
61
+ GridFilterableDirective,
62
+ GridServerBindingDirective,
63
+ GridSortableDirective
64
+ } from "@nanahoshi/mona-ui/grid";
65
+ import { type CompositeFilterDescriptor, Query, type SortDescriptor } from "@nanahoshi/mona-ui/query";
66
+
67
+ interface Customer {
68
+ id: number;
69
+ name: string;
70
+ orders: number;
71
+ }
72
+
73
+ interface CustomerPage {
74
+ data: Customer[];
75
+ total: number;
76
+ }
77
+
78
+ const CUSTOMERS: Customer[] = Array.from({ length: 87 }, (_, index) => ({
79
+ id: index + 1,
80
+ name: `Customer ${String(index + 1).padStart(2, "0")}`,
81
+ orders: (index * 37) % 50
82
+ }));
83
+
84
+ /** Stands in for a server: it filters and sorts every record, then returns one page after a short wait. */
85
+ async function fetchCustomers(state: GridDataState): Promise<CustomerPage> {
86
+ await new Promise<void>(resolve => setTimeout(resolve, 400));
87
+ let query = Query.from(CUSTOMERS);
88
+ for (const filter of state.filter) {
89
+ query = query.filter(filter);
90
+ }
91
+ const rows = query.sort(state.sort).run();
92
+ return { data: rows.slice(state.skip, state.skip + state.take), total: rows.length };
93
+ }
94
+
95
+ @Component({
96
+ selector: "app-grid-server-server-example",
97
+ imports: [
98
+ GridColumnComponent,
99
+ GridComponent,
100
+ GridFilterableDirective,
101
+ GridServerBindingDirective,
102
+ GridSortableDirective
103
+ ],
104
+ templateUrl: "./server-example.component.html"
105
+ })
106
+ export class GridServerExampleComponent implements OnInit {
107
+ readonly #destroyRef = inject(DestroyRef);
108
+ #requestId = 0;
109
+ protected readonly filter = signal<CompositeFilterDescriptor[]>([]);
110
+ protected readonly loading = signal(false);
111
+ protected readonly result = signal<CustomerPage>({ data: [], total: 0 });
112
+ protected readonly sort = signal<SortDescriptor[]>([]);
113
+ protected readonly state = signal<GridDataState>({ skip: 0, take: 10, sort: [], filter: [] });
114
+
115
+ public ngOnInit(): void {
116
+ void this.load(this.state());
117
+ }
118
+
119
+ /** Loads the requested page, and ignores a response when a newer request has started. */
120
+ protected async load(state: GridDataState): Promise<void> {
121
+ const requestId = ++this.#requestId;
122
+ this.state.set(state);
123
+ this.loading.set(true);
124
+ try {
125
+ const result = await fetchCustomers(state);
126
+ if (!this.#destroyRef.destroyed && requestId === this.#requestId) {
127
+ this.result.set(result);
128
+ }
129
+ } finally {
130
+ if (!this.#destroyRef.destroyed && requestId === this.#requestId) {
131
+ this.loading.set(false);
132
+ }
133
+ }
134
+ }
135
+ }
136
+ ```
137
+
138
+ `server-example.component.html`
139
+
140
+ ```html
141
+ <mona-grid
142
+ aria-label="Customers from the server"
143
+ class="h-96"
144
+ monaGridServerBinding
145
+ monaGridSortable
146
+ [data]="result().data"
147
+ [loading]="loading()"
148
+ [monaGridFilterable]="{ enabled: true, type: 'row' }"
149
+ [pageSize]="state().take"
150
+ [skip]="state().skip"
151
+ [total]="result().total"
152
+ [(filter)]="filter"
153
+ [(sort)]="sort"
154
+ (dataStateChange)="load($event)">
155
+ <mona-grid-column field="id" title="ID" type="number" [width]="90" />
156
+ <mona-grid-column field="name" title="Customer" />
157
+ <mona-grid-column field="orders" title="Orders" type="number" />
158
+ </mona-grid>
159
+ ```
160
+
161
+ `dataStateChange` supplies `{ skip, take, sort, filter }` once for a user page, page-size, sort, or filter action. All its properties are readonly, and each emission holds its own copies of the descriptors, including nested filter values. Use the grid's `pageSize` for the request's `take`; in server mode it must be a positive safe integer. Sort and filter actions start at `skip: 0`; a page-size change keeps the first row of the page in view. Sort order follows multi-column sort priority. Bindings from the parent synchronize silently, and creating the directive doesn't emit an initial request: load the first page yourself.
162
+
163
+ The application owns HTTP requests, errors, and stale-response handling. The example ignores a response that arrives after a newer request started. `SortDescriptor` and `CompositeFilterDescriptor` come from `@nanahoshi/mona-ui/query`; the grid has no endpoint or transport dependency.
164
+
165
+ ### Virtual Scrolling
166
+
167
+ `monaGridVirtualScroll` renders only the rows in view. `scrollEndThreshold` (rows from the bottom) controls when `scrollEnd` fires, so you can load more rows as people scroll.
168
+
169
+ `virtual-example.component.ts`
170
+
171
+ ```typescript
172
+ import { Component, signal } from "@angular/core";
173
+ import { GridColumnComponent, GridComponent, GridVirtualScrollDirective } from "@nanahoshi/mona-ui/grid";
174
+
175
+ interface Reading {
176
+ id: number;
177
+ sensor: string;
178
+ temperature: number;
179
+ }
180
+
181
+ function readings(from: number, count: number): Reading[] {
182
+ return Array.from({ length: count }, (_, offset) => {
183
+ const id = from + offset;
184
+ return { id, sensor: `Sensor ${(id % 12) + 1}`, temperature: 18 + ((id * 7) % 90) / 10 };
185
+ });
186
+ }
187
+
188
+ @Component({
189
+ selector: "app-grid-server-virtual-example",
190
+ imports: [GridColumnComponent, GridComponent, GridVirtualScrollDirective],
191
+ templateUrl: "./virtual-example.component.html"
192
+ })
193
+ export class GridVirtualExampleComponent {
194
+ protected readonly rows = signal(readings(1, 500));
195
+
196
+ /** Adds the next 500 readings when the last rows come into view, up to 5,000. */
197
+ protected onScrollEnd(): void {
198
+ const current = this.rows();
199
+ if (current.length < 5000) {
200
+ this.rows.set([...current, ...readings(current.length + 1, 500)]);
201
+ }
202
+ }
203
+ }
204
+ ```
205
+
206
+ `virtual-example.component.html`
207
+
208
+ ```html
209
+ <div class="flex flex-col gap-2">
210
+ <mona-grid
211
+ aria-label="Sensor readings"
212
+ class="h-96"
213
+ rowKey="id"
214
+ [data]="rows()"
215
+ [monaGridVirtualScroll]="{ height: 36 }"
216
+ [scrollEndThreshold]="20"
217
+ (scrollEnd)="onScrollEnd()">
218
+ <mona-grid-column field="id" title="Reading" type="number" [width]="110" />
219
+ <mona-grid-column field="sensor" title="Sensor" />
220
+ <mona-grid-column field="temperature" title="Temperature (°C)" type="number" />
221
+ </mona-grid>
222
+ <span aria-live="polite" class="text-sm">{{ rows().length }} readings loaded</span>
223
+ </div>
224
+ ```
225
+
226
+ ## Behavior
227
+
228
+ ### What the server must do
229
+
230
+ Apply the descriptors to the whole matching data set before paging. The grid keeps the returned row order and shows every supplied row, even when `skip` is nonzero: ten rows at `skip: 40`, `pageSize: 10` and `total: 137` show page five with all ten rows.
231
+
232
+ ### Loading
233
+
234
+ While `loading` is true, the body shows a localized loading indicator, keeps the existing rows, and doesn't show the empty state. The grid exposes `aria-busy`; the header and pager stay available for another request. Set `loading` back to false on both success and error.
235
+
236
+ ### Features with server binding
237
+
238
+ - Grouping and aggregates work on the loaded page. Each group direction orders the loaded page's group headers while keeping server row order within each group. `group` isn't part of `GridDataState`, and there is no remote aggregate protocol.
239
+ - CSV export contains loaded data only. Fetch a full export through your application when needed.
240
+ - Select-all selects loaded rows only. Use `selectBy` and controlled `selectedKeys` to keep stable keys across pages; this doesn't select unloaded records.
241
+ - Editing events describe loaded rows; the application saves changes to its server.
242
+ - Row reordering is disabled, with an accessible, localized reason.
243
+ - State persistence restores sort, filter, and page size (unless `persistPageSize` is `false`), then emits one refresh at `skip: 0`. The offset isn't persisted. When you provide a saved initial state, use that refresh to load the restored query instead of starting a competing initial request.
244
+
245
+ ### Row height in virtual scrolling
246
+
247
+ `height` in the options defaults to **36px** and must be finite and greater than zero. It sets the rendered height and the virtual item size of every row, group header and group footer, and templates taller than it are clipped. Variable-height virtualization isn't provided. Ordinary rows, by contrast, fit their content, see [Rows](grid-rows.md#rows-that-fit-their-content).
248
+
249
+ ### Features with virtual scrolling
250
+
251
+ While virtual scrolling is on, the grid shows no pager and no aggregate footer, and ignores detail rows. Selection's `selectAllScope: "page"` covers the whole view, since there are no pages. `Ctrl+End` scrolls to the last row before moving focus there.
252
+
253
+ ## Accessibility
254
+
255
+ Rows outside the view aren't in the page, so `aria-rowcount` and each row's `aria-rowindex` tell assistive technology where a row sits in the whole set. Focus returns to a cell of a virtual grid when its row scrolls back into view.
256
+
257
+ ### Keyboard
258
+
259
+ Virtual scrolling and server binding add no keys. `PageUp`, `PageDown`, `Ctrl+Home` and `Ctrl+End` move through every row of a virtual grid; with server binding, the pager changes pages. The grid's keys are on [Grid Overview](grid.md#keyboard).
260
+
261
+ ### ARIA
262
+
263
+ | Attribute | When present | Value |
264
+ | --- | --- | --- |
265
+ | `aria-busy` | On the tabular content region in server mode | `"true"` while `loading` is true |
266
+ | `aria-rowcount` | On the tabular content region | Every row, including rows not rendered |
267
+ | `aria-rowindex` | On every row | 1-based position in the whole set, counting earlier pages |
268
+
269
+ ## API
270
+
271
+ Everything below is imported from `@nanahoshi/mona-ui/grid`.
272
+
273
+ ### `GridServerBindingDirective`
274
+
275
+ **Selector:** `mona-grid[monaGridServerBinding]`
276
+
277
+ #### Inputs
278
+
279
+ | Name | Type | Default | Description |
280
+ | --- | --- | --- | --- |
281
+ | `loading` | `boolean` | `false` | Shows a loading overlay while the application requests server data. |
282
+ | `skip` | `number` | `0` | Zero-based offset of the supplied server page. External changes do not request data. |
283
+ | `total` (required) | `number` | — | Total matching server records before paging. Must be a non-negative integer. |
284
+
285
+ #### Outputs
286
+
287
+ | Name | Type | Description |
288
+ | --- | --- | --- |
289
+ | `dataStateChange` | `GridDataState` | Emitted once per user paging, sorting, or filtering action with the requested server state. |
290
+
291
+ ### `GridVirtualScrollDirective`
292
+
293
+ **Selector:** `mona-grid[monaGridVirtualScroll]`
294
+
295
+ #### Inputs
296
+
297
+ | Name | Type | Default | Description |
298
+ | --- | --- | --- | --- |
299
+ | `monaGridVirtualScroll` | `Partial<VirtualScrollOptions> \| "" \| undefined` | `undefined` | Enables row virtualization on the grid. Pass a `VirtualScrollOptions` object to configure the fixed row height. |
300
+ | `scrollEndThreshold` | `number` | `5` | Distance, in rows, from the bottom of the virtualized list at which `scrollEnd` is emitted. |
301
+
302
+ #### Outputs
303
+
304
+ | Name | Type | Description |
305
+ | --- | --- | --- |
306
+ | `scrollEnd` | `void` | Emitted when the scroll position reaches the configured threshold from the bottom of the virtualized list. |
307
+
308
+ ### Types
309
+
310
+ #### `GridDataState`
311
+
312
+ The paging, sorting, and filtering requested by a server-bound Grid.
313
+
314
+ | Name | Type | Description |
315
+ | --- | --- | --- |
316
+ | `filter` (read-only) | `readonly CompositeFilterDescriptor[]` | Applied composite filter descriptors. |
317
+ | `skip` (read-only) | `number` | Requested zero-based offset. |
318
+ | `sort` (read-only) | `readonly SortDescriptor[]` | Sort descriptors in priority order. |
319
+ | `take` (read-only) | `number` | Requested page size. |
320
+
321
+ #### `ArrayFilterDescriptor`
322
+
323
+ From `@nanahoshi/mona-ui/query`.
324
+
325
+ ```typescript
326
+ type ArrayFilterDescriptor = FieldDefinition & { operator: ArrayFilterOperators; value: readonly unknown[] };
327
+ ```
328
+
329
+ #### `ArrayFilterOperators`
330
+
331
+ From `@nanahoshi/mona-ui/query`.
332
+
333
+ ```typescript
334
+ type ArrayFilterOperators = "in" | "notin";
335
+ ```
336
+
337
+ #### `BooleanFilterDescriptor`
338
+
339
+ From `@nanahoshi/mona-ui/query`.
340
+
341
+ ```typescript
342
+ type BooleanFilterDescriptor = FieldDefinition & ({ operator: BooleanFilterOperators } | NullFilterDescriptor);
343
+ ```
344
+
345
+ #### `BooleanFilterOperators`
346
+
347
+ From `@nanahoshi/mona-ui/query`.
348
+
349
+ ```typescript
350
+ type BooleanFilterOperators = "istrue" | "isfalse" | NullFilterOperators;
351
+ ```
352
+
353
+ #### `CommonFilterDescriptor`
354
+
355
+ From `@nanahoshi/mona-ui/query`.
356
+
357
+ ```typescript
358
+ type CommonFilterDescriptor = FieldDefinition & { operator: CommonFilterOperators; value: unknown };
359
+ ```
360
+
361
+ #### `CommonFilterOperators`
362
+
363
+ From `@nanahoshi/mona-ui/query`.
364
+
365
+ ```typescript
366
+ type CommonFilterOperators = "eq" | "neq";
367
+ ```
368
+
369
+ #### `CompositeFilterDescriptor`
370
+
371
+ From `@nanahoshi/mona-ui/query`.
372
+
373
+ An empty `filters` list matches every item, whatever the logic.
374
+
375
+ | Name | Type |
376
+ | --- | --- |
377
+ | `filters` | `Array<FilterDescriptor \| CompositeFilterDescriptor>` |
378
+ | `logic` | `FilterLogic` |
379
+
380
+ #### `DateFilterDescriptor`
381
+
382
+ From `@nanahoshi/mona-ui/query`.
383
+
384
+ ```typescript
385
+ type DateFilterDescriptor = FieldDefinition & ( | { operator: DateFilterOperators; value: Date; } | NullFilterDescriptor );
386
+ ```
387
+
388
+ #### `DateFilterOperators`
389
+
390
+ From `@nanahoshi/mona-ui/query`.
391
+
392
+ ```typescript
393
+ type DateFilterOperators = NumericFilterOperators | NullFilterOperators;
394
+ ```
395
+
396
+ #### `EmptyFilterDescriptor`
397
+
398
+ From `@nanahoshi/mona-ui/query`.
399
+
400
+ ```typescript
401
+ type EmptyFilterDescriptor = FieldDefinition & { operator: EmptyFilterOperators };
402
+ ```
403
+
404
+ #### `EmptyFilterOperators`
405
+
406
+ From `@nanahoshi/mona-ui/query`.
407
+
408
+ ```typescript
409
+ type EmptyFilterOperators = "isempty" | "isnotempty";
410
+ ```
411
+
412
+ #### `FieldDefinition`
413
+
414
+ From `@nanahoshi/mona-ui/query`.
415
+
416
+ ```typescript
417
+ type FieldDefinition = { field: string };
418
+ ```
419
+
420
+ #### `FilterDescriptor`
421
+
422
+ From `@nanahoshi/mona-ui/query`.
423
+
424
+ ```typescript
425
+ type FilterDescriptor = | CommonFilterDescriptor | BooleanFilterDescriptor | NumericFilterDescriptor | StringFilterDescriptor | DateFilterDescriptor | NullFilterDescriptor | EmptyFilterDescriptor | NullOrEmptyFilterDescriptor | ArrayFilterDescriptor | RangeFilterDescriptor | FunctionFilterDescriptor;
426
+ ```
427
+
428
+ #### `FilterLogic`
429
+
430
+ From `@nanahoshi/mona-ui/query`.
431
+
432
+ ```typescript
433
+ type FilterLogic = "and" | "or";
434
+ ```
435
+
436
+ #### `FunctionFilterDescriptor`
437
+
438
+ From `@nanahoshi/mona-ui/query`.
439
+
440
+ ```typescript
441
+ type FunctionFilterDescriptor = FieldDefinition & { operator: FunctionFilterOperators; predicate(value: unknown): boolean; };
442
+ ```
443
+
444
+ #### `FunctionFilterOperators`
445
+
446
+ From `@nanahoshi/mona-ui/query`.
447
+
448
+ ```typescript
449
+ type FunctionFilterOperators = "function";
450
+ ```
451
+
452
+ #### `NullFilterDescriptor`
453
+
454
+ From `@nanahoshi/mona-ui/query`.
455
+
456
+ ```typescript
457
+ type NullFilterDescriptor = FieldDefinition & { operator: NullFilterOperators };
458
+ ```
459
+
460
+ #### `NullFilterOperators`
461
+
462
+ From `@nanahoshi/mona-ui/query`.
463
+
464
+ ```typescript
465
+ type NullFilterOperators = "isnull" | "isnotnull";
466
+ ```
467
+
468
+ #### `NullOrEmptyFilterDescriptor`
469
+
470
+ From `@nanahoshi/mona-ui/query`.
471
+
472
+ ```typescript
473
+ type NullOrEmptyFilterDescriptor = FieldDefinition & { operator: NullOrEmptyFilterOperators };
474
+ ```
475
+
476
+ #### `NullOrEmptyFilterOperators`
477
+
478
+ From `@nanahoshi/mona-ui/query`.
479
+
480
+ ```typescript
481
+ type NullOrEmptyFilterOperators = "isnullorempty" | "isnotnullorempty";
482
+ ```
483
+
484
+ #### `NumericFilterDescriptor`
485
+
486
+ From `@nanahoshi/mona-ui/query`.
487
+
488
+ ```typescript
489
+ type NumericFilterDescriptor = FieldDefinition & ( | { operator: NumericFilterOperators; value: number; } | NullFilterDescriptor );
490
+ ```
491
+
492
+ #### `NumericFilterOperators`
493
+
494
+ From `@nanahoshi/mona-ui/query`.
495
+
496
+ ```typescript
497
+ type NumericFilterOperators = CommonFilterOperators | "gt" | "gte" | "lt" | "lte" | NullFilterOperators;
498
+ ```
499
+
500
+ #### `RangeFilterBound`
501
+
502
+ From `@nanahoshi/mona-ui/query`.
503
+
504
+ A bound of a range filter. `null` leaves that end open.
505
+
506
+ ```typescript
507
+ type RangeFilterBound = number | Date | string | null;
508
+ ```
509
+
510
+ #### `RangeFilterDescriptor`
511
+
512
+ From `@nanahoshi/mona-ui/query`.
513
+
514
+ `between` and `notbetween` include both bounds.
515
+
516
+ ```typescript
517
+ type RangeFilterDescriptor = FieldDefinition & { operator: RangeFilterOperators; value: readonly [from: RangeFilterBound, to: RangeFilterBound]; };
518
+ ```
519
+
520
+ #### `RangeFilterOperators`
521
+
522
+ From `@nanahoshi/mona-ui/query`.
523
+
524
+ ```typescript
525
+ type RangeFilterOperators = "between" | "notbetween";
526
+ ```
527
+
528
+ #### `SortDescriptor`
529
+
530
+ From `@nanahoshi/mona-ui/query`.
531
+
532
+ **Type parameters:** `TValue = unknown`
533
+
534
+ | Name | Type |
535
+ | --- | --- |
536
+ | `dir` | `SortDirection` |
537
+ | `field` | `string` |
538
+
539
+ **Methods**
540
+
541
+ | Signature | Description |
542
+ | --- | --- |
543
+ | `sort(left: TValue, right: TValue): number` | Compares two values of `field` (not two rows). Return a negative number when `left` comes first in ascending order; `dir: "desc"` reverses the result. Empty values are passed in as they are. |
544
+
545
+ #### `SortDirection`
546
+
547
+ From `@nanahoshi/mona-ui/query`.
548
+
549
+ ```typescript
550
+ type SortDirection = "asc" | "desc";
551
+ ```
552
+
553
+ #### `StringFilterDescriptor`
554
+
555
+ From `@nanahoshi/mona-ui/query`.
556
+
557
+ ```typescript
558
+ type StringFilterDescriptor = FieldDefinition & ( | { operator: StringFilterOperators; value: string; } | NullFilterDescriptor | EmptyFilterDescriptor | NullOrEmptyFilterDescriptor );
559
+ ```
560
+
561
+ #### `StringFilterOperators`
562
+
563
+ From `@nanahoshi/mona-ui/query`.
564
+
565
+ ```typescript
566
+ type StringFilterOperators = | CommonFilterOperators | "startswith" | "endswith" | "contains" | "doesnotcontain" | NullFilterOperators | EmptyFilterOperators | NullOrEmptyFilterOperators;
567
+ ```
568
+
569
+ **Documented elsewhere:** [`VirtualScrollOptions`](list-view.md)