|
| 1 | +<script setup lang="ts"> |
| 2 | +import { |
| 3 | + ColumnFiltersState, |
| 4 | + FlexRender, |
| 5 | + createColumnHelper, |
| 6 | + getCoreRowModel, |
| 7 | + getFacetedMinMaxValues, |
| 8 | + getFacetedRowModel, |
| 9 | + getFacetedUniqueValues, |
| 10 | + getFilteredRowModel, |
| 11 | + useVueTable, |
| 12 | +} from '@tanstack/vue-table' |
| 13 | +import { ref } from 'vue' |
| 14 | +import DebouncedInput from './DebouncedInput.vue' |
| 15 | +import Filter from './Filter.vue' |
| 16 | +type Person = { |
| 17 | + firstName: string |
| 18 | + lastName: string |
| 19 | + age: number |
| 20 | + visits: number |
| 21 | + status: string |
| 22 | + progress: number |
| 23 | +} |
| 24 | +const defaultData: Person[] = [ |
| 25 | + { |
| 26 | + firstName: 'tanner', |
| 27 | + lastName: 'linsley', |
| 28 | + age: 24, |
| 29 | + visits: 100, |
| 30 | + status: 'In Relationship', |
| 31 | + progress: 50, |
| 32 | + }, |
| 33 | + { |
| 34 | + firstName: 'tandy', |
| 35 | + lastName: 'miller', |
| 36 | + age: 40, |
| 37 | + visits: 40, |
| 38 | + status: 'Single', |
| 39 | + progress: 80, |
| 40 | + }, |
| 41 | + { |
| 42 | + firstName: 'joe', |
| 43 | + lastName: 'dirte', |
| 44 | + age: 45, |
| 45 | + visits: 20, |
| 46 | + status: 'Complicated', |
| 47 | + progress: 10, |
| 48 | + }, |
| 49 | +] |
| 50 | +const columnHelper = createColumnHelper<Person>() |
| 51 | +const columns = [ |
| 52 | + columnHelper.group({ |
| 53 | + header: 'Name', |
| 54 | + footer: props => props.column.id, |
| 55 | + columns: [ |
| 56 | + columnHelper.accessor('firstName', { |
| 57 | + cell: info => info.getValue(), |
| 58 | + footer: props => props.column.id, |
| 59 | + }), |
| 60 | + columnHelper.accessor(row => row.lastName, { |
| 61 | + id: 'lastName', |
| 62 | + cell: info => info.getValue(), |
| 63 | + header: () => 'Last Name', |
| 64 | + footer: props => props.column.id, |
| 65 | + }), |
| 66 | + ], |
| 67 | + }), |
| 68 | + columnHelper.group({ |
| 69 | + header: 'Info', |
| 70 | + footer: props => props.column.id, |
| 71 | + columns: [ |
| 72 | + columnHelper.accessor('age', { |
| 73 | + header: () => 'Age', |
| 74 | + footer: props => props.column.id, |
| 75 | + }), |
| 76 | + columnHelper.group({ |
| 77 | + header: 'More Info', |
| 78 | + columns: [ |
| 79 | + columnHelper.accessor('visits', { |
| 80 | + header: () => 'Visits', |
| 81 | + footer: props => props.column.id, |
| 82 | + }), |
| 83 | + columnHelper.accessor('status', { |
| 84 | + header: 'Status', |
| 85 | + footer: props => props.column.id, |
| 86 | + }), |
| 87 | + columnHelper.accessor('progress', { |
| 88 | + header: 'Profile Progress', |
| 89 | + footer: props => props.column.id, |
| 90 | + }), |
| 91 | + ], |
| 92 | + }), |
| 93 | + ], |
| 94 | + }), |
| 95 | +] |
| 96 | +const data = ref(defaultData) |
| 97 | +const rerender = () => { |
| 98 | + data.value = defaultData |
| 99 | +} |
| 100 | +const columnFilters = ref<ColumnFiltersState>([]) |
| 101 | +const globalFilter = ref('') |
| 102 | +const table = useVueTable({ |
| 103 | + get data() { |
| 104 | + return data.value |
| 105 | + }, |
| 106 | + columns, |
| 107 | + state: { |
| 108 | + get columnFilters() { |
| 109 | + return columnFilters.value |
| 110 | + }, |
| 111 | + get globalFilter() { |
| 112 | + return globalFilter.value |
| 113 | + }, |
| 114 | + }, |
| 115 | + onColumnFiltersChange: updaterOrValue => { |
| 116 | + columnFilters.value = |
| 117 | + typeof updaterOrValue === 'function' |
| 118 | + ? updaterOrValue(columnFilters.value) |
| 119 | + : updaterOrValue |
| 120 | + }, |
| 121 | + onGlobalFilterChange: updaterOrValue => { |
| 122 | + globalFilter.value = |
| 123 | + typeof updaterOrValue === 'function' |
| 124 | + ? updaterOrValue(globalFilter.value) |
| 125 | + : updaterOrValue |
| 126 | + }, |
| 127 | + getCoreRowModel: getCoreRowModel(), |
| 128 | + getFilteredRowModel: getFilteredRowModel(), |
| 129 | + getFacetedRowModel: getFacetedRowModel(), |
| 130 | + getFacetedUniqueValues: getFacetedUniqueValues(), |
| 131 | + getFacetedMinMaxValues: getFacetedMinMaxValues(), |
| 132 | +}) |
| 133 | +</script> |
| 134 | + |
| 135 | +<template> |
| 136 | + <div class="p-2"> |
| 137 | + <div> |
| 138 | + <DebouncedInput |
| 139 | + :modelValue="globalFilter ?? ''" |
| 140 | + @update:modelValue="value => (globalFilter = String(value))" |
| 141 | + className="p-2 font-lg shadow border border-block" |
| 142 | + placeholder="Search all columns..." |
| 143 | + /> |
| 144 | + </div> |
| 145 | + <div className="h-2" /> |
| 146 | + <table> |
| 147 | + <thead> |
| 148 | + <tr |
| 149 | + v-for="headerGroup in table.getHeaderGroups()" |
| 150 | + :key="headerGroup.id" |
| 151 | + > |
| 152 | + <th |
| 153 | + v-for="header in headerGroup.headers" |
| 154 | + :key="header.id" |
| 155 | + :colSpan="header.colSpan" |
| 156 | + > |
| 157 | + <FlexRender |
| 158 | + v-if="!header.isPlaceholder" |
| 159 | + :render="header.column.columnDef.header" |
| 160 | + :props="header.getContext()" |
| 161 | + /> |
| 162 | + <template |
| 163 | + v-if="!header.isPlaceholder && header.column.getCanFilter()" |
| 164 | + > |
| 165 | + <Filter :column="header.column" :table="table" /> |
| 166 | + </template> |
| 167 | + </th> |
| 168 | + </tr> |
| 169 | + </thead> |
| 170 | + <tbody> |
| 171 | + <tr v-for="row in table.getRowModel().rows" :key="row.id"> |
| 172 | + <td v-for="cell in row.getVisibleCells()" :key="cell.id"> |
| 173 | + <FlexRender |
| 174 | + :render="cell.column.columnDef.cell" |
| 175 | + :props="cell.getContext()" |
| 176 | + /> |
| 177 | + </td> |
| 178 | + </tr> |
| 179 | + </tbody> |
| 180 | + <tfoot> |
| 181 | + <tr |
| 182 | + v-for="footerGroup in table.getFooterGroups()" |
| 183 | + :key="footerGroup.id" |
| 184 | + > |
| 185 | + <th |
| 186 | + v-for="header in footerGroup.headers" |
| 187 | + :key="header.id" |
| 188 | + :colSpan="header.colSpan" |
| 189 | + > |
| 190 | + <FlexRender |
| 191 | + v-if="!header.isPlaceholder" |
| 192 | + :render="header.column.columnDef.footer" |
| 193 | + :props="header.getContext()" |
| 194 | + /> |
| 195 | + </th> |
| 196 | + </tr> |
| 197 | + </tfoot> |
| 198 | + </table> |
| 199 | + <div class="h-4" /> |
| 200 | + <button @click="rerender" class="border p-2">Rerender</button> |
| 201 | + </div> |
| 202 | +</template> |
| 203 | +<style> |
| 204 | +html { |
| 205 | + font-family: sans-serif; |
| 206 | + font-size: 14px; |
| 207 | +} |
| 208 | +
|
| 209 | +table { |
| 210 | + border: 1px solid lightgray; |
| 211 | +} |
| 212 | +
|
| 213 | +tbody { |
| 214 | + border-bottom: 1px solid lightgray; |
| 215 | +} |
| 216 | +
|
| 217 | +th { |
| 218 | + border-bottom: 1px solid lightgray; |
| 219 | + border-right: 1px solid lightgray; |
| 220 | + padding: 2px 4px; |
| 221 | +} |
| 222 | +
|
| 223 | +tfoot { |
| 224 | + color: gray; |
| 225 | +} |
| 226 | +
|
| 227 | +tfoot th { |
| 228 | + font-weight: normal; |
| 229 | +} |
| 230 | +</style> |
0 commit comments