This commit is contained in:
seb
2026-07-19 19:43:23 +02:00
parent eea8e99358
commit 92dc32bf12
2 changed files with 24 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ Fuzzy article search. Use this to build a search box / result list with category
Pass category IDs as a comma-separated list: `filter=16,39`.
Whenever `filter` is present, **`categories` always includes every id from `filter`**, even when `length` is `0` or a filtered id has no hits. Those entries use master-data `name` / `parent` and may have `count: 0`. Other facet categories with hits are still returned as usual.
Whenever `filter` is present, **`categories` always includes every id from `filter` and its full ancestor chain** (parent → … → root), even when `length` is `0` or a filtered id has no hits. Those entries use master-data `name` / `parent` and may have `count: 0`. Other facet categories with hits are still returned as usual.
## Response `200`
@@ -58,14 +58,15 @@ Whenever `filter` is present, **`categories` always includes every id from `filt
}
```
Empty result with an active filter still returns the applied category:
Empty result with a nested filter still returns the leaf and its ancestors:
```json
{
"length": 0,
"recordset": [],
"categories": {
"16": { "name": "Dünger", "count": 0, "parent": 0 }
"16": { "name": "Dünger", "count": 0, "parent": 0 },
"39": { "name": "Erde", "count": 0, "parent": 16 }
}
}
```

View File

@@ -77,18 +77,27 @@ function shapeCategories(counts, byId) {
return categories;
}
/** Ensure every applied filter id appears in categories (count 0 if no hits). */
function ensureFilterCategories(categories, filter, byId) {
/**
* Ensure every applied filter id and its ancestors appear in categories
* (count 0 if no hits), so the frontend can render hierarchy via parent links.
*/
function ensureFilterCategories(categories, filter, byId, parentMap) {
for (const id of filter) {
if (id === '' || id === null || categories[id]) {
if (id === '' || id === null) {
continue;
}
const meta = byId[id] ?? {};
categories[id] = {
count: 0,
name: meta.name ?? null,
parent: meta.parent ?? null,
};
for (const ancestorId of getUpPath(id, parentMap)) {
const key = String(ancestorId);
if (categories[key]) {
continue;
}
const meta = byId[ancestorId] ?? byId[key] ?? {};
categories[key] = {
count: 0,
name: meta.name ?? null,
parent: meta.parent ?? null,
};
}
}
return categories;
}
@@ -206,7 +215,8 @@ export async function searchArticles(target, filter = [''], offset = 0, maxCount
categoryData.byId
),
filter,
categoryData.byId
categoryData.byId,
categoryData.parentMap
);
const length = result.recordset.length;
const recordset = result.recordset