Semur Nabiev
Semur Nabiev

Reputation: 800

jquery autocomplete data format

I am trying to display jSON data in jQuery autocomplete, and everything works fine except the dropdown doesnt show up.
I could use parse: (for which there is a ton of examples) but i think i need the data to be displayed through source:.

is it possible to display data the way i am doing or do we need parse?
if yes, how?

Im not very good with jquery UI

$(function() {
    function log( message ) {
        $( "<div/>" ).text( message ).prependTo( "#log" );
        $( "#log" ).attr( "scrollTop", 0 );
    }

    $( "#aut_teachers" ).autocomplete({
        source: function(request, response) {
            $.ajax({
                url: base_url+"controller/model",
                data: request,
                dataType: "json",
                type: "post",
                success: function(data){
                    response(data.value);// here is where the problem is
                }

            });
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
            "Selected: " + ui.item.value + " aka " + ui.item.id :
            "Nothing selected, input was " + this.value );
        }
    });
});

Upvotes: 2

Views: 5542

Answers (1)

sergiogarciadev
sergiogarciadev

Reputation: 2202

You should use data.message as a parameter do response instead of data.value.

Since, data.message is a array of objects, and the objects are label/value, you can use it.

Check Autocomplete Docs for more information.

Upvotes: 2

Related Questions