import Alpine from 'alpinejs'
import { createTableHook, tableFeatures } from '@tanstack/alpine-table'
import './index.css'
// This example uses the new `createTableHook` method to create a re-usable table hook factory instead of independently using the standalone `createTable` function and `createColumnHelper` method. You can choose to use either way.
// 1. Define what the shape of your data will be for each row
type Person = {
firstName: string
lastName: string
age: number
visits: number
status: string
progress: number
}
// 2. Create some dummy data with a stable reference (this could be an API response stored in `Alpine.reactive` or similar)
const defaultData: Array<Person> = [
{
firstName: 'tanner',
lastName: 'linsley',
age: 24,
visits: 100,
status: 'In Relationship',
progress: 50,
},
{
firstName: 'tandy',
lastName: 'miller',
age: 40,
visits: 40,
status: 'Single',
progress: 80,
},
{
firstName: 'joe',
lastName: 'dirte',
age: 45,
visits: 20,
status: 'Complicated',
progress: 10,
},
{
firstName: 'kevin',
lastName: 'vandy',
age: 28,
visits: 100,
status: 'Single',
progress: 70,
},
]
// 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features. These are now shared across every table created from this hook.
const { createAppTable, createAppColumnHelper } = createTableHook({
features: tableFeatures({}),
debugTable: true,
})
// 4. Create a helper object to help define our columns (pre-bound to the hook's features)
const columnHelper = createAppColumnHelper<Person>()
// 5. Define the columns for your table with a stable reference (defined statically outside of the Alpine component). Renderers return HTML strings that are rendered with `x-html` via `table.FlexRender`.
const columns = columnHelper.columns([
// accessorKey method (most common for simple use-cases)
columnHelper.accessor('firstName', {
cell: (info) => info.getValue(),
footer: (info) => info.column.id,
}),
// accessorFn used (alternative) along with a custom id
columnHelper.accessor((row) => row.lastName, {
id: 'lastName',
cell: (info) => `<i>${info.getValue()}</i>`,
header: () => '<span>Last Name</span>',
footer: (info) => info.column.id,
}),
// accessorFn used to transform the data
columnHelper.accessor((row) => Number(row.age), {
id: 'age',
header: () => 'Age',
cell: (info) => info.renderValue(),
footer: (info) => info.column.id,
}),
columnHelper.accessor('visits', {
header: () => '<span>Visits</span>',
footer: (info) => info.column.id,
}),
columnHelper.accessor('status', {
header: 'Status',
footer: (info) => info.column.id,
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
footer: (info) => info.column.id,
}),
])
// 6. Register the Alpine component.
Alpine.data('table', () => {
// Store data with a stable reference
const local = Alpine.reactive({ data: [...defaultData] })
// 7. Create the table instance with the required columns and data. Features
// and row models were already defined in the `createTableHook` call above.
const table = createAppTable({
columns,
get data() {
return local.data
},
// add additional table options here or in the createTableHook call above
})
// `table.FlexRender` is attached by the hook, so the template can render
// headers/cells/footers with `x-html="table.FlexRender({ ... })"`.
return { table }
})
window.Alpine = Alpine
Alpine.start()
import Alpine from 'alpinejs'
import { createTableHook, tableFeatures } from '@tanstack/alpine-table'
import './index.css'
// This example uses the new `createTableHook` method to create a re-usable table hook factory instead of independently using the standalone `createTable` function and `createColumnHelper` method. You can choose to use either way.
// 1. Define what the shape of your data will be for each row
type Person = {
firstName: string
lastName: string
age: number
visits: number
status: string
progress: number
}
// 2. Create some dummy data with a stable reference (this could be an API response stored in `Alpine.reactive` or similar)
const defaultData: Array<Person> = [
{
firstName: 'tanner',
lastName: 'linsley',
age: 24,
visits: 100,
status: 'In Relationship',
progress: 50,
},
{
firstName: 'tandy',
lastName: 'miller',
age: 40,
visits: 40,
status: 'Single',
progress: 80,
},
{
firstName: 'joe',
lastName: 'dirte',
age: 45,
visits: 20,
status: 'Complicated',
progress: 10,
},
{
firstName: 'kevin',
lastName: 'vandy',
age: 28,
visits: 100,
status: 'Single',
progress: 70,
},
]
// 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features. These are now shared across every table created from this hook.
const { createAppTable, createAppColumnHelper } = createTableHook({
features: tableFeatures({}),
debugTable: true,
})
// 4. Create a helper object to help define our columns (pre-bound to the hook's features)
const columnHelper = createAppColumnHelper<Person>()
// 5. Define the columns for your table with a stable reference (defined statically outside of the Alpine component). Renderers return HTML strings that are rendered with `x-html` via `table.FlexRender`.
const columns = columnHelper.columns([
// accessorKey method (most common for simple use-cases)
columnHelper.accessor('firstName', {
cell: (info) => info.getValue(),
footer: (info) => info.column.id,
}),
// accessorFn used (alternative) along with a custom id
columnHelper.accessor((row) => row.lastName, {
id: 'lastName',
cell: (info) => `<i>${info.getValue()}</i>`,
header: () => '<span>Last Name</span>',
footer: (info) => info.column.id,
}),
// accessorFn used to transform the data
columnHelper.accessor((row) => Number(row.age), {
id: 'age',
header: () => 'Age',
cell: (info) => info.renderValue(),
footer: (info) => info.column.id,
}),
columnHelper.accessor('visits', {
header: () => '<span>Visits</span>',
footer: (info) => info.column.id,
}),
columnHelper.accessor('status', {
header: 'Status',
footer: (info) => info.column.id,
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
footer: (info) => info.column.id,
}),
])
// 6. Register the Alpine component.
Alpine.data('table', () => {
// Store data with a stable reference
const local = Alpine.reactive({ data: [...defaultData] })
// 7. Create the table instance with the required columns and data. Features
// and row models were already defined in the `createTableHook` call above.
const table = createAppTable({
columns,
get data() {
return local.data
},
// add additional table options here or in the createTableHook call above
})
// `table.FlexRender` is attached by the hook, so the template can render
// headers/cells/footers with `x-html="table.FlexRender({ ... })"`.
return { table }
})
window.Alpine = Alpine
Alpine.start()