MultiSelect Filter as/in column header #2106
-
|
Hi all, my current logic to filter the table with the MultiSelect component, that are outside the table, is working fine. But I would like to put the dropdown filter into the column header itself. Like in Excel for example. I found an example (don't ask me how), which seems to be for react: Can we do something similar in the svelte version? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Try using the DataTable <script>
import { DataTable, MultiSelect } from 'carbon-components-svelte';
const headers = [
{ key: 'name', value: 'Name' },
{ key: 'status', value: 'Status', columnMenu: true },
];
const rows = [
{ id: 1, name: 'Item 1', status: 'Active' },
{ id: 2, name: 'Item 2', status: 'Inactive' },
];
let selectedStatuses = [];
const statusOptions = [
{ id: 'active', text: 'Active' },
{ id: 'inactive', text: 'Inactive' },
];
</script>
<DataTable {headers} {rows}>
<svelte:fragment slot="cellHeader" let:header>
{#if header.columnMenu && header.key === 'status'}
<MultiSelect
labelText="Filter"
hideLabel
items={statusOptions}
bind:selectedIds={selectedStatuses}
size="sm"
/>
{:else}
{header.value}
{/if}
</svelte:fragment>
</DataTable> |
Beta Was this translation helpful? Give feedback.
Try using the DataTable
cellHeaderslot withcolumnMenu: trueon the header to render a MultiSelect (or Dropdown) filter directly in the column header. ThecellHeaderslot receives{ header }and lets you customize the header content per column. Subsequently, you can filter therowsin your script based onselectedStatusesbefore passing to DataTable.