ngx-bootstrap icon indicating copy to clipboard operation
ngx-bootstrap copied to clipboard

Typeahead : Could we get an example with an actual async observable?

Open dessalines opened this issue 10 years ago • 17 comments

I had been using the typeahead from ng2-bootstrap 1.0.17, but 1.0.23 broke it. I was using a debounced switch map as follows:

    this.discussionResultsObservable = this.discussionSearchTermStream
      .debounceTime(300)
      .distinctUntilChanged()
      .switchMap((term: string) => this.discussionService.searchDiscussions(term));

This is preferable to one that fetches on every keystroke. But now, the example on this page doesn't have a real async observable version, its just creating and filtering one from static code, and I can't figure out how to get it to work with an actual one. Couldn't you guys create a static json page, possibly either pointing to a raw file on github, or somewhere else, to be able to test and see what the code would look like for an actual observable?

Thanks.

dessalines avatar Jul 15 '16 17:07 dessalines

Based on the example , I did this:

constructor(service : Service) {
        this.dataSource = Observable.create((observer:any) => {
            this.service.method(this.typeheadModel)
            .subscribe((result : any ) => {
                observer.next(result);
            });
        });
    }

Combined with [typeaheadWaitMs]="300"

This helps you?

nereujunior avatar Jul 15 '16 18:07 nereujunior

Yes this does, I'll test this out later today. It should be in the docs too. I never knew typeaheadWaitMs existed.

dessalines avatar Jul 15 '16 19:07 dessalines

@tchoulihan The docs are not up to date, I'm sorry. I will do this on the weekend.

Dinistro avatar Jul 15 '16 20:07 Dinistro

Thanks @Dinistro .

@nereujunior Thanks, I got it working!

dessalines avatar Jul 16 '16 00:07 dessalines

This is a side question, and maybe I should open up another issue for it but:

How do we get the loading and no results to show up in the typeahead dropdown? I can get them to render elsewhere, but not in the dropdown.

dessalines avatar Jul 16 '16 17:07 dessalines

@tchoulihan The sample was updated a little bit (#745). I hope you see how you can use it now.

Dinistro avatar Jul 17 '16 13:07 Dinistro

Sure, looks good to me.

dessalines avatar Jul 17 '16 15:07 dessalines

@nereujunior Can you post your complete code for typeahead async working with a service? I mean the complete html and ts code?

noorahmed94 avatar Jul 21 '16 10:07 noorahmed94

@noorahmed94 An example is here: http://valor-software.com/ng2-bootstrap/#/typeahead

Dinistro avatar Jul 21 '16 12:07 Dinistro

@nereujunior @Dinistro worked for me !! helped a lot. thanks!!

noorahmed94 avatar Jul 21 '16 13:07 noorahmed94

Hi Guys, I am getting error "TypeError: matches.slice is not a function".

Am i doing anything wrong?

app.component.ts

  public constructor(private freightService: FreightService) {
      this.dataSource = Observable.create((observer: any) => {
          this.freightService.getMatchingProduct(this.asyncSelected).subscribe((result: any) => {
              observer.next(result);
          })
      });
  }

productSearch.service.ts

    getMatchingProduct(productKeyword: string): Observable <any> {

        return this.http.get("app/test")
            .map(this.extractData);
    }

  private extractData(res: Response) {
        let body = res.json();
        return body.data || { };
    }

typeahead-demo.html

  <form [formGroup]="myForm">
    <input formControlName="state"
         [(ngModel)]="asyncSelected"
         [typeahead]="dataSource"
         (typeaheadLoading)="changeTypeaheadLoading($event)"
         (typeaheadNoResults)="changeTypeaheadNoResults($event)"
         (typeaheadOnSelect)="typeaheadOnSelect($event)"
         [typeaheadWaitMs]="300"
         [typeaheadOptionsLimit]="7"
         [typeaheadOptionField]="'productDescription'"
         placeholder="Locations loaded with timeout"
         class="form-control">
  </form>

I can see http response body in network tab

[{
    "productDescription": "MAIL MKTG NIKLES HIGHLIGHT TUBE",
    "productNumber": "10667"
}, {
    "productDescription": "TEST GLASSES/VAC BREAKER BR 15MM BSP",
    "productNumber": "100436"
}, {
    "productDescription": "SIGHT GLASSES/VAC BREAKER BR 15MM BSP",
    "productNumber": "100438"
}]

sourabh1983 avatar Aug 24 '16 23:08 sourabh1983

It looks good to me so far, except try doing this in the ngOnInit instead of the constructor.

dessalines avatar Aug 25 '16 15:08 dessalines

Thanks tchoulihan for responding. Found problem, below code was mapping Observable to undefined.

 private extractData(res: Response) {
        let body = res.json();
        return body.data || { };**_
    }

"body.data" is undefined. It should be "body"

sourabh1983 avatar Aug 25 '16 23:08 sourabh1983

this.dataSource = Observable.create((observer: any) => { this.freightService.getMatchingProduct(this.asyncSelected).subscribe((result: any) => { observer.next(result); }) });

@sourabh1983 Im almost two years too late x) but what should i do in the error case of the respond. Its abviously ugly if the console throws me an error if the result is not found, since the user could type anything into the input^^

MarcoLeko avatar May 05 '18 18:05 MarcoLeko

@MarcoLeko I think you would use something like this inside the subscribe method:

if (result) {
  observer.next(result);
}
observer.error(new Error('ERRORRR!'));

marksyzm avatar May 15 '18 15:05 marksyzm

Upgraded to Angular 7 and it stopped working.

this.dataSource is undefined.

this.dataSource = Observable.create((observer: any) => { this.service.getAreaSearchByText(this.placeNameText) .subscribe((result: any) => { this.searchResults = <AreaTextSearchResult[]>result; let newResult = _.map(this.searchResults, function (result) { return new AutoCompleteResult(result.PolygonAreaCode, result.PlaceName, result.PolygonAreaName); }); observer.next(newResult); }); });

johnbenjaminphe avatar Mar 12 '19 10:03 johnbenjaminphe

Resolved the issue by upgrading the ngx-bootstrap version to 3.2.0

johnbenjaminphe avatar Mar 13 '19 10:03 johnbenjaminphe