Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected object in Kendo Autocomplete

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.

like image 335
Axel GALLIOT Avatar asked Aug 02 '16 14:08

Axel GALLIOT


3 Answers

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);
                    }
                }
like image 167
Vijai Avatar answered Nov 15 '22 23:11

Vijai


AutoComplete.select() does not return the current selection, which is confusing as it usually does for the other widgets(Grid, TreeView). http://docs.telerik.com/kendo-ui/api/javascript/ui/autocomplete#methods-select

The .dataItem() method without a parameter should return the selected object for an AutoComplete.

Example: http://dojo.telerik.com/@Stephen/eJonI

like image 44
The Dread Pirate Stephen Avatar answered Nov 15 '22 21:11

The Dread Pirate Stephen


It seems that :

var test = this.dataItem();

don't work on IE, I tried my solution with the globals var on Firefox and it worked... Don't really know why I have this issue on IE.

EDIT : The problem wasn't coming from IE, I was going from one autocomplete to another using tab. But, if I use the tab key or Enter key without selecting the element in the appearing list (if I only use the auto-completion of the word) I'm passing in the change event, but there is nothing selected in my autoComplete, so the content of my var is "undefined".

like image 1
Axel GALLIOT Avatar answered Nov 15 '22 22:11

Axel GALLIOT