@spectrum-web-components/table 0.0.2-table.2609 → 0.0.2-table.2695

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.
Files changed (57) hide show
  1. package/README.md +362 -102
  2. package/custom-elements.json +169 -89
  3. package/elements.d.ts +7 -0
  4. package/elements.js +19 -0
  5. package/elements.js.map +1 -0
  6. package/package.json +11 -15
  7. package/src/Table.d.ts +38 -1
  8. package/src/Table.js +100 -74
  9. package/src/Table.js.map +1 -1
  10. package/src/TableBody.js +1 -1
  11. package/src/TableBody.js.map +1 -1
  12. package/src/TableCell.d.ts +0 -1
  13. package/src/TableCell.js +0 -4
  14. package/src/TableCell.js.map +1 -1
  15. package/src/TableCheckboxCell.d.ts +1 -1
  16. package/src/TableCheckboxCell.js +3 -11
  17. package/src/TableCheckboxCell.js.map +1 -1
  18. package/src/TableHead.d.ts +1 -0
  19. package/src/TableHead.js +1 -1
  20. package/src/TableHead.js.map +1 -1
  21. package/src/TableHeadCell.d.ts +2 -1
  22. package/src/TableHeadCell.js +2 -4
  23. package/src/TableHeadCell.js.map +1 -1
  24. package/src/TableRow.d.ts +8 -1
  25. package/src/TableRow.js +39 -3
  26. package/src/TableRow.js.map +1 -1
  27. package/src/spectrum-config.js +10 -7
  28. package/src/spectrum-table-head-cell.css.js +1 -1
  29. package/src/spectrum-table-head-cell.css.js.map +1 -1
  30. package/src/spectrum-table.css.js +83 -103
  31. package/src/spectrum-table.css.js.map +1 -1
  32. package/src/table-body.css.js +1 -1
  33. package/src/table-body.css.js.map +1 -1
  34. package/src/table-checkbox-cell.css.js +1 -1
  35. package/src/table-checkbox-cell.css.js.map +1 -1
  36. package/src/table-head-cell.css.js +1 -1
  37. package/src/table-head-cell.css.js.map +1 -1
  38. package/src/table.css.js +83 -103
  39. package/src/table.css.js.map +1 -1
  40. package/stories/index.js +5 -12
  41. package/stories/index.js.map +1 -1
  42. package/stories/table-elements.stories.js +84 -21
  43. package/stories/table-elements.stories.js.map +1 -1
  44. package/stories/table-virtualized.stories.js +68 -22
  45. package/stories/table-virtualized.stories.js.map +1 -1
  46. package/test/benchmark/basic-test.js +39 -1
  47. package/test/benchmark/basic-test.js.map +1 -1
  48. package/test/table-selects.test.js +214 -0
  49. package/test/table-selects.test.js.map +1 -0
  50. package/test/table.test.js +36 -689
  51. package/test/table.test.js.map +1 -1
  52. package/test/virtualized-table-selects.test.js +341 -0
  53. package/test/virtualized-table-selects.test.js.map +1 -0
  54. package/test/virtualized-table.test.js +262 -0
  55. package/test/virtualized-table.test.js.map +1 -0
  56. package/stories/table.stories.js.map +0 -1
  57. package/test/table.test-vrt.js.map +0 -1
package/README.md CHANGED
@@ -13,6 +13,12 @@ yarn add @spectrum-web-components/table
13
13
 
14
14
  Import the side effectful registration of `<sp-table>`, `<sp-table-body>`, `<sp-table-cell>`, `<sp-table-checkbox-cell>`, `<sp-table-head>`, `<sp-table-head-cell>`. and `<sp-table-row>` via:
15
15
 
16
+ ```
17
+ import '@spectrum-web-components/table/elements.js';
18
+ ```
19
+
20
+ Or individually via:
21
+
16
22
  ```
17
23
  import '@spectrum-web-components/table/sp-table.js';
18
24
  import '@spectrum-web-components/table/sp-table-body.js';
@@ -42,7 +48,7 @@ import {
42
48
  To ensure that the table scrolls, make sure to add a `style` attribute to `<sp-table>` with your desired height. Otherwise, the table will automatically show all its items.
43
49
 
44
50
  ```html
45
- <sp-table size="m" style="height: 120px">
51
+ <sp-table size="m">
46
52
  <sp-table-head>
47
53
  <sp-table-head-cell>Column Title</sp-table-head-cell>
48
54
  <sp-table-head-cell>Column Title</sp-table-head-cell>
@@ -78,99 +84,21 @@ To ensure that the table scrolls, make sure to add a `style` attribute to `<sp-t
78
84
  </sp-table>
79
85
  ```
80
86
 
81
- ## Sorting
82
-
83
- For each table column you want to sort, use the `sortable` attribute in its respective `<sp-table-head-cell>`. `sort-direction="asc"|"desc"` specifies the direction the sort goes, in either ascending or descending order, respectively. The `@sorted` event listener on `<sp-table>` can be utilised to specify a method to fire when the `<sp-table-head-cell>` dispatches the `sorted` event. To specify which aspect of an item you'd like to sort by, use the `sort-key` attribute.
84
-
85
- ```html
86
- <sp-table size="m" style="height: 120px">
87
- <sp-table-head>
88
- <sp-table-head-cell sortable sort-direction="asc">
89
- Column Title
90
- </sp-table-head-cell>
91
- <sp-table-head-cell>Column Title</sp-table-head-cell>
92
- <sp-table-head-cell>Column Title</sp-table-head-cell>
93
- </sp-table-head>
94
- <sp-table-body>
95
- <sp-table-row>
96
- <sp-table-cell>Row Item Bravo</sp-table-cell>
97
- <sp-table-cell>Row Item Bravo</sp-table-cell>
98
- <sp-table-cell>Row Item Bravo</sp-table-cell>
99
- </sp-table-row>
100
- <sp-table-row>
101
- <sp-table-cell>Row Item Alpha</sp-table-cell>
102
- <sp-table-cell>Row Item Alpha</sp-table-cell>
103
- <sp-table-cell>Row Item Alpha</sp-table-cell>
104
- </sp-table-row>
105
- <sp-table-row>
106
- <sp-table-cell>Row Item Charlie</sp-table-cell>
107
- <sp-table-cell>Row Item Charlie</sp-table-cell>
108
- <sp-table-cell>Row Item Charlie</sp-table-cell>
109
- </sp-table-row>
110
- <sp-table-row>
111
- <sp-table-cell>Row Item Delta</sp-table-cell>
112
- <sp-table-cell>Row Item Delta</sp-table-cell>
113
- <sp-table-cell>Row Item Delta</sp-table-cell>
114
- </sp-table-row>
115
- <sp-table-row>
116
- <sp-table-cell>Row Item Echo</sp-table-cell>
117
- <sp-table-cell>Row Item Echo</sp-table-cell>
118
- <sp-table-cell>Row Item Echo</sp-table-cell>
119
- </sp-table-row>
120
- </sp-table-body>
121
- </sp-table>
122
- ```
123
-
124
87
  ## Selection
125
88
 
126
89
  To manage selection on an `<sp-table>`, utilise the `selects` attribute on `<sp-table>`. Each `<sp-table-row>` has a `value` attribute which, by default, corresponds to its index in the table, and these `value`s tell `<sp-table>` which `<sp-table-row>`s are selected. The selected items can be manually fed in through the `.selected` attribute on the table.
127
90
 
128
- ```html
129
- <sp-table size="m" style="height: 120px">
130
- <sp-table-head>
131
- <sp-table-head-cell sortable sort>Column Title</sp-table-head-cell>
132
- <sp-table-head-cell>Column Title</sp-table-head-cell>
133
- <sp-table-head-cell>Column Title</sp-table-head-cell>
134
- </sp-table-head>
135
- <sp-table-body>
136
- <sp-table-row>
137
- <sp-table-cell>Row Item Alpha</sp-table-cell>
138
- <sp-table-cell>Row Item Alpha</sp-table-cell>
139
- <sp-table-cell>Row Item Alpha</sp-table-cell>
140
- </sp-table-row>
141
- <sp-table-row>
142
- <sp-table-cell>Row Item Bravo</sp-table-cell>
143
- <sp-table-cell>Row Item Bravo</sp-table-cell>
144
- <sp-table-cell>Row Item Bravo</sp-table-cell>
145
- </sp-table-row>
146
- <sp-table-row>
147
- <sp-table-cell>Row Item Charlie</sp-table-cell>
148
- <sp-table-cell>Row Item Charlie</sp-table-cell>
149
- <sp-table-cell>Row Item Charlie</sp-table-cell>
150
- </sp-table-row>
151
- <sp-table-row>
152
- <sp-table-cell>Row Item Delta</sp-table-cell>
153
- <sp-table-cell>Row Item Delta</sp-table-cell>
154
- <sp-table-cell>Row Item Delta</sp-table-cell>
155
- </sp-table-row>
156
- <sp-table-row>
157
- <sp-table-cell>Row Item Echo</sp-table-cell>
158
- <sp-table-cell>Row Item Echo</sp-table-cell>
159
- <sp-table-cell>Row Item Echo</sp-table-cell>
160
- </sp-table-row>
161
- </sp-table-body>
162
- </sp-table>
163
- ```
164
-
165
91
  ### `selects="single"`
166
92
 
93
+ When `selects="single"` the `<sp-table>` will manage a _single_ selection in the array value of `selected`.
94
+
167
95
  ```html
168
96
  <sp-table
169
97
  size="m"
170
98
  selects="single"
171
99
  selected='["row1"]'
172
- style="height: 120px"
173
- onchange="spAlert(`Selected: ${JSON.stringify(this.selected)`)"
100
+ style="height: 200px"
101
+ onchange="spAlert(this, `Selected: ${JSON.stringify(this.selected)}`)"
174
102
  >
175
103
  <sp-table-head>
176
104
  <sp-table-head-cell>Column Title</sp-table-head-cell>
@@ -209,15 +137,15 @@ To manage selection on an `<sp-table>`, utilise the `selects` attribute on `<sp-
209
137
 
210
138
  ### `selects="multiple"`
211
139
 
212
- When `selects` is set to "multiple", the `<sp-table-checkbox-cell>` in `<sp-table-head>` acts as the select/deselect all button.
140
+ When `selects="multiple"` the `<sp-table>` will manage a selection in the array value of `selected` in via a presence toggle. Further, an `<sp-table-checkbox-cell>` will be made available in the `<sp-table-head>` in order to select/deselect all items in the `<sp-table>`.
213
141
 
214
142
  ```html
215
143
  <sp-table
216
144
  size="m"
217
- style="height: 120px"
145
+ style="height: 200px"
218
146
  selects="multiple"
219
147
  selected='["row1", "row2"]'
220
- onchange="spAlert(`Selected: ${JSON.stringify(this.selected)`)"
148
+ onchange="spAlert(this, `Selected: ${JSON.stringify(this.selected)}`)"
221
149
  >
222
150
  <sp-table-head>
223
151
  <sp-table-head-cell>Column Title</sp-table-head-cell>
@@ -262,6 +190,8 @@ For large amounts of data, the `<sp-table>` can be virtualised to easily add tab
262
190
  <sp-table
263
191
  size="m"
264
192
  id="table-virtualized-demo"
193
+ style="height: 200px"
194
+ scroller="true"
265
195
  >
266
196
  <sp-table-head>
267
197
  <sp-table-head-cell>Column Title</sp-table-head-cell>
@@ -290,13 +220,15 @@ For large amounts of data, the `<sp-table>` can be virtualised to easily add tab
290
220
  const cell1 = document.createElement('sp-table-cell');
291
221
  const cell2 = document.createElement('sp-table-cell');
292
222
  const cell3 = document.createElement('sp-table-cell');
293
- cell1.textContent = `Row Item Alpha ${item.value}`;
223
+ cell1.textContent = `Row Item Alpha ${item.name}`;
294
224
  cell2.textContent = `Row Item Alpha ${index}`;
295
225
  cell3.textContent = `Last Thing`;
296
226
  return [cell1, cell2, cell3];
297
227
  }
298
228
  };
299
- setTimeout(initTable, 500);
229
+ customElements.whenDefined('sp-table').then(() => {
230
+ initTable();
231
+ });
300
232
  </script>
301
233
  ```
302
234
 
@@ -314,7 +246,6 @@ For large amounts of data, the `<sp-table>` can be virtualised to easily add tab
314
246
  return items;
315
247
  }
316
248
 
317
- }
318
249
  const initTable = () => {
319
250
  const table = document.querySelector('#table-virtualized-demo');
320
251
  table.items = initItems(50);
@@ -323,18 +254,20 @@ For large amounts of data, the `<sp-table>` can be virtualised to easily add tab
323
254
  const cell1 = document.createElement('sp-table-cell');
324
255
  const cell2 = document.createElement('sp-table-cell');
325
256
  const cell3 = document.createElement('sp-table-cell');
326
- cell1.textContent = `Row Item Alpha ${item.value}`;
257
+ cell1.textContent = `Row Item Alpha ${item.name}`;
327
258
  cell2.textContent = `Row Item Alpha ${index}`;
328
259
  cell3.textContent = `Last Thing`;
329
260
  return [cell1, cell2, cell3];
330
261
  }
331
262
  };
332
- setTimeout(initTable, 500);
263
+ customElements.whenDefined('sp-table').then(() => {
264
+ initTable();
265
+ });
333
266
  </script>
334
267
 
335
268
  ### How to use it
336
269
 
337
- The virtualised table takes `.items`, an array of type `Record`, where the key is a `string` and the value can be whatever you'd like. `.items` is then fed into the `renderItem` method, which takes an `item` and its `index` as parameters and renders the `<sp-table-row>` for each item. An example is as follows:
270
+ The virtualised table takes `items` as either a property or a JSON-encoded string, an array of type `Record`, where the key is a `string` and the value can be whatever you'd like. `items` is then fed into the `renderItem` method, which takes an `item` and its `index` as parameters and renders the `<sp-table-row>` for each item. An example is as follows:
338
271
 
339
272
  ```javascript
340
273
  const renderItem = (item: Item, index: number): TemplateResult => {
@@ -346,19 +279,346 @@ const renderItem = (item: Item, index: number): TemplateResult => {
346
279
  };
347
280
  ```
348
281
 
349
- `.renderItem` is then included as a property of `<sp-table>`, along with the `.items`, to render a full `<sp-table>` without excessive manual HTML writing.
282
+ `renderItem` is then included as a property of `<sp-table>`, along with the `items`, to render a full `<sp-table>` without excessive manual HTML writing.
283
+
284
+ You can also render a different cell at a particular index by doing something like below:
285
+
286
+ ```javascript
287
+ const renderItem = (item: Item, index: number): TemplateResult => {
288
+ if (index === 15) {
289
+ return html`
290
+ <sp-table-cell style="text-align: center">Custom Row</sp-table-cell>
291
+ `;
292
+ }
293
+ return html`
294
+ <sp-table-cell>Row Item ${item.name}</sp-table-cell>
295
+ <sp-table-cell>Row Item ${item.date}</sp-table-cell>
296
+ <sp-table-cell>Row Item ${index}</sp-table-cell>
297
+ `;
298
+ };
299
+ ```
350
300
 
351
301
  Please note that there is a bug when attempting to select all virtualised elements. The items are selected programatically, it's just not reflected visually.
352
302
 
353
- ## TO-DO:
303
+ ### Selection
354
304
 
355
- Scrolling w/ screenreader on virtualised table elements
305
+ When making a selection on a virtualized table, it can sometimes be useful to track that selection as something other than an array of indexes. To make this possible the `itemValue` property will accept a method who argument is an item object, return a computed string value to track selection with this value rather than the item's index:
356
306
 
357
- NOT CURRENTLY NEEDED but still important:
307
+ ```html-live
308
+ <sp-table
309
+ size="m"
310
+ id="table-item-value-demo"
311
+ style="height: 200px"
312
+ scroller="true"
313
+ selects="multiple"
314
+ >
315
+ <sp-table-head>
316
+ <sp-table-head-cell>Column Title</sp-table-head-cell>
317
+ <sp-table-head-cell>Column Title</sp-table-head-cell>
318
+ <sp-table-head-cell>Column Title</sp-table-head-cell>
319
+ </sp-table-head>
320
+ </sp-table>
321
+ <div class="selection">Selected: [ ]</div>
322
+ <script type="module">
323
+ const initItems = (count) => {
324
+ const total = count;
325
+ const items = [];
326
+ while (count) {
327
+ count--;
328
+ items.push({
329
+ id: crypto.randomUUID(),
330
+ name: String(total - count),
331
+ date: count,
332
+ });
333
+ }
334
+ return items;
335
+ };
336
+ const initTable = () => {
337
+ const table = document.querySelector('#table-item-value-demo');
338
+ table.items = initItems(50);
339
+
340
+ table.renderItem = (item, index) => {
341
+ const cell1 = document.createElement('sp-table-cell');
342
+ const cell2 = document.createElement('sp-table-cell');
343
+ const cell3 = document.createElement('sp-table-cell');
344
+ cell1.textContent = `Row Item Alpha ${item.name}`;
345
+ cell2.textContent = `Row Item Alpha ${index}`;
346
+ cell3.textContent = `Last Thing`;
347
+ return [cell1, cell2, cell3];
348
+ };
349
+
350
+ table.addEventListener('change', (event) => {
351
+ const selected = event.target.nextElementSibling;
352
+ selected.textContent = `Selected: ${JSON.stringify(event.target.selected, null, ' ')}`;
353
+ });
354
+ };
355
+ customElements.whenDefined('sp-table').then(() => {
356
+ initTable();
357
+ });
358
+ </script>
359
+ ```
360
+
361
+ <script type="module">
362
+ const initItems = (count) => {
363
+ const total = count;
364
+ const items = [];
365
+ while (count) {
366
+ count--;
367
+ items.push({
368
+ id: crypto.randomUUID(),
369
+ name: String(total - count),
370
+ date: count,
371
+ });
372
+ }
373
+ return items;
374
+ }
358
375
 
359
- 1. multiselects via attributes (not required for Express delivery)
360
- 2. Update checkbox element to dispatch event correctly
361
- 3. Non-virtual sorting (ie sort data supplied through the DOM)
362
- 4. Handle the console error that happens when we don't use Virtualizer
363
- 5. Manage sort internally & prevent sort events
364
- 6. Preventing change events
376
+ const initTable = () => {
377
+ const table = document.querySelector('#table-item-value-demo');
378
+ table.items = initItems(50);
379
+ table.itemValue = (item) => item.id;
380
+
381
+ table.renderItem = (item, index) => {
382
+ const cell1 = document.createElement('sp-table-cell');
383
+ const cell2 = document.createElement('sp-table-cell');
384
+ const cell3 = document.createElement('sp-table-cell');
385
+ cell1.textContent = `Row Item Alpha ${item.name}`;
386
+ cell2.textContent = `Row Item Alpha ${index}`;
387
+ cell3.textContent = `Last Thing`;
388
+ return [cell1, cell2, cell3];
389
+ };
390
+
391
+ table.addEventListener('change', (event) => {
392
+ const selected = event.target.nextElementSibling;
393
+ selected.textContent = `Selected: ${JSON.stringify(event.target.selected, null, ' ')}`;
394
+ });
395
+ };
396
+ customElements.whenDefined('sp-table').then(() => {
397
+ initTable();
398
+ });
399
+ </script>
400
+
401
+ ### Row Types
402
+
403
+ All values in the item array are assumed to be homogenous by default. This means all of the rendered rows are either delivered as provided, or, in the case you are leveraging `selects`, rendered with an `<sp-table-checkbox-cell>`. However, when virtualizing a table with selection it can sometimes be useful to surface rows with additional interactions, e.g. "Load more data" links. To support that you can optionally include the `_$rowType$` brand in your item. The values for this are outlined by the `RowType` enum and include `ITEM` (0) and `INFORMATION` (1). When `_$rowType$: RowType.INFORMATION` is provided it informs the `<sp-table>` element not to deliver an `<sp-table-checkbox-cell>` with that row.
404
+
405
+ ```html-live
406
+ <sp-table
407
+ size="m"
408
+ id="table-row-type-demo"
409
+ style="height: 200px"
410
+ scroller="true"
411
+ selects="single"
412
+ >
413
+ <sp-table-head>
414
+ <sp-table-head-cell>Column Title</sp-table-head-cell>
415
+ <sp-table-head-cell>Column Title</sp-table-head-cell>
416
+ <sp-table-head-cell>Column Title</sp-table-head-cell>
417
+ </sp-table-head>
418
+ </sp-table>
419
+ <script type="module">
420
+ const initItems = (count) => {
421
+ const total = count;
422
+ const items = [];
423
+ while (count) {
424
+ count--;
425
+ items.push({
426
+ name: String(total - count),
427
+ date: count,
428
+ });
429
+ }
430
+ return items;
431
+ };
432
+ const initTable = () => {
433
+ const table = document.querySelector('#table-row-type-demo');
434
+ const items = initItems(50);
435
+ items.splice(3, 0, {
436
+ _$rowType$: 1,
437
+ });
438
+ table.items = items;
439
+
440
+ table.renderItem = (item, index) => {
441
+ if (item._$rowType$ === 1) {
442
+ const infoCell = document.createElement('sp-table-cell');
443
+ infoCell.textContent = 'Use this row type for non-selectable content.';
444
+ return [infoCell];
445
+ }
446
+ const cell1 = document.createElement('sp-table-cell');
447
+ const cell2 = document.createElement('sp-table-cell');
448
+ const cell3 = document.createElement('sp-table-cell');
449
+ cell1.textContent = `Row Item Alpha ${item.name}`;
450
+ cell2.textContent = `Row Item Alpha ${index}`;
451
+ cell3.textContent = `Last Thing`;
452
+ return [cell1, cell2, cell3];
453
+ };
454
+ };
455
+ customElements.whenDefined('sp-table').then(() => {
456
+ initTable();
457
+ });
458
+ </script>
459
+ ```
460
+
461
+ <script type="module">
462
+ const initItems = (count) => {
463
+ const total = count;
464
+ const items = [];
465
+ while (count) {
466
+ count--;
467
+ items.push({
468
+ name: String(total - count),
469
+ date: count,
470
+ });
471
+ }
472
+ return items;
473
+ }
474
+
475
+ const initTable = () => {
476
+ const table = document.querySelector('#table-row-type-demo');
477
+ const items = initItems(50);
478
+ items.splice(3, 0, {
479
+ _$rowType$: 1,
480
+ });
481
+ table.items = items;
482
+
483
+
484
+ table.renderItem = (item, index) => {
485
+ if (item._$rowType$ === 1) {
486
+ const infoCell = document.createElement('sp-table-cell');
487
+ infoCell.textContent = 'Use this row type for non-selectable content.';
488
+ return [infoCell];
489
+ }
490
+ const cell1 = document.createElement('sp-table-cell');
491
+ const cell2 = document.createElement('sp-table-cell');
492
+ const cell3 = document.createElement('sp-table-cell');
493
+ cell1.textContent = `Row Item Alpha ${item.name}`;
494
+ cell2.textContent = `Row Item Alpha ${index}`;
495
+ cell3.textContent = `Last Thing`;
496
+ return [cell1, cell2, cell3];
497
+ };
498
+ };
499
+ customElements.whenDefined('sp-table').then(() => {
500
+ initTable();
501
+ });
502
+ </script>
503
+
504
+ ### The `scroller` property
505
+
506
+ By default, the virtualized table doesn't contain a scroll bar and will display the entire length of the table body. Use the `scroller` property and specify an inline style for the height to get a `Table` of your desired height that scrolls.
507
+
508
+ ## Sorting on the Virtualized Table
509
+
510
+ The virtualized table supports sorting its elements.
511
+
512
+ For each table column you want to sort, use the `sortable` attribute in its respective `<sp-table-head-cell>`. `sort-direction="asc"|"desc"` specifies the direction the sort goes, in either ascending or descending order, respectively. The `@sorted` event listener on `<sp-table>` can be utilised to specify a method to fire when the `<sp-table-head-cell>` dispatches the `sorted` event. To specify which aspect of an item you'd like to sort by, use the `sort-key` attribute.
513
+
514
+ ```html-live
515
+ <sp-table
516
+ size="m"
517
+ id="sorted-virtualized-table"
518
+ style="height: 200px"
519
+ scroller="true"
520
+ >
521
+ <sp-table-head>
522
+ <sp-table-head-cell sortable sort-direction="desc" sort-key="name">
523
+ Sortable Column
524
+ </sp-table-head-cell>
525
+ <sp-table-head-cell>Non-sortable Column</sp-table-head-cell>
526
+ <sp-table-head-cell>Non-sortable Column</sp-table-head-cell>
527
+ </sp-table-head>
528
+ </sp-table>
529
+ <script type="module">
530
+ const initItems = (count) => {
531
+ const total = count;
532
+ const items = [];
533
+ while (count) {
534
+ count--;
535
+ items.push({
536
+ name: String(total - count),
537
+ date: count,
538
+ });
539
+ }
540
+ return items;
541
+ }
542
+
543
+ let items = initItems(50);
544
+
545
+ const initTable = () => {
546
+ const table = document.querySelector('#sorted-virtualized-table');
547
+
548
+ table.items = items;
549
+
550
+ table.renderItem = (item, index) => {
551
+ const cell1 = document.createElement('sp-table-cell');
552
+ const cell2 = document.createElement('sp-table-cell');
553
+ const cell3 = document.createElement('sp-table-cell');
554
+ cell1.textContent = `Row Item Alpha ${item.name}`;
555
+ cell2.textContent = `Row Item Beta ${item.date}`;
556
+ cell3.textContent = `Index: ${index}`;
557
+ return [cell1, cell2, cell3];
558
+ }
559
+ table.addEventListener('sorted', (event) => {
560
+ const { sortDirection, sortKey } = event.detail;
561
+ items = items.sort((a, b) => {
562
+ const first = String(a[sortKey]);
563
+ const second = String(b[sortKey]);
564
+ return sortDirection === 'asc'
565
+ ? first.localeCompare(second)
566
+ : second.localeCompare(first);
567
+ });
568
+ table.items = [...items];
569
+ });
570
+ };
571
+
572
+ customElements.whenDefined('sp-table').then(() => {
573
+ initTable();
574
+ });
575
+ </script>
576
+ ```
577
+
578
+ <script type="module">
579
+ const initItems = (count) => {
580
+ const total = count;
581
+ const items = [];
582
+ while (count) {
583
+ count--;
584
+ items.push({
585
+ name: String(total - count),
586
+ date: count,
587
+ });
588
+ }
589
+ return items;
590
+ }
591
+
592
+ let items = initItems(50);
593
+
594
+ const initTable = () => {
595
+ const table = document.querySelector('#sorted-virtualized-table');
596
+
597
+ table.items = items;
598
+
599
+ table.renderItem = (item, index) => {
600
+ const cell1 = document.createElement('sp-table-cell');
601
+ const cell2 = document.createElement('sp-table-cell');
602
+ const cell3 = document.createElement('sp-table-cell');
603
+ cell1.textContent = `Row Item Alpha ${item.name}`;
604
+ cell2.textContent = `Row Item Beta ${item.date}`;
605
+ cell3.textContent = `Index: ${index}`;
606
+ return [cell1, cell2, cell3];
607
+ }
608
+ table.addEventListener('sorted', (event) => {
609
+ const { sortDirection, sortKey } = event.detail;
610
+ items = items.sort((a, b) => {
611
+ const first = String(a[sortKey]);
612
+ const second = String(b[sortKey]);
613
+ return sortDirection === 'asc'
614
+ ? first.localeCompare(second)
615
+ : second.localeCompare(first);
616
+ });
617
+ table.items = [...items];
618
+ });
619
+ };
620
+
621
+ customElements.whenDefined('sp-table').then(() => {
622
+ initTable();
623
+ });
624
+ </script>