mskocik / svelecte

Flexible autocomplete/select component written in Svelte,

Home Page:https://svelecte.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fetch Remote Query - Pass in default value

Tam2 opened this issue · comments

commented

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

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.

commented

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

commented

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

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

commented

Thanks @mskocik - will test out and report back

Added a monthly sponsorship via my company 👍

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.