2

I have an invoice template and want to start from another index on the second page, if the service fields doesnt fit on the first page anymore. For example if 12 service fields were created on the 1st page than please add a new service object on the 2nd page (13th element). Right now, it shows the first element from the formArray on the 2nd page. I use the reactive form with formArray.

Please take a look in my code on Stackblitz: https://stackblitz.com/edit/angular-invoice?file=src/app/app.component.html

I have created for the second page and further pages another template:

<ng-template #sndPage>
        <div id="{{ i+1 }}" class="page">
            <div class="content">
                <div resizeObserver (resize)="onResize($event)">
                    <h1>Another Page</h1>
                    <form [formGroup]="invoiceForm" (ngSubmit)="onSubmit()">
                        <div class="service-table" formArrayName="services">
                            <ng-container 
                                *ngFor="let service of services.controls | slice:13; let j = index;" <- How to change the start index?
                                [formGroupName]="j" 
                            >
                            <div id="service" *ngIf="page.growth; else elseBlock" >   
                                
                                <div class="pos">Pos.: {{page.start + j+1}} </div>
                                <textarea 
                                    class="title"
                                    formControlName="title"

                                    autosize
                                    [minRows]="1"

                                    placeholder="Caption2"
                                >
                                </textarea>
                                <textarea 
                                    class="detail"
                                    formControlName="detail"

                                    autosize
                                    [minRows]="1"

                                    placeholder="Describe2"

                                >
                                </textarea>
                                <div class="price">
                                    <input
                                        class="price"
                                        formControlName="price"
                                        AutoSizeInput
                                        placeholder="Price"
                                    >
                                </div>
                            </div>
                            <ng-template #elseBlock>
                                <div id="service" *ngIf="page.start + j < page.lastServiceElement">   
                                
                                    <div class="pos">Pos.: {{page.start + j+1}}</div>
                                    <textarea 
                                        class="title"
                                        formControlName="title"

                                        autosize
                                        [minRows]="1"

                                        placeholder="caption2"
                                    >
                                    </textarea>
                                    <textarea 
                                        class="detail"
                                        formControlName="detail"

                                        autosize
                                        [minRows]="1"

                                        placeholder="Describe2"

                                    >
                                    </textarea>
                                    <div class="price">
                                        <input
                                            class="price"
                                            formControlName="price"
                                            AutoSizeInput
                                            placeholder="Price"
                                        >
                                    </div>
                                </div>
                            </ng-template>
                        </ng-container>
                        <button mat-icon-button type="button" color="accent" *ngIf="page.growth" (click)="addService()"> 
                            <mat-icon>add_circle_outline</mat-icon>
                        </button>
                        </div>
                        <button type="submit" [disabled]="!invoiceForm.valid">Submit</button>
                    </form>
                </div>
            </div>
        </div>
    </ng-template>

Also I guess that the slice pipe doesnt work for formArray. I set the number (13) only for testing.

Bonus: If the "last" service description field doesnt fit on the first page, is there a good way to continue with the description field on the next page?

B0r1
  • 400
  • 3
  • 15
  • Maybe I've found a workaround for my issue. The idea is to do it with nested formArrays, so every page will hold an own services-array. I'll try it out! – B0r1 Aug 25 '20 at 21:48
  • Can you please reduce the code just to keep minimal reproduction? We don't need all textareas at all here – yurzui Aug 26 '20 at 02:16
  • I have fixed my issue with nested formArrays. Much more elegant way. – B0r1 Aug 28 '20 at 09:50
  • @B0r1 can you post that solution then? You can add itt to another section in the original post. – Alex Biro Aug 29 '20 at 06:14
  • I already tried to post the source where I found the solution for my problem. Unfortunately it was deleted by a mod. I will post it here: https://stackblitz.com/edit/deep-nested-reactive-form – B0r1 Aug 29 '20 at 22:23

0 Answers0