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: '{"2":"treatment 1"}'
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: '{"2":"treatment 1"}'
seem strange you?
first, sent through htmlspecialchars
, quotes became "e;
, 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
Post a Comment