Skip to content

Commit ef9c458

Browse files
committed
Add conversion for missing values in filters
1 parent 9499aac commit ef9c458

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

src/lib/components/Table/TableFilterServer.svelte

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,29 @@
1313
export let filters;
1414
export let updateTable;
1515
export let pageIndex;
16+
export let toStringFn: undefined | ((value: any) => string) = undefined;
1617
1718
// If the filter is applied and the displayed values are filtered
1819
let active = false;
20+
let type: string = 'string';
21+
let isDate = false;
22+
let dropdowns: {
23+
option: FilterOptionsEnum;
24+
value: string | number | Date | undefined;
25+
}[] = [];
26+
27+
// Check the type of the column
28+
$values.forEach((item) => {
29+
if (item) {
30+
type = typeof (toFilterableValueFn ? toFilterableValueFn(item) : item);
31+
32+
if (type === 'object') {
33+
if ((toFilterableValueFn ? toFilterableValueFn(item) : item) instanceof Date) {
34+
isDate = true;
35+
}
36+
}
37+
}
38+
});
1939
2040
// Options for different types of values
2141
const options = {
@@ -99,11 +119,6 @@
99119
]
100120
};
101121
102-
let dropdowns: {
103-
option: FilterOptionsEnum;
104-
value: string | number | Date | undefined;
105-
}[] = [];
106-
107122
// Unique ID for the column filter popup
108123
const popupId = `${tableId}-${id}`;
109124
// Popup config
@@ -113,24 +128,25 @@
113128
placement: 'bottom-start'
114129
};
115130
116-
let type: string = 'string';
117-
let isDate = false;
118-
// Check the type of the column
119-
$values.forEach((item) => {
120-
if (item) {
121-
type = typeof (toFilterableValueFn ? toFilterableValueFn(item) : item);
131+
const stringValues = $values.map((item) => (toStringFn ? toStringFn(item) : item));
122132
123-
if (type === 'object') {
124-
if (item instanceof Date) {
125-
isDate = true;
126-
}
127-
}
128-
}
129-
});
133+
const missingValues = stringValues.reduce((acc, item, index) => {
134+
acc[typeof item === 'string' ? item.toLowerCase() : item] = $values[index];
135+
return acc;
136+
}, {});
137+
138+
const getMissingValue = (value: string) => {
139+
return Object.keys(missingValues).includes(value.toLowerCase())
140+
? missingValues[value.toLowerCase()]
141+
: value;
142+
};
130143
131144
const optionChangeHandler = (e, index) => {
132145
delete $filters[id][dropdowns[index].option];
133-
$filters[id] = { ...$filters[id], [e.target.value]: dropdowns[index].value };
146+
$filters[id] = {
147+
...$filters[id],
148+
[e.target.value]: getMissingValue(dropdowns[index].value as string)
149+
};
134150
$filters = $filters;
135151
136152
dropdowns[index] = {
@@ -142,17 +158,18 @@
142158
const valueChangeHandler = (e, index) => {
143159
dropdowns[index] = {
144160
...dropdowns[index],
145-
value:
146-
type === 'number'
147-
? +e.target.value
148-
: type === 'date'
149-
? new Date(e.target.value)
150-
: e.target.value
161+
value: type === 'date' ? new Date(e.target.value) : e.target.value
151162
};
152163
153164
$filters = {
154165
...$filters,
155-
[id]: { ...$filters[id], [dropdowns[index].option]: dropdowns[index].value }
166+
[id]: {
167+
...$filters[id],
168+
[dropdowns[index].option]:
169+
type === 'number'
170+
? parseFloat(getMissingValue(e.target.value)) || undefined
171+
: getMissingValue(e.target.value)
172+
}
156173
};
157174
};
158175
@@ -201,6 +218,8 @@
201218
202219
// Start by adding the default filter
203220
$: addFilter(options[type][0].value, undefined);
221+
222+
$: console.log($filters);
204223
</script>
205224

206225
<form class="">
@@ -258,14 +277,14 @@
258277
{/if}
259278
</div>
260279

261-
{#if type === 'number'}
280+
<!-- {#if type === 'number'}
262281
<input
263282
type="number"
264283
class="input p-1 border border-primary-500"
265284
on:input={(e) => valueChangeHandler(e, index)}
266285
bind:value={dropdown.value}
267-
/>
268-
{:else if type === 'string'}
286+
/> -->
287+
{#if type === 'number' || type === 'string'}
269288
<input
270289
type="text"
271290
class="input p-1 border border-primary-500"

0 commit comments

Comments
 (0)