Table with vue template syntax #6071
Replies: 1 comment
|
You do not actually need an external wrapper or render functions ( While the documentation examples heavily feature React/JSX style render functions, the official Here is how you can use 100% Vue template syntax today with the official package, plus ecosystem solutions: 1. The Scoped Slots Pattern (Best Template-First Experience)The cleanest pattern to avoid render functions entirely is to build a reusable table component that exposes dynamic scoped slots for each column: <!-- DataTable.vue -->
<script setup lang="ts" generic="TData">
import { FlexRender, type Table } from '@tanstack/vue-table'
defineProps<{
table: Table<TData>
}>()
</script>
<template>
<table>
<thead>
<tr v-for="headerGroup in table.getHeaderGroups()" :key="headerGroup.id">
<th v-for="header in headerGroup.headers" :key="header.id">
<slot :name="`header-${header.id}`" :header="header">
<FlexRender
v-if="!header.isPlaceholder"
:render="header.column.columnDef.header"
:props="header.getContext()"
/>
</slot>
</th>
</tr>
</thead>
<tbody>
<tr v-for="row in table.getRowModel().rows" :key="row.id">
<td v-for="cell in row.getVisibleCells()" :key="cell.id">
<!-- Dynamic scoped slot based on column ID -->
<slot :name="`cell-${cell.column.id}`" :cell="cell" :row="row" :value="cell.getValue()">
<FlexRender
:render="cell.column.columnDef.cell"
:props="cell.getContext()"
/>
</slot>
</td>
</tr>
</tbody>
</table>
</template>How you use it in your views:Now you can define pure accessor columns without any render functions, and customize any column using regular Vue template syntax: <!-- UsersView.vue -->
<template>
<DataTable :table="table">
<!-- Custom template for status column -->
<template #cell-status="{ value }">
<span :class="value === 'active' ? 'text-green-500 font-bold' : 'text-gray-400'">
● {{ value.toUpperCase() }}
</span>
</template>
<!-- Custom template for actions column -->
<template #cell-actions="{ row }">
<button @click="handleEdit(row.original)">Edit</button>
<button @click="handleDelete(row.original.id)">Delete</button>
</template>
</DataTable>
</template>2. Passing Vue Single File Components (SFCs) directly to
|
Uh oh!
There was an error while loading. Please reload this page.
Im currently searching for something that i can use with the normal vue template syntax instead of using render functions.
I found this recently:
https://github.com/dacsang97/tanstack-table-vue
But does anybody know something similar? Maybe even another project?
Is there any plan to support something similar with the official tanstack table?
All reactions