Basic Usage
Here's a basic example of how to use the Table component:
<script lang="ts">
import { Table } from '@cailabs/components.data';
let users = $state([
{ id: 1, name: 'Alice', email: 'alice@example.com', role: 'Admin', status: 'Active' },
{ id: 2, name: 'Bob', email: 'bob@example.com', role: 'User', status: 'Inactive' },
{ id: 3, name: 'Charlie', email: 'charlie@example.com', role: 'User', status: 'Active' }
]);
const columns = [
{ header: 'ID', accessor: 'id' },
{ header: 'Name', accessor: 'name' },
{ header: 'Email', accessor: 'email' },
{ header: 'Role', accessor: 'role' },
{ header: 'Status', accessor: 'status' }
];
const actions = [
{ name: 'Edit', icon: 'ri:edit-2-fill' },
{ name: 'Deactivate', icon: 'mdi:account-cancel' }
];
function handleAction(event: any) {
const { name, row } = event;
console.log(`${name} action for:`, row);
if (name === 'Deactivate') {
users = users.map((u) => (u.id === row.id ? { ...u, status: 'Inactive' } : u));
}
}
function handleRowClick(event: any) {
console.log('Row clicked:', event);
}
</script>
<Table
data={users}
tableColumns={columns}
showActions
actionLists={actions}
rowClickable
headerColor="blue"
onActionClicked={handleAction}
onRowClicked={handleRowClick}
/>
Props
Full public API from TableProps / related types.
Data & Columns
| Prop | Type | Default | Description |
data | any[] | [] | Row data |
tableColumns | TableColumn[] | — | Column definitions (required) |
hiddenColumns | string[] | — | Column ids hidden initially |
allColumns | any | — | Full column catalog for visibility menus |
Appearance
| Prop | Type | Default | Description |
headerColor | 'green' | 'orange' | 'white' | 'purple' | 'sky' | 'blue' | 'red' | 'pink' | — | Header background |
headerTextColor | 'black' | 'white' | 'gray' | — | Header text color |
height | number | — | Table container height |
bgWhite | boolean | — | White body background |
hideWhiteSpace | boolean | — | Prevent cell text wrap |
showMiniWidth | boolean | — | Compact column widths |
showTableBottomPadding | boolean | — | Extra padding under the table |
tableBottomContent | Snippet | — | Content rendered below the table |
Features
| Prop | Type | Default | Description |
showActions | boolean | — | Actions column |
showCheckBox | boolean | — | Row selection checkboxes |
showViewDetails | boolean | — | “View Details” in action menu |
showEdit | boolean | — | “Edit” in action menu |
showDelete | boolean | — | “Delete” in action menu |
showIndex | boolean | — | Index column |
rowClickable | boolean | — | Rows fire onRowClicked |
Sorting
| Prop | Type | Default | Description |
sortedColumns | TableSortInfo[] | — | Bindable active sort |
initialSortKeys | TableSortInfo[] | — | Initial sort configuration |
Selection
| Prop | Type | Default | Description |
selectedRows | any[] | — | Bindable selected rows |
selectAllChecked | boolean | — | Bindable select-all state |
Actions
| Prop | Type | Default | Description |
actionLists | TableActionList[] | — | Custom action menu entries |
Events
| Prop | Type | Description |
handleCheckbox | (val: any[]) => void | Selection changed |
onView | (val: any) => void | View Details clicked |
onEdit | (val: any) => void | Edit clicked |
onDelete | (val: any) => void | Delete clicked |
onActionClicked | ({ name, row }) => void | Custom action clicked |
onRowClicked | (val: any) => void | Row clicked |
Types
TableColumn
export interface TableColumn {
header: string;
id?: string;
plugins?: any;
accessor?: string | string[] | ((row: any) => unknown);
cell?: (ctx: { value: unknown; row: any }) => unknown;
renderComponent?: Component<any>;
renderProps?: (ctx: {
value: unknown;
row: any;
column: TableColumn;
}) => Record<string, unknown>;
}
TableActionList
export interface TableActionList {
name: string;
icon?: string;
otherClasses?: string;
visible?: (row: any) => boolean;
onClick?: (row: any) => void;
}
TableSortInfo
export interface TableSortInfo {
id: string;
order: 'asc' | 'desc';
}
Examples
Basic Table
<script>
import { Table } from '@cailabs/components.data';
const data = [
{ id: 1, name: 'Item 1', price: 10.99 },
{ id: 2, name: 'Item 2', price: 20.50 }
];
const columns = [
{ header: 'ID', accessor: 'id' },
{ header: 'Name', accessor: 'name' },
{ header: 'Price', accessor: 'price' }
];
</script>
<Table data={data} tableColumns={columns} />
Function Accessor Column
<script lang="ts">
import { Table } from '@cailabs/components.data';
const rows = [
{ id: 1, specialityName: 'Cardiology' },
{ id: 2, specialityName: null }
];
const columns = [
{
header: 'Specialty Name',
accessor: (row: any) => row?.specialityName ?? '-',
plugins: { sort: { disable: true } }
}
];
</script>
<Table data={rows} tableColumns={columns} />
Render Component Per Column (JSX-like)
<script lang="ts">
import { Table } from '@cailabs/components.data';
import SpecialtyCell from './SpecialtyCell.svelte';
const rows = [
{ id: 1, specialityName: 'Cardiology' },
{ id: 2, specialityName: null }
];
const columns = [
{
id: 'specialty',
header: 'Specialty Name',
accessor: (row: any) => row?.specialityName ?? '-',
renderComponent: SpecialtyCell,
renderProps: ({ row }) => ({ muted: !row?.specialityName }),
plugins: { sort: { disable: true } }
}
];
</script>
<Table data={rows} tableColumns={columns} />
<script lang="ts">
let { value, row, muted = false } = $props();
</script>
<div class="flex items-center gap-1">
<span class:opacity-60={muted}>{value}</span>
{#if !row?.specialityName}
<span class="text-xs text-gray-400">(not set)</span>
{/if}
</div>
Table with Custom Actions
<script>
import { Table } from '@cailabs/components.data';
let products = $state([...]);
const columns = [...];
const actions = [
{ name: 'Edit', icon: 'ri:edit-2-fill' },
{ name: 'Archive', icon: 'mdi:archive' }
];
function handleAction({ name, row }) {
if (name === 'Edit') {
} else if (name === 'Archive') {
}
}
</script>
<Table
data={products}
tableColumns={columns}
showActions
actionLists={actions}
onActionClicked={handleAction}
/>
Advanced Features
Row Selection
Enable row selection with checkboxes:
<script>
let selectedRows = $state([]);
let selectAllChecked = $state(false);
function handleSelection(rows) {
console.log(rows);
}
</script>
<Table
data={data}
tableColumns={columns}
showCheckBox
selectedRows={selectedRows}
selectAllChecked={selectAllChecked}
handleCheckbox={handleSelection}
/>
Conditional Actions
Show actions conditionally based on row data:
<script>
const actions = [
{
name: 'Activate',
icon: 'mdi:account-check',
visible: (row) => row.status === 'Inactive'
},
{
name: 'Deactivate',
icon: 'mdi:account-cancel',
visible: (row) => row.status === 'Active'
}
];
</script>
<Table
data={users}
tableColumns={columns}
showActions
actionLists={actions}
/>