Column Definition Reference

Column definitions configure how each column displays and behaves.

import type { ColumnDef } from 'svelte-datagrid';

const columns: ColumnDef<MyRowType>[] = [
  { key: 'id', header: 'ID', width: 80 },
  { key: 'name', header: 'Name', width: 200 }
];

Type Definition

interface ColumnDef<TData, TValue = unknown> {
  // Required
  key: keyof TData | string;
  header: string;

  // Sizing
  width?: number;
  minWidth?: number;
  maxWidth?: number;

  // Display
  align?: 'left' | 'center' | 'right';
  headerClass?: string;
  cellClass?: string | ((row: TData, value: TValue) => string);

  // Features
  sortable?: boolean;
  filterable?: boolean;
  resizable?: boolean;
  reorderable?: boolean;
  visible?: boolean;
  pinned?: 'left' | 'right' | false;
  editable?: boolean;

  // Filtering
  filterType?: 'text' | 'number' | 'date' | 'boolean' | 'select';
  filterOptions?: string[];

  // Custom rendering
  render?: (value: TValue, row: TData, index: number) => unknown;
}

Properties

Required Properties

PropertyTypeDescription
keykeyof TData \| stringProperty name in row data, or dot-notation path (e.g., 'user.name')
headerstringText displayed in column header

Sizing Properties

PropertyTypeDefaultDescription
widthnumber150Initial column width in pixels
minWidthnumber50Minimum width when resizing
maxWidthnumberundefinedMaximum width when resizing (no limit if undefined)

Display Properties

PropertyTypeDefaultDescription
align'left' \| 'center' \| 'right''left'Text alignment
headerClassstringundefinedCSS class for header cell
cellClassstring \| ((row, value) => string)undefinedCSS class for data cells

Feature Properties

PropertyTypeDefaultDescription
sortablebooleantrueEnable sorting for this column
filterablebooleantrueEnable filtering for this column
resizablebooleantrueEnable resizing for this column
reorderablebooleantrueEnable drag-and-drop reordering
visiblebooleantrueColumn visibility
pinned'left' \| 'right' \| falsefalsePin column to left or right edge
editablebooleantrueEnable editing (requires grid editable prop)

Filter Properties

PropertyTypeDefaultDescription
filterType'text' \| 'number' \| 'date' \| 'boolean' \| 'select''text'Type of filter input
filterOptionsstring[]undefinedOptions for select filter type

Render Properties

PropertyTypeDescription
render(value, row, index) => unknownCustom render function

Examples

Basic columns

const columns = [
  { key: 'id', header: 'ID', width: 60, sortable: false },
  { key: 'name', header: 'Name', width: 200 },
  { key: 'email', header: 'Email', width: 250 }
];

Numeric column

{
  key: 'price',
  header: 'Price',
  width: 100,
  align: 'right',
  filterType: 'number',
  render: (value) => `$${value.toFixed(2)}`
}

Boolean column

{
  key: 'active',
  header: 'Active',
  width: 80,
  align: 'center',
  filterType: 'boolean',
  render: (value) => value ? '✓' : '✗'
}

Date column

{
  key: 'createdAt',
  header: 'Created',
  width: 120,
  filterType: 'date',
  render: (value) => new Date(value).toLocaleDateString()
}

Select filter column

{
  key: 'status',
  header: 'Status',
  width: 120,
  filterType: 'select',
  filterOptions: ['pending', 'active', 'completed', 'cancelled']
}

Conditional styling

{
  key: 'amount',
  header: 'Amount',
  width: 100,
  align: 'right',
  cellClass: (row, value) => {
    if (value > 1000) return 'amount-high';
    if (value < 0) return 'amount-negative';
    return '';
  }
}

Nested property access

const data = [
  { id: 1, user: { name: 'Alice', email: 'alice@example.com' } }
];

const columns = [
  { key: 'user.name', header: 'Name', width: 150 },
  { key: 'user.email', header: 'Email', width: 200 }
];

Non-resizable fixed column

{
  key: 'id',
  header: 'ID',
  width: 60,
  minWidth: 60,
  maxWidth: 60,
  resizable: false,
  sortable: false,
  filterable: false
}

Editable columns with read-only ID

const columns = [
  { key: 'id', header: 'ID', width: 60, editable: false },
  { key: 'name', header: 'Name', width: 200 },  // editable by default
  { key: 'email', header: 'Email', width: 250 } // editable by default
];

Filter Types

TypeInputMatching
textText inputCase-insensitive contains
numberNumber inputExact match
dateDate pickerDate comparison
booleanYes/No/All dropdownBoolean match
selectDropdown with optionsExact match

See also