mskocik / svelecte

Flexible autocomplete/select component written in Svelte,
https://svelecte.vercel.app
MIT License
469 stars 42 forks source link

Not being able to set custom label for options with remote datasource. #157

Closed miklereverie closed 1 year ago

miklereverie commented 1 year ago

Hello! I may be f**king up this somehow, but I can't seem to be able to set the options's labels to a specific property of the objects I'm getting from the remote datasource.


       let selectionProcedencia: any;

    function myFetch2(inputQuery: any, fetchCallback: any) {
        return new Promise((resolve, reject) => {
            fetch(
                `https://mytestingdomain.com/procedencia/find?query=descripcion~${inputQuery}&page=0&size=10`,
                {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        Authorization:
                            ' Bearer eyJhbGciOiJIUzUxM'
                    }
                }
            )
                .then((res) => res.json())
                .then((json) => {
                    let myData = json.content;
                    resolve(myData);
                }); //
        });
    }

                                <Svelecte
                                    {resetOnBlur}
                                    {fetchResetOnBlur}
                                    bind:value={selectionProcedencia}
                                    valueAsObject
                                    valueField={selectionProcedencia?.idProcedencia}
                                                                        labelField={selectionProcedencia?.descripcion}
                                    name="afiliadoProcedencia"
                                    minQuery={minQueryValue}
                                    placeholder="Comience a escribir el nombre de la obra social..."
                                    fetch={myFetch2}
                                    inputId="selectProcedencia"
                                />

However, when I try this, it shows the code instead of the description: image

I'm logging the selection to the console and I see the object with the correct properties, image so I'm quite sure I'm just making some stupid mistake that is preventing me from setting value and label to the correct object properties, could you help me out please? Thanks in advance for your time!!!

mskocik commented 1 year ago

Three things:

And solution to your issue is to read carefully my inline comment from this line:

    resolve(myData) // this should be 👉item array👈 suitable for svelecte

I've added emojis to point you in the right direction. (I am closing this now, because it's duplicate/continuation of your previous question). Open a discussion for questions, not a issue.

miklereverie commented 1 year ago

I'm so sorry! I wasn't sure if I should've continued the conversation on the previous issue or not because I wasn't sure if it was strictly related to it, clearly I made the wrong decision. I didn't provide a REPL because I couldn't afford to post the private API information I'm fetching from, sorry about that too!

Thanks again for your time and your help, will go and re-read carefully everything you answered in the previous issue.

Sorry, again, for any annoyances I may have caused! Most of it just comes from a lack of experience of my part. Thanks for taking the time to explain though, will keep it in mind and act accordingly in the future!

mskocik commented 1 year ago

No hard feelings, I just want to make things clear for the future. So if you still struggle, comment in the previous issue.

REPL doesn't need to expose your live endpoint, you could use http://jsonplaceholder.typicode.com/ for demonstration purpose - svelecte is using it as well in documentation examples.