Shikha
Shikha

Reputation: 551

How to preserve the data for the previous http request before making new request using observable?

Parent component

this.statisticsService.getData().subscribe(data =>  
   this.processData(data));

processData(data: DataWrapper) {
  if (!isNullOrUndefined(data) && data.table.rows.length > 1) {
    this.noDataAvailableFlag = false;
    this._statisticsData.next(data);
  } else { // If no data available from backend
    console.log('no data available');
    this.handleNoDataAvailable();
  }
}

Template

<div *ngIf="isDataAvailable" class="table-box clearfix default-style [ngClass]="backgroundColour">
  <div [hidden] ="!noDataAvailableFlag">
    <span class="no-data-message">{{'DETAILS.No_Data_Available' | translate}}</span>
  </div>
  <div [hidden] ="noDataAvailableFlag" detail-table [_statisticsData]="statisticsData" [maximiser]="maximiser" [queryParams]="filterService.getFullQueryParams()"
     (onVariationClicked$)="onVariationClicked($event)"></div>
  <div [hidden] ="noDataAvailableFlag" class="graph-holder clearfix" [ngClass]="{'opened': maximiser.isMaximised}" detail-graph
     [_statisticsData]="statisticsData"
     [tableVariation]="selectedVariationTitle"></div>

Child component ngOnit method.

ngOnInit() {
  this.windowWidth = window.innerWidth;
  this._statisticsData.subscribe(data => {
  if (!isNullOrUndefined(data)) {
    this.processData(data);
  }
 });
}

If no data is available , i dont want to clear the table. I want data for the previous request remain shown in the table. Please suggest how i can make this workaround.

Upvotes: 1

Views: 129

Answers (1)

Meir
Meir

Reputation: 14375

That's what the filter is for:

this._statisticsData
  .filter(data => !isNullOrUndefined(data))
  .subscribe(data => {
    this.processData(data);
  });

Upvotes: 1

Related Questions