-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add filter function to column headers (#179)
- Loading branch information
1 parent
0a6ae86
commit 89df575
Showing
5 changed files
with
88 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script setup lang="ts"> | ||
import type { Column } from "@tanstack/vue-table"; | ||
import { Filter } from "lucide-vue-next"; | ||
const props = defineProps<{ | ||
column: Column<never, unknown>; | ||
}>(); | ||
const facets = computed(() => props.column?.getFacetedUniqueValues()); | ||
const selectedValues = computed(() => new Set(props.column?.getFilterValue() as Array<string>)); | ||
</script> | ||
|
||
<template> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger class="group p-1 align-middle" | ||
><Filter class="size-4 group-hover:scale-125"></Filter | ||
></DropdownMenuTrigger> | ||
<DropdownMenuContent> | ||
<DropdownMenuCheckboxItem | ||
v-for="facet in facets" | ||
:key="facet[0]" | ||
:checked="selectedValues.has(facet[0])" | ||
@update:checked=" | ||
(checked) => { | ||
if (!checked) { | ||
selectedValues.delete(facet[0]); | ||
} else { | ||
selectedValues.add(facet[0]); | ||
} | ||
column?.setFilterValue([...selectedValues]); | ||
} | ||
" | ||
> | ||
{{ facet[0] }} | ||
<Badge class="ml-2" variant="outline">{{ facet[1] }}</Badge> | ||
</DropdownMenuCheckboxItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</template> |
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
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,37 @@ | ||
import { memo, type Table } from "@tanstack/vue-table"; | ||
|
||
const customFacetedUniqueValues = (table: Table<never>, columnId: string) => { | ||
return memo( | ||
() => [table.getColumn(columnId)?.getFacetedRowModel()], | ||
(facetedRowModel) => { | ||
if (!facetedRowModel) return new Map(); | ||
|
||
const facetedUniqueValues = new Map<unknown, number>(); | ||
|
||
for (const row of facetedRowModel.flatRows) { | ||
const values = row.getUniqueValues<number>(columnId); | ||
|
||
for (const val of values) { | ||
let wrappedValue = [val]; | ||
if (Array.isArray(val)) { | ||
wrappedValue = val; | ||
} | ||
for (const value of wrappedValue) { | ||
if (facetedUniqueValues.has(value)) { | ||
facetedUniqueValues.set(value, (facetedUniqueValues.get(value) ?? 0) + 1); | ||
} else { | ||
facetedUniqueValues.set(value, 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return facetedUniqueValues; | ||
}, | ||
{ | ||
key: `getFacetedUniqueValues_${columnId}`, | ||
}, | ||
); | ||
}; | ||
|
||
export default customFacetedUniqueValues; |