bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

typeahead not working with objects

Open Grigorius opened this issue 13 years ago • 4 comments

When using an array of objects typeahead doesn't fill input field on select. This is an ugly fix:

scope.select = function (activeIdx) {
        //called from within the $digest() cycle
        var locals = {};
        var model,
            item,
            first;

        if (typeof scope.matches[activeIdx].model === "object"){
            for (first in scope.matches[activeIdx].model) {
                break;
            }
            locals[parserResult.itemName] = item = scope.matches[activeIdx].model[first]
        } else {
            locals[parserResult.itemName] = item = scope.matches[activeIdx].model;
        }
        model = parserResult.modelMapper(originalScope, locals);
        $setModelValue(originalScope, model);
        modelCtrl.$setValidity('editable', true);

        onSelectCallback(originalScope, {
          $item: item,
          $model: model,
          $label: parserResult.viewMapper(originalScope, locals)
        });

        resetMatches();

        //return focus to the input element if a mach was selected via a mouse click event
        element[0].focus();
      };

Grigorius avatar Sep 13 '13 16:09 Grigorius

@Grigorius you should start with a reproduce scenario, instead of dumping "ugly fix" in the issue content. I'm afraid that I can't do anything with it until you provide a minimal reproduce scenario using http://plnkr.co/

pkozlowski-opensource avatar Sep 13 '13 16:09 pkozlowski-opensource

Sorry about that ... here is a plunk: http://plnkr.co/edit/F9uhj7NhM7bVuPbqdh22

When selecting a country - the input field becomes [object Object], it would be nice if it took the value of the first key and display that instead? or I might be missing something obvious :)

Grigorius avatar Sep 13 '13 20:09 Grigorius

You are missing something obvious :-) One of the possible solutions: http://plnkr.co/edit/70SUf7OEfJzZp4xM1rMq?p=preview

Btw, talking about "first key" doesn't make much sense in case of objects as you don't have any guarantee in JavaScript regarding keys order in objects.

pkozlowski-opensource avatar Sep 13 '13 20:09 pkozlowski-opensource

Thank you!

Grigorius avatar Sep 15 '13 18:09 Grigorius