I have a svelte page to show a list of post, I need to give the user the ability to filter that list by several conditions, eg: year, tags, category, etc. All those conditions should be concurrent, just like the amazon website sidebar.
Using a derived store I got one filter working fine, but still I can't manage to get 2 or 3 filters applied at the same time.
For the "year" filter I have the following code:
import { readable, writable, derived } from "svelte/store";
export let data;
let posts = readable(data.posts);
const extractColumn = (arr, column) => arr.map((x) => x[column]);
const allYears = extractColumn(data.posts, "year");
const years = readable ([...new Set(allYears)]);
const filter = writable({ year: null });
const filteredPosts = derived([filter, posts], ([$filter, $posts]) => {
if (!$filter.year) return $posts;
return $posts.filter((x) => x.year === $filter.year);
});
So, based in the posts list I can get the years array for the filter and then with a simple select trigger the filtering action, and this work pretty fine.
<select bind:value={$filter.year}>
{#each $years as year}
<option value={year}>{year}</option>
{/each}
</select>
As I said at the beginning, the point is how to add complexity to this scenario to turn a simple filter into a system, with a variable number of dynamic cumulative filters.
Thanks for any idea.