Used with a component · DataTable
useDataTableContext
Reaches the nearest <DataTable>'s sort and selection state from a component rendered inside it, such as a custom cell. A slot function can't call a composable, so put the call in a small component and render that from a <Column>'s #cell or #header slot. <DataTable> provides the context with provideDataTableContext(), and <Column> registers itself through it. useDataTableContext() throws outside a DataTable.
Install
import { useDataTableContext, provideDataTableContext } from 'vael-ui'Examples
<!-- StarCell.vue -->
<script setup lang="ts">
import { useDataTableContext } from 'vael-ui'
interface Person { id: number; name: string }
const props = defineProps<{ row: Person }>()
const table = useDataTableContext<Person>()
</script>
<template>
<button :aria-pressed="table.isSelected(props.row)" @click="table.toggleSelect(props.row)">
{{ table.isSelected(props.row) ? 'Starred' : 'Star' }}
</button>
</template>
<!-- In the table -->
<DataTable :data="people" row-key="id" @update:selection="onSelection">
<Column field="name" label="Name" />
<Column field="id" label="">
<template #cell="{ row }"><StarCell :row="row" /></template>
</Column>
</DataTable>