Get selected object in Kendo Autocomplete

Axel GALLIOT picture Axel GALLIOT · Aug 2, 2016 · Viewed 9k times · Source

I have a Kendo Autocomplete item :

<input type="text" id="Ac_Transporteur" class="" maxlength="30" required/>
--------------------------------------------------------------------------
    $("#Ac_Transporteur").kendoAutoComplete({
    dataTextField: "Nom",
    //Not interesting code here
    dataSource: dsTransporteurs,
    suggest: true,
    delay: 0
    });

I have no problem selecting my objects from my datasource dsTransporteur, but I need to get the object that is selected in the autocomplete.
I tried this :

var transp = $("#Ac_Transporteur").data("kendoAutoComplete");
var transpSelect = transp.select();
oVehicule._Transporteur = transp.dataItem(transpSelect);

but transp.select() don't return the index of the object in the datasource and is "undefined".
Any idea how I can get the object selected in my autocomplete ?

I also tried to add a global var named veh_Transporteur and added this :

change: function (e) {
        veh_TRANSPORTEUR = this.dataItem();
},

But I still have "undefined" in veh_TRANSPORTEUR.

Answer

Vijai picture Vijai · Aug 2, 2016

Try the following

$("#Ac_Transporteur").kendoAutoComplete({
dataTextField: "Nom",
dataSource: dsTransporteurs,
suggest: true,
delay: 0,
select: onSelect
});

function onSelect(e) {
                        var dataItem = this.dataItem(e.item.index());
                        alert(dataItem);
                    }
                }