php - Typeahead and Laravel not returning any results -


i trying setup typeahead.js, use autosuggestion feature on laravel app. unfortunately, returns no results, each time.

i return data beforehand take advantage of local storage, there no querying db in instance.

route:

route::get('/', function () {     return view('welcome', ['treatments' => treatment::orderby('treatment')         ->pluck('treatment', 'id')]); }); 

welcome view:

const treatments = new bloodhound({     datumtokenizer: bloodhound.tokenizers.whitespace,     querytokenizer: bloodhound.tokenizers.whitespace,     local: '{{$treatments}}'   });    $('#bloodhound').typeahead({       hint: true,       highlight: true,       minlength: 1     },     {       name: 'treatments',       source: treatments,        templates: {         empty: [           '<div class="list-group search-results-dropdown"><div class="list-group-item">nothing found.</div></div>'         ],         header: [           '<div class="list-group search-results-dropdown">'         ]        }     }).on('typeahead:selected', function (evt, item) {     $('#bloodhound').text(item.id);   }); 

input field:

<input type="search" name="treatment" id="bloodhound" class="form-control"          placeholder="find treatment" autocomplete="off" required> 

output of $treatments array:

local: '{&quot;2&quot;:&quot;treatment 1&quot;}' 

the last section of script, should enter value of selection (id ) within input field, unfortunately doesn't work either.

many thanks.

doesn't string local: '{&quot;2&quot;:&quot;treatment 1&quot;}' seem strange you?

first, sent through htmlspecialchars , quotes became &quote;, second - value of local string. think, typeahead can understand have here?

solution: elements form database , output'em json-encoded. avoid quotes escaping use {!! !!}:

const treatments = new bloodhound({     datumtokenizer: bloodhound.tokenizers.whitespace,     querytokenizer: bloodhound.tokenizers.whitespace,     local: {!! $treatments !!} }); 

route:

route::get('/', function () {     return view(         'welcome',          ['treatments' => treatment::orderby('treatment')->pluck('treatment', 'id')->tojson()]     ); }); 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -