Flask AJAX Autocomplete

后端 未结 1 1665
梦如初夏
梦如初夏 2020-12-15 13:00

I\'m trying to make the jQuery UI autocomplete widget work with the Flask framework.

http://flask.pocoo.org/docs/patterns/jquery/

http://jqueryui.com/autocom

相关标签:
1条回答
  • 2020-12-15 13:32

    You have to wrap the $.getJSON() in a function which will get executed by the plugin whenever the value of the textfield is changed

    source: function( request, response ) {
        $.getJSON($SCRIPT_ROOT + "/_search_university", {
            search: request
        }, response);
    }
    

    Now depending on what you are returning from the server, the above may suffice. However if you need to filter or map the data in order for autocomplete to display it you will need to use the $.map() function to transform the data to a format acceptable by autocomplete

    source: function( request, response ) {
            $.getJSON($SCRIPT_ROOT + "/_search_university", {
                search: request
            }, function( data ) {
                response( $.map( data.results, function( item ) {
                    return {
                        label: item.name,
                        value: item.id
                    }
                }));
            });
    }
    

    If you provide me, with the JSON that your server returns, I can be more specific

    Check http://api.jqueryui.com/autocomplete/#option-source to see more information

    0 讨论(0)
提交回复
热议问题