Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

rxjs startWith but on nested array

I have a component whose rxjs listens to changes in the queryparams:

universityApplications$ = this.activatedRoute.queryParams.pipe(
    switchMap((obj) => this.httpService.getUniversityApplicationsList(this.numberOfRows, new URLSearchParams(obj).toString()).pipe(startWith(null))
  ));

And for the html

    <div class="skeleton-y5ha4dikw8f">
        <ng-container *ngIf="(universityApplications$ | async)?.data as uniData">
            <p-table [value]="uniData">
                <ng-template pTemplate="header">
                    <tr>
                        <th>Application name</th>
                        <th>University</th>
                    </tr>
                </ng-template>
                <ng-template pTemplate="body" let-detail>
                    <tr>
                        <td>{{detail.name}}</td>
                        <td>{{detail.university_name}}</td>
                    </tr>
                </ng-template>
            </p-table>
        </ng-container>
    </div>
  <p-paginator *ngIf="(universityApplications$ | async)?.pagination as paginationData" [rows]="numberOfRows" [first]="paginationData.current_page" [totalRecords]="paginationData.total" (onPageChange)="paginate($event)" #pp></p-paginator>

And this works fine. When I click through the pagination the page changes correctly. Since I’m using startWith(null) my inner data array gets wiped each time so I can see the nice skeleton screen. However the pagination also gets wiped out and I want that to always stay on screen until it gets updated. Is there a way I can only have startWith(null) only apply to the inner data array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What if you introduce a dedicated observable nonNullResult$ for the paginator, that ignores null-values? E.g. you could do something like this:

Component TS:

universityApplications$ = this.activatedRoute.queryParams.pipe(
    switchMap((obj) => this.httpService.getUniversityApplicationsList(this.numberOfRows, new URLSearchParams(obj).toString()).pipe(startWith(null))
));

nonNullResult$ = this.universityApplications$.pipe(filter(x => !!x));

Component HTML:

<p-paginator *ngIf="(nonNullResult$ | async)?.pagination as paginationData" [rows]="numberOfRows" [first]="paginationData.current_page" [totalRecords]="paginationData.total" (onPageChange)="paginate($event)" #pp></p-paginator>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading