Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typeahead): typeahead with initial object value displays [object Object] instead of default value #749

Open
Kekstas opened this issue Jul 18, 2016 · 34 comments

Comments

@Kekstas
Copy link

Kekstas commented Jul 18, 2016

Hi,
I am having trouble to set initial input field value when input value is Object using TypeAhead.
I crated a plunk to demonstrate the issue: http://plnkr.co/edit/WSUJ49?p=preview

It is showing [object Object] because it is actually an object, but when TypeAhead getting object from [typeahead] it is able to set object to [(ngModel)] and display value described in [typeaheadOptionField]
I need to do same but on init from object that is set in [(ngModel)].

I could see that same solution was working with angular1.

Any solutions...?
Any comments/suggestions appreciated.

@Dinistro
Copy link
Contributor

@Kekstas You are using a really old version of angular2 and ng2-bootstrap. Try to upgrade to rc.4 and ng2-bootstrap 1.0.24

@Kekstas
Copy link
Author

Kekstas commented Jul 19, 2016

Well... Updated to most latest version... Same issue....
http://plnkr.co/edit/WSUJ49?p=preview

@valorkin valorkin reopened this Jul 19, 2016
@valorkin
Copy link
Member

@Dinistro seems predefined state options just sets ngModel value as is

@Kekstas
Copy link
Author

Kekstas commented Jul 19, 2016

It sets Object to input( looks right), but in that case I can't use objects with TypeAhead when I need Initial value.... But somehow I need save reference Id of input text(that is stored in Objects).

@justasSav
Copy link

+1

@justasSav
Copy link

@Dinistro
Copy link
Contributor

@valorkin @Kekstas @justasSav Currently It's not possible to work with objects in the ngModel. You need to set the value of the `typeaheadOptionField``. You will only get this value, if you change the value.

@roni-frantchi
Copy link

Hi there.
@Kekstas @justasSav were any of you able to work around this?.. How?..

@Dinistro @valorkin is it planned yet to include a fix in any forthcoming releases?
Do you have a solution in mind?.. I might spend some time working on this

@Kekstas
Copy link
Author

Kekstas commented Oct 14, 2016

Just used autocomplete from jqueryui :\

@kernwig
Copy link

kernwig commented May 10, 2017

No work around then? It seems the point of having typeaheadOptionField, which is to let us work with objects, is killed by not being able to get the selected object back.

@Hainesy
Copy link

Hainesy commented Nov 9, 2017

Are there any plans to address this?

@valorkin
Copy link
Member

valorkin commented Nov 9, 2017

we are grooming issues list and fixing all the small things now
plus writing roadmap for ngx-bs v2
so it's in roadmap and will be added soon

@mickeypuri
Copy link

Is there any work around for this?

@YevheniiaMazur YevheniiaMazur changed the title TypeAhead with initial object value fix(typeahead): typeahead with initial object value displays [object Object] instead of default value Jan 16, 2018
@mariusl-a
Copy link

+1 just bumped into this one.. :) Thanks!

@ghost
Copy link

ghost commented Apr 2, 2018

+1 - setting the default selected value would be a good standard feature to have

@ghost
Copy link

ghost commented Apr 2, 2018

FYI for those who are interested I found a bit of a work around until something more solid is available.

in the html I put a second hash tag on the element:
<input #itemSelect
#instance="ngbTypeahead"
.......

then in the .ts file I declared a second @ViewChild variable:
@ViewChild('instance') instance: NgbTypeahead;
@ViewChild('itemSelect') input: ElementRef;

this then allowed me to set the initial value of the underlying tag within the code:
this.input.nativeElement.value = 'xxx';

@ravithb
Copy link

ravithb commented Apr 7, 2018

+1 This is absolutely required when we use this in a search page and we need to come back to the previous page with the search key populated.

@MuraliM
Copy link

MuraliM commented May 21, 2018

We keep waiting for this fix. Unfortunately we cant use https://ng-bootstrap.github.io/#/components/typeahead/examples as it has some other problems like select on blur and bootstrap 4 only. Any good typeahead plugin which is stable and covers the basic functionality like initialization, etc?

@mbasleraecom
Copy link

definitely +1 on this. Strictly using array's of strings is extremely limiting.

@AndreaVitale
Copy link

+1, stuck on this :(

@theCrius

This comment was marked as off-topic.

@exhtml
Copy link

exhtml commented Jul 20, 2018

+1

@suparnavg
Copy link

+1...

@molerat619
Copy link

+1

@prizeGit
Copy link

+1 Using typeahead on a search form which need to be populated after the first initial search.

@TomasMasiulis
Copy link

+1

@davidsennekool
Copy link

+1 This is a key functionality. Does anybody have a workaround?

@valor-software valor-software deleted a comment from theCrius Dec 7, 2018
@Helen1987
Copy link

+1

@valorkin valorkin added this to Needs review in typeahead v2 Dec 16, 2018
@svetoldo4444ka
Copy link
Contributor

@Kekstas
according to docs
joxi_screenshot_1547119154422
ngModel should get a string. Why don't you pass to ngModel (like in your example ) [(ngModel)]="StatePredefined.name"

@valor-software valor-software deleted a comment from theCrius Jan 10, 2019
@vvolodin
Copy link

vvolodin commented Feb 14, 2019

+1
My workaround for working with objects was not using typeaheadOptionField at all. I've created a class for my options and overridden the toString method:

export class ManagerOption {
    constructor(public ManagerId: string, public FriendlyManagerId: string) {}

    public toString = () => {
        return this.FriendlyManagerId || this.ManagerId;
    }
}

I've found out that when typeaheadOptionField is null, the directive will use toString to set a displayed value. So now I'm mapping my options to ManagerOption objects and my reactive form works great with typeahead.

@okayestdeveloper
Copy link

okayestdeveloper commented Apr 2, 2019

Here's basically how I worked around this problem with Angular 7 and NGX-Bootstrap 3.

HTML

<input
  [formControl]="myTypeahead"
  [typeahead]="filteredOpts"
  typeaheadOptionField="value"
  (typeaheadOnSelect)="select($event.item)"/>

TypeScript

interface Opt {
  value: string;
  key: string;
}

export class MyComp implements OnInit {
  myTypeahead = new FormControl();
  options: Opts[];
  filteredOpts: Opts[] = [];
  selectedOption: Opt;

  constructor() {}

  ngOnInit() {
    this.myTypeahead.valueChanges.pipe(startWith(''))
      .subscribe((value) => {
        this.filteredOpts = this._filter(value));
  }
  
  private _filter(value: string): Opt[] {
    return this.options
      .filter((opt: Opt) => opt.value.toLowerCase().includes(value.toLowerCase()));
  }

  select(opt: Opt) {
    this.selectedOption = opt;
  }
}

Based on the Angular Material Autocomplete custom filter example found here: https://material.angular.io/components/autocomplete/overview

My case was slightly more complicated in that I was loading options with an API call in ngOnInit, but that was no issue.

Also note that this is basically doing the work of the typeahead in _filter and is not the most efficient method. However it got me moving forward again.

@Pizzicato
Copy link

Quite surprised this hasn't been fixed yet... Please, add this functionality 🙏

@pedrorey21
Copy link

set the value when the form is opened or initialized with id and description
ngOnInit ()
this.model = {id: 2, description: 'apple'};

@i-Chan93
Copy link

+1.
typeaheadOptionField ideally needs to have the capability to get the entire item object and not just any one of the properties. Defeats the purpose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
typeahead v2
  
Needs review
Development

No branches or pull requests