svelecte icon indicating copy to clipboard operation
svelecte copied to clipboard

Fetch Remote Query - Pass in default value

Open Tam2 opened this issue 1 year ago • 7 comments

Is it possible to trigger a remote query when the value changes programmatically?

I have a field set-up like this:

<Svelecte
	fetch="/api/parties?query=[query]"
	{fetchCallback}
	bind:value
	valueField="id"
	labelField="name"
	placeholder="Select Party"
	required
	name="party"
	clearable
	renderer="avatar-blocks"
	style="position:unset"
	fetchResetOnBlur={true}
	resetOnBlur={true}
/>

I'm passing in a value but the fetch call is not being made, if i type into the box it's running the fetch with the query, but i have a use case where i need to change the value programmatically and have the ability for it to trigger the remote query

Tam2 avatar Dec 10 '23 13:12 Tam2

Ca you provide working example? Because it's not clear whether you mean you pass value for bind:value or you pass value for input field triggering the fetch.

mskocik avatar Dec 10 '23 14:12 mskocik

Here is an example

https://svelte.dev/repl/e520ca3eb29c4017af07fe4121585f94?version=4.2.8

I would want it to do a ?id=1 search when mounted and then if the value of the value variable changes for it to re-trigger the fetch

Tam2 avatar Dec 10 '23 14:12 Tam2

You can do it like this

https://svelte.dev/repl/e8f4dbc65650402a876b77366afb66d3?version=4.2.8

mskocik avatar Dec 10 '23 14:12 mskocik

Thanks for that, what if we had something like this

https://svelte.dev/repl/e520ca3eb29c4017af07fe4121585f94?version=4.2.8

Where we wanted to change the value either by typing in the box or allow the value variable to be updated directly

Tam2 avatar Dec 10 '23 14:12 Tam2

Here you go

https://svelte.dev/repl/3dd4e216c461459a9f2b2baf762a52d4?version=4.2.8

This is pretty hacky, but with current fetch api limitations there is not other way. Because Svelecte discards every value, that's invalid (ie. not present in the options array, which for fetch are always empty). It only works for initial fetch.

There should be support for such an use case in v4

mskocik avatar Dec 10 '23 15:12 mskocik

Thanks @mskocik - will test out and report back

Added a monthly sponsorship via my company 👍

Tam2 avatar Dec 10 '23 16:12 Tam2

Thanks @mskocik - will test out and report back

Let me know If you would need to clarify something, why it's done this way.

Added a monthly sponsorship via my company 👍

Much appreciated, thanks for support.

mskocik avatar Dec 11 '23 08:12 mskocik