Row expanding

Published at Oct 15, 2024

Name
On stock
Price
Sprouts - Onion
972
261.51
Beer - True North Lager
38
191.32
Milk - Condensed
714
826.1
Brandy Cherry - Mcguinness
212
555.07
Olive - Spread Tapenade
509
506.45
Tea - Black Currant
848
750.65
Coconut Milk - Unsweetened
149
658.56
Nut - Walnut, Pieces
147
763.42
Apple - Northern Spy
950
81.35
Orange - Canned, Mandarin
200
939.59
Veal - Brisket, Provimi,bnls
226
353.49
Wine - Ice Wine
618
867.85
Table Cloth 54x72 White
770
703.86
Wine - Cotes Du Rhone Parallele
435
967.56
Wine - Duboeuf Beaujolais
105
247.42
Carrots - Jumbo
350
880.11
Wine - Touraine Azay - Le - Rideau
290
949.68
Schnappes Peppermint - Walker
69
879.93
Wine - Champagne Brut Veuve
201
496.79
Appetizer - Smoked Salmon / Dill
856
274.6

Implementation

Create custom column

{
	id: 'expand',
	title: '',
	width: '50px',
	pinned: {
		position: 'left'
	},
	visible: true,
	resizable: false,
	sortable: false,
	exportable: false,
	selectable: false,
	moveable: false
},

Render custom header (optional)

{#if column.id === 'expand'}
	<Datagrid.HeaderWithoutSpacing {column} title="" />
{:else}

Render toggler

{#if column.id === 'expand'}
	<Datagrid.CellWithoutSpacing {...props}>
		<Datagrid.ExpandRowToggler rowId={row.id} />
	</Datagrid.CellWithoutSpacing>
{/if}

Render expanded row content

{#if Datagrid.isRowExpanded(datagrid, row.id)}
	<Datagrid.Row
		class={`border-b ${Datagrid.STAY_IN_PLACE} ${Datagrid.HIDE_BEHIND_PARENT_ROW}`}
		{rowIndex}
	>
		Content
	</Datagrid.Row>
{/if}

Styling is optional (except border)

Datagrid.STAY_IN_PLACE - use this if you want header to be in fixed position if datagrid scrolls horizontally

${Datagrid.HIDE_BEHIND_PARENT_ROW} - adds proper z-index, add this if you want previou

Code

Column definitions

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

export const columns = [
	{
		id: 'expand',
		title: '',
		width: '50px',
		pinned: {
			position: 'left'
		},
		visible: true,
		resizable: false,
		sortable: false,
		exportable: false,
		selectable: false,
		moveable: false
	},
	{
		id: 'product.name',
		title: 'Name',
		grow: true,
		align: 'start'
	},
	{
		id: 'quantity',
		title: 'On stock',
		align: 'center',
		grow: true
	},
	{
		id: 'price',
		title: 'Price',
		align: 'end',
		grow: true
	}
] 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';

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

<Datagrid.Datagrid>
	{#snippet head()}
		{#each datagrid.columns as column (column.id)}
			{#if column.id === 'expand'}
				<Datagrid.HeaderWithoutSpacing {column} title="" />
			{:else}
				<Datagrid.Header {column} />
			{/if}
		{/each}
	{/snippet}
	{#snippet body()}
		{#each datagrid.data as row, rowIndex}
			<Datagrid.Row {rowIndex}>
				{#each datagrid.columns as column, columnIndex}
					{@const props = { row, rowIndex, column, columnIndex }}
					{#if column.id === 'expand'}
						<Datagrid.CellWithoutSpacing {...props}>
							<Datagrid.ExpandRowToggler rowId={row.id} />
						</Datagrid.CellWithoutSpacing>
					{:else if column.id === 'product.name'}
						<Datagrid.Cell
							{...props}
							class={{ data: 'overflow-hidden text-ellipsis text-nowrap' }}
						/>
					{:else}
						<Datagrid.Cell {...props} />
					{/if}
				{/each}
			</Datagrid.Row>
			{#if Datagrid.isRowExpanded(datagrid, row.id)}
				<Datagrid.Row
					class={`border-b ${Datagrid.STAY_IN_PLACE} ${Datagrid.HIDE_BEHIND_PARENT_ROW}`}
					{rowIndex}
				>
					<div class="p-2 pl-3">
						Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum deserunt tenetur debitis
						praesentium aliquam error quibusdam explicabo nam voluptates, dignissimos minima quasi
						aliquid. Repellat, voluptatibus. Natus cumque temporibus nostrum quos. Assumenda
						laboriosam nostrum laborum impedit dolorem consectetur praesentium doloribus iusto
						accusamus recusandae! Sint, natus dolorem perferendis nesciunt similique nihil optio
						repellat adipisci ad expedita numquam quaerat incidunt cum consectetur praesentium.
						Pariatur tempore delectus sunt necessitatibus at voluptatum beatae molestias ratione
						modi nostrum a neque dolor illo magnam vero, natus dolorem, corporis eum aspernatur
						quaerat? Quibusdam ab velit neque rerum excepturi.
					</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! ❤️