0

I have created a formArray table and now I am trying to show only 4 rows in 4 columns because I have an array that contains only 4 regions so I made a combinations of region with each other region and with itself using a nested loop with O(n)^2 complexity which gave me 16 combinations and on initializating the formarray all the input fields show up in first column which should not be the case. I need to show only 4 in each column. I am using Angular material, So how can i achieve this? Here is my html code

<div *ngIf="enableCombinationDirective">
  <div fxLayout="row">
      <div class="h1">Add Prices</div>
      <div [formGroup]="combinationForm">
        <div formArrayName="dest" *ngFor="let item of combinationForm.get('dest')['controls']; let i = index">
          <div fxLayout="row" class="check" fxLayoutAlign="space-evenly">
            <mat-form-field appearance="outline" [formGroupName]="i" style="width: 20% !important;" apprearance="outline" fxFlex="50">
              <mat-label>{{
                combinationForm
                    .get("dest")
                    ["controls"][i].get("combinationTitle").value
            }}</mat-label>
            <input
                          matInput
                          type="number"
                          #destInput
                          formControlName="price"
                          placeholder="Enter Price"
                      />
                      <mat-checkbox
                      formControlName="isActive"
                      matSuffix
                  ></mat-checkbox>
            </mat-form-field>
          </div>
        </div>
        <div fxLayout="row">
          <button
              fxFlex="100"
              mat-raised-button
              color="primary"
              (click)="save()"
          >
              Save Changes
          </button>
      </div>
    </div>
  </div>
</div>

The form works fine and i am able to fetch data from it too. here is how it looks

enter image description here

here is how i am initializing it

 createCombinations() {
    let arrayOfData = [];
    arrayOfData.push({
      volumeSlabType: this.combinationSelectionForm.value.volumeSlab, 
      serviceType: this.combinationSelectionForm.value.serviceType,
      weightSlab: this.combinationSelectionForm.value.weightSlab,
      region: this.getRegionsCombinations(this.combinationSelectionForm.value.region)
    });

    this.data = arrayOfData;
    this.data[0].region.forEach((reg, i) => {
      const fArray = this.combinationForm.get("dest") as FormArray;
      fArray.push(this.createForm(reg, i));
    });
    console.log(this.data);
  }

  createForm(dest: any, index): FormGroup {
    let group: FormGroup;

    if(this.data) {
      group = this._fb.group({
        combinationTitle: [dest.combinationTitle],
        regionId: [dest.regionId],
        price: [],
        isActive: [false],
      });
    }
    return group;
  }

  getFormData(data): FormGroup {
    return data[0].region;
  }

  getRegionsCombinations(region) {
    let selectedRegion: IRegion[] = this.regions.filter((data) => data.id === region);
    let regionCombination = [];
    this.regions.forEach((region) => {
      this.regions.forEach((rData) => {
        regionCombination.push({
          combinationTitle: `${region.name} - ${rData.name}`,
          regionId: rData.id,
          price: 0
        });
      });
    });
    return regionCombination;
  }
6
  • or how can i create a simple table of 4*4 in this form with 4 columns and and each column must contain 4 rows Commented Oct 19, 2020 at 12:38
  • Use NgModel instead of Form Array it's much more easy. Commented Oct 19, 2020 at 13:37
  • @JohnPeters ngModel might be easier but currently the issue is with the layout of form that i need to change. Can you help me out? Commented Oct 19, 2020 at 18:03
  • Sorry, I didn't realize it was a layout issue. To fix use Html Grid with grid-template-colomns mark up. Commented Oct 19, 2020 at 21:51
  • @JohnPeters I am using angular material with flex css. What i need to do is that after each 4 textboxes i want the next 4 boxes in next column on the right side and not in a straight line like they are currently showing up. I am not a CSS ninja so little help with the code will help me a lot Commented Oct 20, 2020 at 5:54

0

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.