Repository navigation
|
I am trying to make an advanced filters modal, and I am hitting a snag. I have a Here is an example of what I made <script lang="ts" generics="T extends DBContent[keyof DBContent]">
import type { TablePersistent } from "$lib/components/NavTable/TableClass.svelte";
import type { AdvancedFilterBuilder } from "@vincjo/datatables";
import { database } from "$lib";
import { check } from "@vincjo/datatables";
type Props = {
table: TablePersistent<T>;
};
const { table }: Props = $props();
const sourceFilterIS = table.createAdvancedFilter(r => r.sources, check.isEqualTo, "sourceIS");
const sourceFilterNOT = table.createAdvancedFilter(r => r.sources, check.isNotEqualTo, "sourceNOT");
const sourcesIds = $derived([...new Set(table.rawRows.flatMap(x => x.sources))]);
const sourcesIdsAfterFilter = $derived([...new Set(table.rows.flatMap(x => x.sources))]);
const sources = $derived(Array.fromAsync(sourcesIds, x => database.sources.getOne(x, true)));
$inspect(table.filters);
function filterFactory(
filters: [AdvancedFilterBuilder<T>, AdvancedFilterBuilder<T>],
value: string,
): { onclick: (ev: MouseEvent) => void; oncontextmenu: (ev: MouseEvent) => void } {
return {
onclick: () => {
const IS = filters[0].criteria.includes(value);
const NOT = filters[1].criteria.includes(value);
// If its in neither, set IS
if (!IS && !NOT) filters[0].set(value);
// If its in IS, unset IS and set NOT
if (IS && !NOT) {
filters[0].set(value); // Unset IS
filters[1].set(value); // Set NOT
}
// If its in NOT, unset NOT
if (NOT) filters[1].set(value);
},
oncontextmenu: (ev) => {
ev.preventDefault();
const IS = filters[0].criteria.includes(value);
const NOT = filters[1].criteria.includes(value);
// If its in neither, set NOT
if (!IS && !NOT) filters[1].set(value);
// If its in NOT, unset NOT and set IS
if (!IS && NOT) {
filters[1].set(value); // Unset NOT
filters[0].set(value); // Set IS
}
// If its in IS, unset IS
if (IS) filters[0].set(value);
},
};
}
</script>
<div class="space-y-2 max-w-screen-5xl w-[720px] h-[60vh]">
<header>
<h1 class="h4">Filters</h1>
<hr class="hr border-surface-400-600" />
<!-- https://github.com/vincjo/datatables/discussions/186 -->
<p class="text-xs opacity-50 text-right pt-1">
Experimental: Known Issues include NOT filters not working.
</p>
</header>
<div>
<span class="
label-text mx-2 px-1 py-0.5 w-min
border border-surface-500 rounded-md border-b-0 rounded-b-none
">
Sources
</span>
<div class="
flex items-center gap-1 flex-wrap p-2
border border-surface-500 rounded-md
">
{#each await sources as source}
{@const value = source.id}
<!-- A standard badge -->
<button
{...filterFactory([sourceFilterIS, sourceFilterNOT], value)}
class:preset-filled-success-50-950!={ sourceFilterIS.criteria.includes(value) }
class:preset-filled-error-50-950!={ sourceFilterNOT.criteria.includes(value) }
disabled={!sourcesIdsAfterFilter.find(x => x === value) && !sourceFilterNOT.criteria.includes(value)}
class="badge preset-surface border border-surface-500 hover:preset-tonal-surface"
>
{source.name}
</button>
{/each}
</div>
</div>
</div>
|
Replies: 3 comments 6 replies
|
Looks like everything gets passed down to this datatables/src/lib/src/client/core/entry.ts Lines 12 to 22 in d1383ac |
|
Ok. One solution that comes to mind is to stringify the array in the getter and then reconstruct it back in a custom check function. So to fix the above code, I'd just have to do this: const sourceFilterIS = table.createAdvancedFilter(r => r.sources.join(","), check.isLike, "sourceIS");
const sourceFilterNOT = table.createAdvancedFilter(r => r.sources.join(","), check.isNotLike, "sourceNOT");Would this be the only available option right now? @vincjo |
|
Indeed. A custom check function should handle arrays of any type. const sources = table.createCalculation('sources').distinct({ sort: ['value', 'asc'] }) // distinct sources
const filter = table.createAdvancedFilter('sources')
// your custom check functions
const IS = (entry, value) => entry.includes(value)
const NOT = (entry, value) => entry.includes(value) ? false : true<ul>
{#each sources as source}
<li>
{source}
<!-- you can set the check function at this level using the same filter -->
<button onclick={() => filter.set(source, IS)}>IS</button>
<button onclick={() => filter.set(source, NOT)}>NOT</button>
<li>
{/each}
<ul>(Theorically it should work) |
Ok. One solution that comes to mind is to stringify the array in the getter and then reconstruct it back in a custom check function.
So to fix the above code, I'd just have to do this:
Would this be the only available option right now? @vincjo