Replies: 1 comment 2 replies
-
Did you ever find something that worked? I had AI in cursor create something real quick that works pretty good. file: src/types/tantable.d.ts: import "@tanstack/react-table";
declare module "@tanstack/react-table" {
interface Column<TData extends RowData, TValue> {
getUniqueValues: () => (string | number)[];
}
} file: src/utils/tantable.ts import { Column, Table } from "@tanstack/react-table";
export function extendColumnWithUniqueValues<TData>(table: Table<TData>) {
// Add getUniqueValues method to each column
table.getAllColumns().forEach((column: Column<TData, unknown>) => {
if (!column.getUniqueValues) {
column.getUniqueValues = () => {
const values = new Set<string | number>();
table.getRowModel().rows.forEach((row) => {
const value = row.getValue(column.id);
if (value !== undefined && value !== null) {
values.add(value as string | number);
}
});
return Array.from(values).sort((a, b) => {
if (typeof a === "number" && typeof b === "number") {
return a - b;
}
return String(a).localeCompare(String(b));
});
};
}
});
} file: src/components/SearchPanel/MyTable.tsx import { ReactElement } from "react";
import {
useReactTable,
} from "@tanstack/react-table";
import { extendColumnWithUniqueValues } from "../../utils/tantable";
export default function MyTable({
params
}): ReactElement {
const table = useReactTable({
// TabStack table params
});
// Extend columns with getUniqueValues method
extendColumnWithUniqueValues(table);
// Example usage of getUniqueValues
table.getAllColumns().forEach((column) => {
if (column.getCanFilter()) {
console.log(`Unique values for ${column.id}:`, column.getUniqueValues());
}
});
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
In the examples, I see the usage of
getFacetedUniqueValues
to populate an html datalist. In my case, I'm using select inputs instead of a text input with datalist, so it would make more sense to show all possible values, even if it will result in no matches when combined with the other applied filters. As I think this use case isn't too uncommon, it would be nice to be able to dogetColumn(columnId).getUniqueValues()
for example, without the faceting.I'm aware that a function already exists by that name internally, but that seems to serve a different purpose.
Beta Was this translation helpful? Give feedback.
All reactions