Allow table dynamic resizing and scrolling whenever rowCount is not specified

This commit is contained in:
Andrew Kingston 2021-04-13 14:14:14 +01:00
parent c3748ce640
commit 08a1b4c1d7
1 changed files with 31 additions and 20 deletions

View File

@ -7,7 +7,7 @@
export let data = [] export let data = []
export let schema = {} export let schema = {}
export let showAutoColumns = false export let showAutoColumns = false
export let rowCount = 14 export let rowCount = 0
export let quiet = true export let quiet = true
export let loading = false export let loading = false
export let allowSelectRows = true export let allowSelectRows = true
@ -23,21 +23,18 @@
const rowHeight = 55 const rowHeight = 55
const headerHeight = 36 const headerHeight = 36
const rowPreload = 5 const rowPreload = 5
const maxRows = 100
// Sorting state // Sorting state
let sortColumn let sortColumn
let sortOrder let sortOrder
// Table state // Table state
let height = 0
let loaded = false let loaded = false
$: if (!loading) loaded = true $: if (!loading) loaded = true
$: rows = data ?? [] $: rows = data ?? []
$: visibleRowCount = loaded $: visibleRowCount = getVisibleRowCount(loaded, height, rows.length, rowCount)
? Math.min(rows.length, rowCount || maxRows, maxRows) $: contentStyle = getContentStyle(visibleRowCount, rowCount)
: rowCount || 8
$: scroll = rows.length > visibleRowCount
$: contentStyle = getContentStyle(visibleRowCount, scroll || !loaded)
$: sortedRows = sortRows(rows, sortColumn, sortOrder) $: sortedRows = sortRows(rows, sortColumn, sortOrder)
$: fields = getFields(schema, showAutoColumns) $: fields = getFields(schema, showAutoColumns)
$: showEditColumn = allowEditRows || allowSelectRows $: showEditColumn = allowEditRows || allowSelectRows
@ -53,8 +50,18 @@
rows.length rows.length
) )
const getContentStyle = (visibleRows, useFixedHeight) => { const getVisibleRowCount = (loaded, height, allRows, rowCount) => {
if (!useFixedHeight) { if (!loaded) {
return rowCount || 0
}
if (rowCount) {
return Math.min(allRows, rowCount)
}
return Math.min(allRows, Math.ceil(height / rowHeight))
}
const getContentStyle = (visibleRows, rowCount) => {
if (!rowCount) {
return "" return ""
} }
return `height: ${headerHeight - 1 + visibleRows * (rowHeight + 1)}px;` return `height: ${headerHeight - 1 + visibleRows * (rowHeight + 1)}px;`
@ -137,6 +144,9 @@
} }
const calculateLastVisibleRow = (firstRow, visibleRowCount, allRowCount) => { const calculateLastVisibleRow = (firstRow, visibleRowCount, allRowCount) => {
if (visibleRowCount === 0) {
return firstRow
}
return Math.min(firstRow + visibleRowCount + 2 * rowPreload, allRowCount) return Math.min(firstRow + visibleRowCount + 2 * rowPreload, allRowCount)
} }
@ -162,14 +172,15 @@
} }
</script> </script>
{#if !loaded} <div
<div class="loading" style={contentStyle} /> bind:offsetHeight={height}
{:else} on:scroll={onScroll}
<div class:quiet
on:scroll={onScroll} style={`--row-height: ${rowHeight}px; --header-height: ${headerHeight}px;`}
class:quiet class="container">
style={`--row-height: ${rowHeight}px; --header-height: ${headerHeight}px;`} {#if !loaded}
class="container"> <div class="loading" style={contentStyle} />
{:else}
<div style={contentStyle}> <div style={contentStyle}>
<table class="spectrum-Table" class:spectrum-Table--quiet={quiet}> <table class="spectrum-Table" class:spectrum-Table--quiet={quiet}>
{#if sortedRows?.length} {#if sortedRows?.length}
@ -265,14 +276,14 @@
focusable="false"> focusable="false">
<use xlink:href="#spectrum-icon-18-Table" /> <use xlink:href="#spectrum-icon-18-Table" />
</svg> </svg>
<div>No rows found.</div> <div>No rows found</div>
</div> </div>
{/if} {/if}
</tbody> </tbody>
</table> </table>
</div> </div>
</div> {/if}
{/if} </div>
<style> <style>
.loading, .loading,