menumenu_openMDC

Data Table

descriptionREADMEwidgetsDemo

Material Design 3 Data Table component with column sorting, single/multiple row selection, contextual selection bar, loading progress indicator, sticky headers, and integrated <mdc-pagination variant="compact"> footer.

Usage

<mdc-data-table title="Nutrition" selection-mode="multiple" paginated page-size="5"></mdc-data-table>
const table = document.querySelector('mdc-data-table')!
table.columns = [
    { key: 'name', label: 'Dessert', sortable: true },
    { key: 'calories', label: 'Calories', numeric: true, sortable: true },
    { key: 'fat', label: 'Fat (g)', numeric: true, sortable: true },
]
table.rows = [
    { id: '1', name: 'Frozen yogurt', calories: 159, fat: 6.0 },
    { id: '2', name: 'Ice cream sandwich', calories: 237, fat: 9.0 },
]

Slots

Slot Description
header Leading toolbar content when no rows are selected.
header-actions Trailing toolbar actions when no rows are selected.
selection-actions Contextual actions shown in the selection bar when rows are selected.
empty Custom empty-state content when rows is empty.
footer Custom footer content placed before the pagination control.

Expandable

Code
<mdc-data-table id="dt-expandable" expandable title="Expandable Detail Rows"></mdc-data-table>
<script type="module">
    import { html } from 'lit'

    const root = document.currentScript?.getRootNode?.() ?? document
    const el = root.getElementById?.('dt-expandable') ?? document.getElementById('dt-expandable')
    if (el) {
        el.columns = [
            { key: 'name', label: 'Dessert', sortable: true },
            { key: 'calories', label: 'Calories', numeric: true, sortable: true },
            { key: 'origin', label: 'Origin' },
        ]
        el.rows = [
            { id: '1', name: 'Frozen yogurt', calories: 159, origin: 'USA', notes: 'Low-fat probiotic dairy dessert.' },
            { id: '2', name: 'Eclair', calories: 262, origin: 'France', notes: 'Oblong choux pastry filled with cream and topped with icing.' },
            { id: '3', name: 'Tiramisu', calories: 310, origin: 'Italy', notes: 'Coffee-flavoured dessert with mascarpone and ladyfingers.' },
        ]
        el.renderExpandedRow = (row) => html`
            <div>
                <strong>${row.name}</strong> — ${row.notes}
            </div>
        `
    }
</script>

Filter Text

Code
<mdc-data-table id="dt-filter" title="Filterable Desserts" paginated page-size="5">
    <mdc-text-field
        id="dt-filter-input"
        slot="header-actions"
        variant="outlined"
        label="Search"
        placeholder="Type to filter..."
    ></mdc-text-field>
</mdc-data-table>
<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const byId = (id) => root.getElementById?.(id) ?? document.getElementById(id)
    const table = byId('dt-filter')
    const input = byId('dt-filter-input')
    if (table) {
        table.columns = [
            { key: 'name', label: 'Dessert', sortable: true },
            { key: 'category', label: 'Category', sortable: true },
            { key: 'calories', label: 'Calories', numeric: true, sortable: true },
        ]
        table.rows = [
            { id: '1', name: 'Frozen yogurt', category: 'Dairy', calories: 159 },
            { id: '2', name: 'Ice cream sandwich', category: 'Dairy', calories: 237 },
            { id: '3', name: 'Eclair', category: 'Pastry', calories: 262 },
            { id: '4', name: 'Cupcake', category: 'Bakery', calories: 305 },
            { id: '5', name: 'Gingerbread', category: 'Bakery', calories: 356 },
            { id: '6', name: 'Macaron', category: 'Pastry', calories: 210 },
        ]
        input?.addEventListener('input', (e) => {
            table.filterText = e.target.value ?? ''
        })
    }
</script>

Interactive

Code
<mdc-data-table
    id="dt-interactive"
    title="Nutrition"
    selection-mode="multiple"
    row-selectable
    paginated
    page-size="5"
    sticky-header
>
    <mdc-icon-button slot="header-actions" aria-label="Filter list">
        <mdc-icon name="filter_list"></mdc-icon>
    </mdc-icon-button>
    <mdc-icon-button slot="selection-actions" aria-label="Delete selected">
        <mdc-icon name="delete"></mdc-icon>
    </mdc-icon-button>
</mdc-data-table>
<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const el = root.getElementById?.('dt-interactive') ?? document.getElementById('dt-interactive')
    if (el) {
        el.columns = [
            { key: 'name', label: 'Dessert (100g serving)', sortable: true },
            { key: 'calories', label: 'Calories', numeric: true, sortable: true },
            { key: 'fat', label: 'Fat (g)', numeric: true, sortable: true, format: (v) => Number(v).toFixed(1) },
            { key: 'carbs', label: 'Carbs (g)', numeric: true, sortable: true },
            { key: 'protein', label: 'Protein (g)', numeric: true, sortable: true, format: (v) => Number(v).toFixed(1) },
        ]
        el.rows = [
            { id: '1', name: 'Frozen yogurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 },
            { id: '2', name: 'Ice cream sandwich', calories: 237, fat: 9.0, carbs: 37, protein: 4.3 },
            { id: '3', name: 'Eclair', calories: 262, fat: 16.0, carbs: 24, protein: 6.0 },
            { id: '4', name: 'Cupcake', calories: 305, fat: 3.7, carbs: 67, protein: 4.3 },
            { id: '5', name: 'Gingerbread', calories: 356, fat: 16.0, carbs: 49, protein: 3.9 },
            { id: '6', name: 'Jelly bean', calories: 375, fat: 0.0, carbs: 94, protein: 0.0 },
            { id: '7', name: 'Lollipop', calories: 392, fat: 0.2, carbs: 98, protein: 0.0 },
            { id: '8', name: 'Honeycomb', calories: 408, fat: 3.2, carbs: 87, protein: 6.5 },
        ]
    }
</script>

Loading

Code
<mdc-data-table id="dt-loading" loading title="Loading State"></mdc-data-table>
<mdc-data-table id="dt-empty" title="Empty State" empty-text="No matching desserts found."></mdc-data-table>
<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const byId = (id) => root.getElementById?.(id) ?? document.getElementById(id)
    const columns = [
        { key: 'name', label: 'Dessert' },
        { key: 'calories', label: 'Calories', numeric: true },
    ]
    const loadingEl = byId('dt-loading')
    if (loadingEl) {
        loadingEl.columns = columns
        loadingEl.rows = [
            { id: '1', name: 'Frozen yogurt', calories: 159 },
            { id: '2', name: 'Eclair', calories: 262 },
        ]
    }
    const emptyEl = byId('dt-empty')
    if (emptyEl) {
        emptyEl.columns = columns
        emptyEl.rows = []
    }
</script>

Paginated

Code
<mdc-data-table
    id="dt-paginated"
    title="Paginated Desserts"
    paginated
    page="1"
    page-size="3"
    page-size-options="3,5,10"
></mdc-data-table>
<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const el = root.getElementById?.('dt-paginated') ?? document.getElementById('dt-paginated')
    if (el) {
        el.columns = [
            { key: 'name', label: 'Dessert', sortable: true },
            { key: 'calories', label: 'Calories', numeric: true, sortable: true },
            { key: 'carbs', label: 'Carbs (g)', numeric: true },
        ]
        el.rows = [
            { id: '1', name: 'Frozen yogurt', calories: 159, carbs: 24 },
            { id: '2', name: 'Ice cream sandwich', calories: 237, carbs: 37 },
            { id: '3', name: 'Eclair', calories: 262, carbs: 24 },
            { id: '4', name: 'Cupcake', calories: 305, carbs: 67 },
            { id: '5', name: 'Gingerbread', calories: 356, carbs: 49 },
            { id: '6', name: 'Jelly bean', calories: 375, carbs: 94 },
            { id: '7', name: 'Lollipop', calories: 392, carbs: 98 },
        ]
    }
</script>

Selection Mode

Code
<mdc-data-table id="dt-single" selection-mode="single" title="Single Selection"></mdc-data-table>
<mdc-data-table id="dt-multiple" selection-mode="multiple" row-selectable title="Multiple Selection">
    <mdc-icon-button slot="selection-actions" aria-label="Archive">
        <mdc-icon name="archive"></mdc-icon>
    </mdc-icon-button>
    <mdc-icon-button slot="selection-actions" aria-label="Delete">
        <mdc-icon name="delete"></mdc-icon>
    </mdc-icon-button>
</mdc-data-table>
<script type="module">
    const columns = [
        { key: 'name', label: 'Dessert' },
        { key: 'calories', label: 'Calories', numeric: true },
        { key: 'fat', label: 'Fat (g)', numeric: true },
    ]
    const rows = [
        { id: '1', name: 'Frozen yogurt', calories: 159, fat: 6.0 },
        { id: '2', name: 'Ice cream sandwich', calories: 237, fat: 9.0 },
        { id: '3', name: 'Eclair', calories: 262, fat: 16.0 },
    ]
    const root = document.currentScript?.getRootNode?.() ?? document
    for (const id of ['dt-single', 'dt-multiple']) {
        const el = root.getElementById?.(id) ?? document.getElementById(id)
        if (el) {
            el.columns = columns
            el.rows = rows
        }
    }
</script>

Size

Code
<mdc-data-table id="dt-small" size="small" title="Small (Compact)"></mdc-data-table>
<mdc-data-table id="dt-medium" size="medium" title="Medium (Standard)"></mdc-data-table>
<mdc-data-table id="dt-large" size="large" title="Large (Comfortable)"></mdc-data-table>
<script type="module">
    const columns = [
        { key: 'name', label: 'Dessert' },
        { key: 'calories', label: 'Calories', numeric: true },
    ]
    const rows = [
        { id: '1', name: 'Frozen yogurt', calories: 159 },
        { id: '2', name: 'Eclair', calories: 262 },
    ]
    const root = document.currentScript?.getRootNode?.() ?? document
    for (const id of ['dt-small', 'dt-medium', 'dt-large']) {
        const el = root.getElementById?.(id) ?? document.getElementById(id)
        if (el) {
            el.columns = columns
            el.rows = rows
        }
    }
</script>

Sticky Header

Code
<mdc-data-table
    id="dt-sticky"
    title="Sticky Header & Pinned Columns"
    sticky-header
    max-height="240px"
></mdc-data-table>
<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const el = root.getElementById?.('dt-sticky') ?? document.getElementById('dt-sticky')
    if (el) {
        el.columns = [
            { key: 'name', label: 'Dessert', sticky: 'start', minWidth: '180px' },
            { key: 'calories', label: 'Calories', numeric: true, minWidth: '120px' },
            { key: 'fat', label: 'Fat (g)', numeric: true, minWidth: '120px' },
            { key: 'carbs', label: 'Carbs (g)', numeric: true, minWidth: '120px' },
            { key: 'protein', label: 'Protein (g)', numeric: true, minWidth: '120px' },
            { key: 'sodium', label: 'Sodium (mg)', numeric: true, sticky: 'end', minWidth: '120px' },
        ]
        el.rows = Array.from({ length: 12 }, (_, i) => ({
            id: String(i + 1),
            name: `Dessert Item #${i + 1}`,
            calories: 150 + i * 25,
            fat: (4 + i * 1.2).toFixed(1),
            carbs: 20 + i * 5,
            protein: (3 + i * 0.6).toFixed(1),
            sodium: 60 + i * 15,
        }))
    }
</script>

Striped

Code
<mdc-data-table id="dt-striped" striped title="Striped Rows"></mdc-data-table>
<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const el = root.getElementById?.('dt-striped') ?? document.getElementById('dt-striped')
    if (el) {
        el.columns = [
            { key: 'name', label: 'Dessert' },
            { key: 'calories', label: 'Calories', numeric: true },
            { key: 'fat', label: 'Fat (g)', numeric: true },
        ]
        el.rows = [
            { id: '1', name: 'Frozen yogurt', calories: 159, fat: 6.0 },
            { id: '2', name: 'Ice cream sandwich', calories: 237, fat: 9.0 },
            { id: '3', name: 'Eclair', calories: 262, fat: 16.0 },
            { id: '4', name: 'Cupcake', calories: 305, fat: 3.7 },
        ]
    }
</script>

Variant

Code
<mdc-data-table id="dt-outlined" variant="outlined" title="Outlined"></mdc-data-table>
<mdc-data-table id="dt-filled" variant="filled" title="Filled"></mdc-data-table>
<mdc-data-table id="dt-elevated" variant="elevated" title="Elevated"></mdc-data-table>
<script type="module">
    const columns = [
        { key: 'name', label: 'Dessert', sortable: true },
        { key: 'calories', label: 'Calories', numeric: true, sortable: true },
        { key: 'fat', label: 'Fat (g)', numeric: true },
    ]
    const rows = [
        { id: '1', name: 'Frozen yogurt', calories: 159, fat: 6.0 },
        { id: '2', name: 'Ice cream sandwich', calories: 237, fat: 9.0 },
    ]
    const root = document.currentScript?.getRootNode?.() ?? document
    for (const id of ['dt-outlined', 'dt-filled', 'dt-elevated']) {
        const el = root.getElementById?.(id) ?? document.getElementById(id)
        if (el) {
            el.columns = columns
            el.rows = rows
        }
    }
</script>