0

Data model in ts:

export class PO {
  id: number;
  name: string;
  submitted: boolean = false;
}

Service, to get all POs

getPO() {
    return this.http
      .get<PO[]>(this.poUrl)
      .pipe(map(data => data), catchError(this.handleError));
  }

to get only those submitted = true, following this existing question,

getSubmitted() {
    return this.http
    .get<PO[]>(this.poUrl)
    .pipe(map(data => data.find(data => data.submitted === true)), catchError(this.handleError));
  }

it still returns everything, tried data.filter(), the find/filter is not in effect.

6
  • You may need to provide an example of how the data comes in specifically for you prior to the map(). You could maybe utilize tap() to console log data before map() is executed. Also, find() will at most return a single element in an array. Commented Jun 28, 2018 at 18:10
  • @AlexanderStaroselsky agree on find(), but tried filter() too. Commented Jun 28, 2018 at 18:14
  • You may want to show how getSubmitted() is executed and the data consumed in your component to get a full picture. Commented Jun 28, 2018 at 18:15
  • component: ngOnInit() { this.getSubmitted();} html: <ul> <li *ngFor="let p of PO">... Commented Jun 28, 2018 at 18:22
  • Make sure the response is actually the data. Also you can use the rxjs filter instead of using map and then JavaScript Array filter. Commented Jun 28, 2018 at 18:32

3 Answers 3

0

You are modifying your API response on front-end. It will diffcult for to manage on different platforms. I prefered creating two WebAPI services. One for getting data where submitted is flag is true. Second service where flag is false.

Reason: In future if you planning to develop mobile app, then you have to hard code you logic in mobile app too.

Service data should be straight.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not clear how entirely you are referencing and rendering the results of the getSubmitted() by the comment provided of <li *ngFor="let p of PO"> there may be issues with clashing of variables given your data model object is called PO and the results are looping through a variable also called PO. Or assignment to a local variable of type PO[] is simply isn't happening. Try the following to filter and render PO objects with submitted property of boolean true:

getSubmitted() {
  return this.http
    .get<PO[]>(this.poUrl)
    .pipe(
      // tap(response => console.log(response)), to check/log structure before map()
      // assumes along lines of [{id:1, name:'foo', submitted:false}, {id:2, name:'bar', submitted:true}]
      map(data => data.filter(d => d.submitted)), 
      catchError(this.handleError)
    );
}

Here are a couple approaches of how you can use to consume the data being returned from a successful http.get().

submitted$: Observable<PO[]>;

ngOnInit() {
  this.submitted$ = this.getSubmitted();
}

// <li *ngFor="let p of submitted$ | async">

Or:

submitted: PO[] = [];

ngOnInit() {
  this.getSubmitted()
    .subscribe(submitted => this.submitted = submitted);
}

// <li *ngFor="let p of submitted">

Here is a Stackblitz showing filtering functionality in action based on submitted object property of PO. It provides an example with and without an Async Pipe.

Hopefully that helps!

Comments

0
data;
filteredData;

getPO() {
   return this.http.get<PO[]>(this.poUrl).subscribe(
      data => {
        this.data = data;
        this.filteredData = data;
      }
   );
}

getSubmitted() {
   this.filteredData = this.data.filter( el => el.submitted === true);
}

Then in your html, in your ngFor, if you have to iterate filteredData,

 *ngFor="let row of filteredData" 

this way you don't download 2 times

Warning *** filter must be on the private array, not de data returned form the http call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.