Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I disable search and make autoComplete just display the API result set #400

Open
adamtheapiguy opened this issue Feb 19, 2023 · 5 comments

Comments

@adamtheapiguy
Copy link

Hey guys,

I have REST API that searches and returns back the results and I use the autoComplete to just display these results 'as is'. I don't need to search them again.

Is there a version of autoComplete or a way to modify current code to just display whatever it receives from the REST API call?

Cheers,
adam

@LuViKu
Copy link

LuViKu commented Mar 15, 2023

Hey I encountered the same problem. I think according to the documentation you could implement a custom search engine. For me setting the search engine to "loose" solved my problem.

EDIT:
It turns out building a custom search engine is easier than I thought.
function searchEngine(query, record) { return record;} This directly returns all the results you receive from your REST API call. The only this is, that the parts of the result matching the query string are not highlighted anymore.

@adamtheapiguy
Copy link
Author

Thank you @LuViKu

@squareclouds
Copy link

@LuViKu can you please give an example of the whole config or setup?

@LuViKu
Copy link

LuViKu commented Mar 22, 2023

@squareclouds sure here is my implementation:

`// custom search engine
function searchEngine(query, record) {
return record;
}

    const autoCompleteJS = new autoComplete({
        selector: "#autoComplete",
        placeHolder: "Search for your stuff",
        searchEngine: searchEngine, // Strict, loose or custom search engine
        data: {
            src: async (query) => {
                try {
                    // Fetch Data from external Source
                    const source = await fetch(`your api endpoint`);
                    const data = await source.json();
                    return data;
                } catch (error) {
                    return error;
                }
            },
            // Gets the value for the specified key from your api's response
            keys: ["my-api-value"]
        },
        resultItem: {
            highlight: true, //Does not work with custom search engine. You'll need to implement this yourself if necessary
            element: (item, data) => {
                // Here you can modify the div containing your data (item) and your data (data).
                }
            },
        },
        events: {
            input: {
                selection: (event) => {
                    // I use this for accessing the data of the result selected by the user.
                }
            }
        }
    });`

Please let me know if anything is unclear.

@squareclouds
Copy link

squareclouds commented Mar 31, 2023

@LuViKu perfect! this worked like a charm :) you had one mistake though, after resultItem you close the whole object, you have one bracket too much

resultItem: {
            highlight: true, //Does not work with custom search engine. You'll need to implement this yourself if necessary
            element: (item, data) => {
                // Here you can modify the div containing your data (item) and your data (data).
                }
            },
        }, //  <<<-------- must be removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants