Skip to content

Commit 3a8b2f1

Browse files
committed
#112 fix resizing issues after page size or page index changes
1 parent 1e1f44d commit 3a8b2f1

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

src/lib/components/Table/ColumnsMenu.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
aria-label={`${column.visible ? 'Hide' : 'Show'} ${column.label} column`}
5858
type="checkbox"
5959
class="checkbox"
60+
id={column.id}
6061
bind:checked={column.visible}
6162
title={`${column.visible ? 'Hide' : 'Show'} ${column.label} column`}
6263
disabled={columns.filter((c) => c.visible).length === 1 && column.visible}

src/lib/components/Table/TableContent.svelte

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { createEventDispatcher } from 'svelte';
2+
import { afterUpdate, onDestroy, createEventDispatcher } from 'svelte';
33
import { readable, writable } from 'svelte/store';
44
55
import Fa from 'svelte-fa';
@@ -80,12 +80,7 @@
8080
const actionDispatcher = (obj) => dispatch('action', obj);
8181
8282
// Stores to hold the width and height information for resizing
83-
const rowHeights = writable<
84-
{
85-
max: number;
86-
min: number;
87-
}[]
88-
>([]);
83+
const rowHeights = writable<{ [key: number]: { max: number; min: number } }>({});
8984
const colWidths = writable<number[]>([]);
9085
9186
// Server-side variables
@@ -392,11 +387,6 @@
392387
const getMaxCellHeightInRow = () => {
393388
if (!tableRef || resizable === 'columns' || resizable === 'none') return;
394389
395-
// Initialize the rowHeights array if it is empty
396-
if ($rowHeights.length === 0) {
397-
$rowHeights = Array.from({ length: $pageRows.length }, () => ({ max: 44, min: 20 }));
398-
}
399-
400390
tableRef.querySelectorAll('tbody tr').forEach((row, index) => {
401391
const cells = row.querySelectorAll('td');
402392
@@ -415,9 +405,14 @@
415405
});
416406
417407
rowHeights.update((rh) => {
418-
rh[index].max = maxHeight - 24;
419-
rh[index].min = Math.max(minHeight - 24, rowHeight ?? 20);
420-
return rh;
408+
const id = +row.id.split(`${tableId}-row-`)[1];
409+
return {
410+
...rh,
411+
[id]: {
412+
max: maxHeight - 24,
413+
min: Math.max(minHeight - 24, rowHeight ?? 20)
414+
}
415+
};
421416
});
422417
});
423418
};
@@ -431,7 +426,7 @@
431426
}
432427
433428
colWidths.update((cw) => {
434-
tableRef.querySelectorAll('thead tr th span').forEach((cell, index) => {
429+
tableRef?.querySelectorAll('thead tr th span').forEach((cell, index) => {
435430
// + 12 pixels for padding and + 32 pixels for filter icon
436431
// If the column width is 100, which means it has not been initialized, then calculate the width
437432
cw[index] = cw[index] === 100 ? cell.getBoundingClientRect().width + 12 + 32 : cw[index];
@@ -440,28 +435,57 @@
440435
});
441436
};
442437
443-
const resizeObserver = new ResizeObserver(() => {
438+
const resizeRowsObserver = new ResizeObserver(() => {
444439
getMaxCellHeightInRow();
440+
});
441+
442+
const resizeColumnsObserver = new ResizeObserver(() => {
445443
getMinCellWidthInColumn();
446444
});
447445
448446
const observeFirstCells = () => {
449447
if (!tableRef) return;
450448
449+
$pageRows.forEach((row) => {
450+
const cell = tableRef.querySelector(`#${tableId}-row-${row.id}`);
451+
if (cell) {
452+
resizeRowsObserver.observe(cell);
453+
}
454+
});
455+
451456
tableRef.querySelectorAll('tbody tr td:first-child').forEach((cell) => {
452-
resizeObserver.observe(cell);
457+
resizeRowsObserver.observe(cell);
453458
});
454459
};
455460
456461
const observeHeaderColumns = () => {
457462
if (!tableRef) return;
458463
459464
tableRef.querySelectorAll('thead tr th').forEach((cell) => {
460-
resizeObserver.observe(cell);
465+
resizeColumnsObserver.observe(cell);
461466
});
462467
};
463468
469+
afterUpdate(() => {
470+
if (resizable !== 'rows' && resizable !== 'both') {
471+
return;
472+
}
473+
// Making sure tableRef is up to date and contains the new rows
474+
// If it contains even one element, it means it contains them all
475+
const e = tableRef?.querySelector(`#${tableId}-row-${$pageRows[0].id}`);
476+
if (e) {
477+
getDimensions();
478+
}
479+
});
480+
481+
// Remove the resize observer when the component is destroyed for performance reasons
482+
onDestroy(() => {
483+
resizeRowsObserver.disconnect();
484+
resizeColumnsObserver.disconnect();
485+
});
486+
464487
const getDimensions = () => {
488+
if (!tableRef) return;
465489
if (resizable === 'none') return;
466490
else if (resizable === 'columns') {
467491
observeHeaderColumns();
@@ -477,9 +501,6 @@
477501
$: serverSide && updateTable();
478502
$: serverSide && sortServer($sortKeys[0]?.order, $sortKeys[0]?.id);
479503
$: $hiddenColumnIds = shownColumns.filter((col) => !col.visible).map((col) => col.id);
480-
$: tableRef && getDimensions();
481-
$: $headerRows.length > 0 && getMinCellWidthInColumn();
482-
$: $pageRows.length > 0 && getMaxCellHeightInRow();
483504
</script>
484505

485506
<div class="grid gap-2 overflow-auto" class:w-fit={!fitToScreen} class:w-full={fitToScreen}>
@@ -682,13 +703,13 @@
682703
class=" h-full {index === 0 &&
683704
(resizable === 'rows' || resizable === 'both')
684705
? 'resize-y overflow-auto'
685-
: ''}"
706+
: 'block'}"
686707
id="{tableId}-{cell.id}-{row.id}"
687708
style={`
688-
min-height: ${$rowHeights && $rowHeights[row.id] ? `${$rowHeights[row.id].min}px` : 'auto'};
709+
min-height: ${$rowHeights && $rowHeights[+row.id] ? `${$rowHeights[+row.id].min}px` : 'auto'};
689710
max-height: ${
690-
index !== 0 && $rowHeights && $rowHeights[row.id]
691-
? `${$rowHeights[row.id].max}px`
711+
index !== 0 && $rowHeights && $rowHeights[+row.id]
712+
? `${$rowHeights[+row.id].max}px`
692713
: 'auto'
693714
};
694715
height: ${$rowHeights && $rowHeights[+row.id] ? `${$rowHeights[+row.id].min}px` : 'auto'};
@@ -698,7 +719,7 @@
698719
<div
699720
class="flex items-start overflow-auto"
700721
style={`
701-
max-height: ${$rowHeights && $rowHeights[row.id] ? `${$rowHeights[row.id].max}px` : 'auto'};
722+
max-height: ${$rowHeights && $rowHeights[+row.id] ? `${$rowHeights[+row.id].max}px` : 'auto'};
702723
`}
703724
>
704725
<div

0 commit comments

Comments
 (0)