-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(vue): vue filters example (#5644)
* examples: vue filters * run through prettier * add missing components * fix typing issue * add docs link to new example
- Loading branch information
Showing
14 changed files
with
485 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Example | ||
|
||
To run this example: | ||
|
||
- `npm install` or `yarn` | ||
- `npm run dev` or `yarn dev` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="vite/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" href="/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/main.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "tanstack-table-example-vue-filters", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview", | ||
"test:types": "vue-tsc" | ||
}, | ||
"dependencies": { | ||
"@faker-js/faker": "^8.4.1", | ||
"vue": "^3.4.31", | ||
"@tanstack/vue-table": "^8.19.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.14.9", | ||
"@vitejs/plugin-vue": "^5.0.5", | ||
"typescript": "5.4.5", | ||
"vite": "^5.3.2", | ||
"vue-tsc": "^2.0.22" | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
<script setup lang="ts"> | ||
import { | ||
ColumnFiltersState, | ||
FlexRender, | ||
createColumnHelper, | ||
getCoreRowModel, | ||
getFacetedMinMaxValues, | ||
getFacetedRowModel, | ||
getFacetedUniqueValues, | ||
getFilteredRowModel, | ||
useVueTable, | ||
} from '@tanstack/vue-table' | ||
import { ref } from 'vue' | ||
import DebouncedInput from './DebouncedInput.vue' | ||
import Filter from './Filter.vue' | ||
type Person = { | ||
firstName: string | ||
lastName: string | ||
age: number | ||
visits: number | ||
status: string | ||
progress: number | ||
} | ||
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 columnHelper = createColumnHelper<Person>() | ||
const columns = [ | ||
columnHelper.group({ | ||
header: 'Name', | ||
footer: props => props.column.id, | ||
columns: [ | ||
columnHelper.accessor('firstName', { | ||
cell: info => info.getValue(), | ||
footer: props => props.column.id, | ||
}), | ||
columnHelper.accessor(row => row.lastName, { | ||
id: 'lastName', | ||
cell: info => info.getValue(), | ||
header: () => 'Last Name', | ||
footer: props => props.column.id, | ||
}), | ||
], | ||
}), | ||
columnHelper.group({ | ||
header: 'Info', | ||
footer: props => props.column.id, | ||
columns: [ | ||
columnHelper.accessor('age', { | ||
header: () => 'Age', | ||
footer: props => props.column.id, | ||
}), | ||
columnHelper.group({ | ||
header: 'More Info', | ||
columns: [ | ||
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 rerender = () => { | ||
data.value = defaultData | ||
} | ||
const columnFilters = ref<ColumnFiltersState>([]) | ||
const globalFilter = ref('') | ||
const table = useVueTable({ | ||
get data() { | ||
return data.value | ||
}, | ||
columns, | ||
state: { | ||
get columnFilters() { | ||
return columnFilters.value | ||
}, | ||
get globalFilter() { | ||
return globalFilter.value | ||
}, | ||
}, | ||
onColumnFiltersChange: updaterOrValue => { | ||
columnFilters.value = | ||
typeof updaterOrValue === 'function' | ||
? updaterOrValue(columnFilters.value) | ||
: updaterOrValue | ||
}, | ||
onGlobalFilterChange: updaterOrValue => { | ||
globalFilter.value = | ||
typeof updaterOrValue === 'function' | ||
? updaterOrValue(globalFilter.value) | ||
: updaterOrValue | ||
}, | ||
getCoreRowModel: getCoreRowModel(), | ||
getFilteredRowModel: getFilteredRowModel(), | ||
getFacetedRowModel: getFacetedRowModel(), | ||
getFacetedUniqueValues: getFacetedUniqueValues(), | ||
getFacetedMinMaxValues: getFacetedMinMaxValues(), | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="p-2"> | ||
<div> | ||
<DebouncedInput | ||
:modelValue="globalFilter ?? ''" | ||
@update:modelValue="value => (globalFilter = String(value))" | ||
className="p-2 font-lg shadow border border-block" | ||
placeholder="Search all columns..." | ||
/> | ||
</div> | ||
<div className="h-2" /> | ||
<table> | ||
<thead> | ||
<tr | ||
v-for="headerGroup in table.getHeaderGroups()" | ||
:key="headerGroup.id" | ||
> | ||
<th | ||
v-for="header in headerGroup.headers" | ||
:key="header.id" | ||
:colSpan="header.colSpan" | ||
> | ||
<FlexRender | ||
v-if="!header.isPlaceholder" | ||
:render="header.column.columnDef.header" | ||
:props="header.getContext()" | ||
/> | ||
<template | ||
v-if="!header.isPlaceholder && header.column.getCanFilter()" | ||
> | ||
<Filter :column="header.column" :table="table" /> | ||
</template> | ||
</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"> | ||
<FlexRender | ||
:render="cell.column.columnDef.cell" | ||
:props="cell.getContext()" | ||
/> | ||
</td> | ||
</tr> | ||
</tbody> | ||
<tfoot> | ||
<tr | ||
v-for="footerGroup in table.getFooterGroups()" | ||
:key="footerGroup.id" | ||
> | ||
<th | ||
v-for="header in footerGroup.headers" | ||
:key="header.id" | ||
:colSpan="header.colSpan" | ||
> | ||
<FlexRender | ||
v-if="!header.isPlaceholder" | ||
:render="header.column.columnDef.footer" | ||
:props="header.getContext()" | ||
/> | ||
</th> | ||
</tr> | ||
</tfoot> | ||
</table> | ||
<div class="h-4" /> | ||
<button @click="rerender" class="border p-2">Rerender</button> | ||
</div> | ||
</template> | ||
<style> | ||
html { | ||
font-family: sans-serif; | ||
font-size: 14px; | ||
} | ||
table { | ||
border: 1px solid lightgray; | ||
} | ||
tbody { | ||
border-bottom: 1px solid lightgray; | ||
} | ||
th { | ||
border-bottom: 1px solid lightgray; | ||
border-right: 1px solid lightgray; | ||
padding: 2px 4px; | ||
} | ||
tfoot { | ||
color: gray; | ||
} | ||
tfoot th { | ||
font-weight: normal; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script lang="ts" setup> | ||
import { computed, onBeforeUnmount, ref } from 'vue' | ||
const props = defineProps({ | ||
modelValue: { | ||
type: [String, Number], | ||
required: true, | ||
}, | ||
debounce: { | ||
type: Number, | ||
default: 500, | ||
}, | ||
}) | ||
const emit = defineEmits(['update:modelValue']) | ||
const timeout = ref<ReturnType<typeof setTimeout>>() | ||
const localValue = computed({ | ||
get() { | ||
return props.modelValue | ||
}, | ||
set(newValue) { | ||
if (timeout.value) { | ||
clearTimeout(timeout.value) | ||
} | ||
timeout.value = setTimeout( | ||
() => emit('update:modelValue', newValue), | ||
props.debounce | ||
) | ||
}, | ||
}) | ||
onBeforeUnmount(() => clearTimeout(timeout.value)) | ||
</script> | ||
|
||
<template> | ||
<input v-model="localValue" v-bind="$attrs" /> | ||
</template> |
Oops, something went wrong.