Working with Vue 2.7.x Composition API #4930
-
Has anyone been able to get this to work with the Vue 2 composition API? I was able to directly copy and paste the vue adapter code ( <script lang="ts">
import {
defineComponent,
ref,
watch,
watchEffect,
} from '@vue/composition-api'; // or 'vue' if version >= 2.7 & < 3
import { mergeObject } from '~/app/components/ui/table/next/core/merge';
// These exports are the exact same code found in @tanstack/table/vue with import references from `[email protected]` update to use `@vue/composition-api`
import {
createColumnHelper,
getCoreRowModel,
TableOptions,
useVueTable,
FlexRender,
SortingState,
getSortedRowModel,
Header,
getPaginationRowModel,
} from '~/app/components/ui/table/next/core/use-table';
type Person = {
firstName: string;
lastName: string;
age: number;
visits: number;
status: string;
progress: number;
};
const columnHelper = createColumnHelper<Person>();
export default defineComponent({
name: 'TestPage',
components: {
FlexRender,
},
setup() {
const defaultData: 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,
},
];
const columns = [
columnHelper.accessor('firstName', {
cell: (info) => info.getValue(),
header: () => 'First Name',
footer: (props) => props.column.id,
}),
columnHelper.accessor('lastName', {
cell: (info) => info.getValue(),
header: () => 'Last Name',
footer: (props) => props.column.id,
}),
columnHelper.accessor('age', {
header: () => 'Age',
footer: (props) => props.column.id,
}),
columnHelper.accessor('visits', {
header: () => 'Visits',
footer: (props) => props.column.id,
}),
columnHelper.accessor('status', {
header: 'Status',
footer: (props) => props.column.id,
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
footer: (props) => props.column.id,
}),
];
const data = ref(defaultData);
const sorting = ref<SortingState>([]);
const INITIAL_PAGE_INDEX = 0;
const goToPageNumber = ref(INITIAL_PAGE_INDEX + 1);
const pageSizes = [1, 3, 5, 10];
const options: TableOptions<Person> = {
columns,
// data: data.value,
get data() {
return data.value;
},
state: {
get sorting() {
return sorting.value;
},
},
onSortingChange(updaterOrValue) {
sorting.value =
typeof updaterOrValue === 'function'
? updaterOrValue(sorting.value)
: updaterOrValue;
},
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
getPaginationRowModel: getPaginationRowModel(),
};
const table = useVueTable(options);
// watch(data, (newData) => {
// console.log('watch data');
// table.setOptions((prev) => {
// return mergeObject<TableOptions<Person>>(prev, { data: newData });
// });
// });
const rerender = () => {
data.value = defaultData;
};
const addData = () => {
console.log('add data before', table.options.data.length);
// This causes the `watchEffect` in use-table to loop
data.value.push({
firstName: 'joe',
lastName: 'dirte',
age: 45,
visits: 20,
status: 'Complicated',
progress: 10,
});
console.log('add data after', table.options.data.length);
};
function onSortHeaderClicked(
header: Header<Person, unknown>,
event: PointerEvent
) {
const sortHandler = header.column.getToggleSortingHandler();
if (!sortHandler) throw new Error('No sort handler...');
sortHandler(event);
}
function handleGoToPage(e: PointerEvent) {
const page = (e.target as any).value
? Number((e.target as any).value) - 1
: 0;
goToPageNumber.value = page + 1;
table.setPageIndex(page);
}
function handlePageSizeChange(e: PointerEvent) {
table.setPageSize(Number((e.target as any).value));
}
return {
table,
rerender,
addData,
sorting,
onSortHeaderClicked,
goToPageNumber,
pageSizes,
handleGoToPage,
handlePageSizeChange,
};
},
});
</script> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Did you end up having any success with getting this to work with vue 2? |
Beta Was this translation helpful? Give feedback.
-
Just wanted to loop back here and share what I got working.
The only other thing worth mentioning is that I found that you have to spread the props into FlexRender to get the state inside FlexRender to rerender. Ex:
|
Beta Was this translation helpful? Give feedback.
Just wanted to loop back here and share what I got working.