whizzly's avatar

bootstrap-3-typeahead with local installation

how can i use bootstrap-3-typeahead without the link to "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"?

i implemented the example for autocompletion with bootstrap3-typeahead. this is described in many online-posts, for example this one: https://w3path.com/laravel-6-autocomplete-search-using-typeahead-js-example/ In all that examples is used the above link.

But i want to install that local without using the link. With npm <install npm install --save bootstrap-3-typeahead> that works fine.

But i can not find a way to register it in app.js, it fails ... has someone a tip? import bootstrap3-typeahead from 'bootstrap-3-typeahead'; --> syntax error because of the minus

thx in advance

whizzly

0 likes
5 replies
ahmeddabak's avatar

This is how i did in my last project

require('typeahead.js'); //import typeahead
let Handlebars = require('handlebars'); //import Handlebars which is a small size js template engine
let axios = require('axios');

$('.typeahead').typeahead({
        highlight: true,
    },
    {
        display: 'title',
        source: function (query, syncResults, asyncResults) {
            axios.get('/search', {
                responseType: 'json',
                params: {
                    query: query
                }
            }).then(function (response) {
                asyncResults(response.data);
            });
        },
        templates: {
            empty: Handlebars.compile('<div class="bg-white border p-3 rounded"><p>No results found</p></div>'),
            suggestion: Handlebars.compile('<div class="d-flex bg-white border p-3 rounded"><div class="w-75 mr-2"><p class="h4">{{title}}</p><p>{{description}}</p></div><div class="w-25"><img class="img-fluid" src="{{thumbnail}}" alt="title" style="max-height: 150px"></div></div>')
            //<a href="/products/{{slug}}" class="position-absolute" style="height: 100%; width: 100%; top:0; left: 0"></a>
        }
    });

whizzly's avatar

what type of file is that? That does not work in the script part of a blade-file

ahmeddabak's avatar
Level 47

No, this is es6 you need to add it in your resources/js/app.js and the run npm run dev to compile it.

if you are going to install package through npm you have to require them require('typeahead.js'); and then compile you source js files, otherwise you should stick with CDN or local installation

whizzly's avatar

It would be nice, if you could explain that in 3 words. I installed es6 with <npm install es6 --save> . Is that the right way? Now which line do i need in app.js to register? Tried

require('./ES6.js');

in app.js, but compiling that gives me: can't resolve

I think i have something to import before ...

Please or to participate in this conversation.