Sticky rows

Published at Oct 15, 2024

Barcode
Location
Category
Quantiy
Weight
3056519006086
Apt 1819
furniture
972
25.6
Barcode 6861816867653
Location Suite 11
Category furniture
Quantiy 38
Weight 52.5
4697486394862
Apt 767
furniture
714
22.1
Barcode 1507651383044
Location Suite 47
Category electronics
Quantiy 212
Weight 49.5
3050521118264
Room 1868
furniture
509
23.5
Barcode 7247654023537
Location Room 1846
Category clothing
Quantiy 848
Weight 88.6
6687435198303
PO Box 75154
electronics
149
89
Barcode 2530797670037
Location PO Box 39137
Category furniture
Quantiy 147
Weight 8.3
7524918686628
Room 1284
clothing
950
33.9
Barcode 3209383758542
Location 18th Floor
Category furniture
Quantiy 200
Weight 4
4885399982401
Suite 20
electronics
226
34.4
Barcode 9040207608954
Location 10th Floor
Category clothing
Quantiy 618
Weight 3.2
4481430085015
Suite 25
furniture
770
29.1
Barcode 6570375480662
Location Suite 73
Category clothing
Quantiy 435
Weight 16
4677591243156
Apt 572
clothing
105
18.4
Barcode 1477064628428
Location Room 1965
Category furniture
Quantiy 350
Weight 10.8
8748415132296
Apt 110
electronics
290
25.8
Barcode 5101093419413
Location Room 139
Category clothing
Quantiy 69
Weight 80.4
3108485537297
Room 1571
clothing
201
49.2
Barcode 9628053961836
Location Suite 97
Category electronics
Quantiy 856
Weight 13.8

Implementation

You can force a row to stay in place by adding class

class={`${Datagrid.STAY_IN_PLACE} ${Datagrid.HIDE_BEHIND_PARENT_ROW} `}

Code

Column definitions

import type { BaseColumn } from '$lib/datagrid/types';
import type { InventoryDataRow } from '$lib/data/inventory';

export const columns = [
	{
		id: 'barcode',
		title: 'Barcode',
		width: '200px'
	},
	{
		id: 'location',
		title: 'Location',
		grow: true,
		width: '200px'
	},
	{
		id: 'category',
		title: 'Category',
		pinnable: false,
		width: '1000px'
	},
	{
		id: 'quantity',
		title: 'Quantiy',
		width: '200px'
	},
	{
		id: 'weight',
		title: 'Weight',
		width: '200px'
	}
] satisfies BaseColumn<InventoryDataRow>[];

Datagrid component

<script lang="ts">
	import { setContext } from 'svelte';
	import { columns } from './columns.svelte';
	import { inventoryData as data } from '$lib/data/inventory';
	import { TzezarDatagrid } from '$lib/datagrid/tzezar-datagrid.svelte';
	import * as Datagrid from '$lib/datagrid';
	import { getNestedValue } from '$lib/datagrid/fns/get-nested-value';

	let datagrid = setContext(
		'datagrid',
		new TzezarDatagrid({
			data: data.slice(0, 20),
			columns,
			options: {
				pagination: { display: false }
			}
		})
	);
</script>

<Datagrid.Datagrid>
	{#snippet head()}
		{#each datagrid.columns as column (column.id)}
			<Datagrid.Header {column} />
		{/each}
	{/snippet}
	{#snippet body()}
		{#each datagrid.data as row, rowIndex}
			{#if rowIndex % 2 === 0}
				<Datagrid.Row {rowIndex}>
					{#each datagrid.columns as column, columnIndex}
						{@const props = { row, rowIndex, column, columnIndex }}
						<Datagrid.Cell {...props} />
					{/each}
				</Datagrid.Row>
			{:else}
				<Datagrid.Row
					class={`${Datagrid.STAY_IN_PLACE} ${Datagrid.HIDE_BEHIND_PARENT_ROW} `}
					{rowIndex}
				>
					<div class="min-w-full">
						{#each datagrid.columns as column, columnIndex}
							{@const props = { row, rowIndex, column, columnIndex }}
							<Datagrid.Cell {...props}>
								<span>{column.title}</span>
								<span>{getNestedValue(row, column.id)}</span>
							</Datagrid.Cell>
						{/each}
					</div>
				</Datagrid.Row>
			{/if}
		{/each}
	{/snippet}
</Datagrid.Datagrid>
In the coming days, the grid will officially launch its version 1.0. I recommend waiting until the release before incorporating it into your projects, as we've completely rewritten and simplified the internal logic, making it more efficient. Version 1.0 will also introduce exciting new features such as row grouping and aggregation. If you have any questions or feedback, feel free to reach out! If you find this project valuable, please consider giving it a star on GitHub. Thank you for your support! ❤️