Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Searchable

(function () {
    var client = algoliasearch('Application ID', 'Admin API Key');
    var index = client.initIndex('headlines');
    //initialize autocomplete on search input (ID selector must match)
    autocomplete('#aa-search-input',
        {hint: false}, {
            source: autocomplete.sources.hits(index),
            //value to be displayed in input control after user's suggestion selection
            displayKey: 'title',
            //hash of templates used when rendering dataset
            templates: {
                suggestion: function (suggestion) {
                    return `
                        <div class="algolia-result">
                            <span>
                                ${suggestion._highlightResult.title.value.replace(/^(.{50}[^s]*).*/, "$1")}
                            </span>
                            <span>${suggestion._highlightResult.category.value}</span>
                        </div>
                    `;
                }
            }
        }).on('autocomplete:selected', function (event, suggestion, dataset) {
            // console.log(suggestion)
        window.location.href = window.location.origin + '/headlines/' + suggestion.slug;
    });
})();
Copy code
Source by programmierfrage.com #
 
PREVIOUS NEXT
Tagged: #Searchable
ADD COMMENT
Topic
Name
3+7 =