Skip to content

Commit

Permalink
chore: use global filter to hide empty rows
Browse files Browse the repository at this point in the history
  • Loading branch information
katharinawuensche committed Nov 13, 2024
1 parent 06a1443 commit f075b83
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
9 changes: 6 additions & 3 deletions components/data-table/data-table-filter-columns.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ function toggleCategory(category: Column<never>) {
targetVisibility = true;
}
category.columns.forEach((c) => {
c.toggleVisibility(targetVisibility);
if (!targetVisibility) c.setFilterValue([]);
if (c.getIsVisible() !== targetVisibility) {
c.setFilterValue([]);
c.toggleVisibility(targetVisibility);
}
});
}
function getAllColumnsVisibilityState() {
Expand All @@ -56,8 +58,9 @@ function toggleAllCategories() {
default:
targetVisibility = true;
}
if (!targetVisibility) props.table.resetColumnFilters(true);
props.table.toggleAllColumnsVisible(targetVisibility);
if (!targetVisibility) props.table.resetColumnFilters();
}
const isCollapsibleOpen = ref(columns.value.map(() => false));
Expand Down
3 changes: 3 additions & 0 deletions components/data-table/data-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
type ColumnDef,
type ColumnFiltersState,
type FilterFn,
FlexRender,
getCoreRowModel,
getFacetedRowModel,
Expand All @@ -26,6 +27,7 @@ interface Props {
minHeaderDepth?: number;
enableFilterOnColumns?: boolean;
initialColumnVisibility?: Record<string, boolean>;
globalFilterFn?: FilterFn<never>;
}
const props = defineProps<Props>();
Expand Down Expand Up @@ -88,6 +90,7 @@ const table = useVueTable({
getFilteredRowModel: getFilteredRowModel(),
getFacetedRowModel: getFacetedRowModel(),
getFacetedUniqueValues: customFacetedUniqueValues,
globalFilterFn: props.globalFilterFn,
});
onMounted(() => {
Expand Down
29 changes: 21 additions & 8 deletions components/geojson-table-window-content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type CellContext,
type ColumnDef,
createColumnHelper,
type Row,
type Table,
} from "@tanstack/vue-table";
Expand Down Expand Up @@ -47,11 +48,8 @@ const columns = computed(() => {
);
},
filterFn: (row, columnId, filterValue) => {
if (
!row.getValue(columnId) ||
(row.getValue(columnId) as Array<string>).length === 0
) {
return false;
if (!row.getVisibleCells().find((cell) => cell.column.id === columnId)) {
return true;
}
if (Object.keys(filterValue).length === 0) return true;
const filter = Object.values(filterValue).some((val) =>
Expand Down Expand Up @@ -82,10 +80,11 @@ const columns = computed(() => {
cell.row.original.properties[cell.column.columnDef.id!],
);
},
accessorFn: (cell: FeatureType) =>
cell.properties[String(Object.keys(heading)[0])],
accessorFn: (cell: FeatureType) => {
return cell.properties[String(Object.keys(heading)[0])];
},
enableColumnFilter: false,
enableGlobalFilter: false,
enableGlobalFilter: true,
};
}),
});
Expand All @@ -101,7 +100,20 @@ const columnVisibility = computed(() => {
);
});
function filterEmptyRows(row: Row<never>) {
const hidableVisibleCells = row.getVisibleCells().filter((cell) => cell.column.getCanHide());
if (
hidableVisibleCells.length > 0 &&
hidableVisibleCells.every(
(cell) => !cell.getValue() || (cell.getValue() as string).length === 0,
)
)
return false;
return true;
}
function applyGlobalFilter(table: Table<FeatureType>) {
// re-apply global filter to remove empty lines from the table
table.resetGlobalFilter(true);
table.setGlobalFilter(true);
}
Expand Down Expand Up @@ -154,6 +166,7 @@ function registerTable(table: Table<FeatureType>) {
v-if="!isPending"
:columns="columns as unknown as Array<ColumnDef<never>>"
:enable-filter-on-columns="true"
:global-filter-fn="filterEmptyRows"
:initial-column-visibility="columnVisibility"
:items="fetchedData.get(url)?.features as Array<never>"
:min-header-depth="1"
Expand Down

0 comments on commit f075b83

Please sign in to comment.