Column ordering
Published at Oct 15, 2024
Sorting columns is an important feature when working with data. The Datagrid supports sorting by multiple columns.
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
moveable
specifies whether a column can be reordered.
{
moveable: true | false
}
Code
Column definitions
import type { BaseColumn } from "$lib/datagrid/types";
import type { InventoryDataRow } from "$lib/data/inventory";
export const columns = [
{
id: 'product.name',
title: 'Name',
grow: true,
},
{
id: 'quantity',
title: 'On stock',
align: 'end',
grow: true,
},
{
id: 'price',
title: 'Price',
moveable: false,
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';
import { topBarOptions } from './top-bar-options';
let datagrid = setContext(
`datagrid`,
new TzezarDatagrid({
data,
columns,
options: {
pagination: { display: false },
topbar: topBarOptions
}
})
);
</script>
<Datagrid.Datagrid>
{#snippet head()}
{#each datagrid.columns as column (column.id)}
<Datagrid.Header {column} />
{/each}
{/snippet}
{#snippet body()}
{#each datagrid.state.processedData as row, rowIndex}
<Datagrid.Row {rowIndex}>
{#each datagrid.columns as column, columnIndex}
{@const props = { row, rowIndex, column, columnIndex }}
{#if column.id === 'product.name'}
<Datagrid.Cell
{...props}
class={{ data: 'overflow-hidden text-ellipsis text-nowrap' }}
/>
{:else}
<Datagrid.Cell {...props} />
{/if}
{/each}
</Datagrid.Row>
{/each}
{/snippet}
</Datagrid.Datagrid>
Topbar options
import type { TopBarOptions } from "$lib/datagrid/tzezar-datagrid.svelte";
export const topBarOptions = {
display: true,
displayCopyDataMenu: false,
displayExportDataMenu: false,
displayFullscreenToggle: false,
displayHeadFilterToggle: false,
settingsMenu: {
display: true,
displayFreezingMenu: false,
displayReorderingMenu: true,
displayResizingMenu: false,
displaySortingMenu: false,
displayVisibilityMenu: false,
adjustmentMenu: {
display: false
}
}
} satisfies TopBarOptions