triggdev
triggdev

Reputation: 105

How to implement jquery-autocomplete from devbridge with Flask?

I've search for about 2 days for an answer and have had no luck. At this point, I have no idea what I'm doing. According to my search this should be very simple, so I must be missing something simple.

This is my javascript in my index.html with a single input field with the id seach_term:

$('#search_term').autocomplete({
    serviceUrl: '/autocomplete',
    dataType: 'json',
    onSelect: function (suggestion) {
        alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
    }
});

This is the view code for /autocomplete:

suggestions = [{'value': 'joe', 'data': 'joe'}, {'value': 'jim', 'data': 'jim'}]
return jsonify({"suggestions": suggestions})

I know this question has been asked a ton, but I still have yet to find an answer that was explained enough for me to be able to apply that solution to my problem.

What am I doing wrong?

Upvotes: 2

Views: 438

Answers (1)

Shiladitya
Shiladitya

Reputation: 12181

$( "#search_term" ).autocomplete({
  serviceUrl: '/autocomplete',
  select: function( event, ui ) {
    alert( "Selected: " + ui.item.value + " aka " + ui.item.data );
  }
});

This might help you. Please check the jquery autocomplete link https://jqueryui.com/autocomplete/#remote-jsonp

Upvotes: 1

Related Questions