Question

I have a jason complex object containing, first name, last name, middkle name, FH name and agent ID. I am uisng bootstrap-typehead for making an ajax call to fetch results. User can enter either first name, last name or agent id. I wouldl ike to show all above fields as lables and agent Id as id. I am geting undefined error.

DEMO

 $('#selectAgent').typeahead({
source: function (query, process) {
    objects = [];
    map = {};
    var data = [{"AgentID": 3456,"FName": "Gary", "LName": "Means","MName": "K","FHName": "GaryFH Name"}, {"AgentID": 2050,"FName": "Kevin", "LName": "Mortha","MName": "V","FHName": "Keving FH Name"},{"AgentID": 5678,"FName": "Mike", "LName": "Hundt","MName": "","FHName": "Mike FH Name"}] // Or get your JSON dynamically and load it into this variable

    $.each(data, function (i, object) {
        map[object.label] = object;
        objects.push(object.Fname + object.LName );
    });
    console.log(objects);
    process(objects);
},
updater: function (item) {
    $('id').val(map[item].AgentID);
    return item;
}
});

//Console log

LOG: undefinedMeans,undefinedMortha,undefinedHundt

Was it helpful?

Solution

I've got the solution to your problem, it's case sensitivity. You'll see that you happen to be referencing Fname and not FName. In practise it's best to dispose of any irregular capitalisation like this, just makes maintaining code easier. In terms of filtering for both agentId and names, this is slightly more difficult but I think you'll find the solution here suitable.

I would pass the whole user record around though that would require you to define sorting and all sorts that is already there for you if it's normal alphanumerical items. Gonna try and get it working by passing along to prototype, if I do I'll update.

JSFiddle

Looked into passing the sorting calls back to the typeahead library but the overhead just isn't worth it. Hope the above is enough!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top