Column hiding

Published at Oct 15, 2024

Sorting columns is an important feature when working with data. The Datagrid supports sorting by multiple columns.

Location
Apt 1819
Suite 11
Apt 767
Suite 47
Room 1868
Room 1846
PO Box 75154
PO Box 39137
Room 1284
18th Floor
Suite 20
10th Floor
Suite 25
Suite 73
Apt 572
Room 1965
Apt 110
Room 139
Room 1571
Suite 97

Implementation

Two properties control the visibility of columns:

hideable – specifies whether a column can be hidden.

visible – represents the current visibility state of the column.

{
    hideable: 'true' | 'false',
    visible: 'true' | 'false',
}

Code

Column definitions

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

export const columns = [
    {
        id: 'barcode',
        title: 'Barcode',
        visible: false,
        grow: true,
    },

    {
        id: 'location',
        title: 'Location',
        hideable: false,
        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: data.slice(0, 20),
			columns,
			options: {
				paginate: false,
				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.data as row, rowIndex}
			<Datagrid.Row {rowIndex}>
				{#each datagrid.columns as column, columnIndex}
					<Datagrid.Cell {column} {row} {columnIndex} {rowIndex} />
				{/each}
			</Datagrid.Row>
		{/each}
	{/snippet}
</Datagrid.Datagrid>

Topbar options

import type { TopBarOptions } from "$lib/datagrid/tzezar-datagrid.svelte";

export const topBarOptions = {
    display: true,
    settingsMenu: {
        display: true,
        displayFreezingMenu: false,
        displayReorderingMenu: false,
        displayResizingMenu: false,
        displaySortingMenu: false,
        displayVisibilityMenu: true,
        adjustmentMenu: {
            display: false,
            displaySpacingMenu: false,
            displayTextSizeMenu: false
        }
    }
} satisfies TopBarOptions
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! ❤️