I have successfully built a reactive form but I wish to order the results using a custom pipe:
<tr *ngFor="let record of myForm.controls.openingHoursForm.controls | sort: 'When'; let c=index; let first = first; let last = last;" [formGroupName]="c">
<td><input type="text" formControlName="Heading" class="openingTimesField" /></td>
<td><input type="text" formControlName="Subheading" class="openingTimesField" /></td>
<td><input type="text" formControlName="When" class="openingTimesField" /></td>
<td><input type="text" formControlName="Times" class="openingTimesField" /></td>
<td><input type="text" formControlName="Emphasis" class="openingTimesField" /></td>
</tr>
And my custom pipe looks like this:
import { Pipe } from "@angular/core";
@Pipe({
name: "sort"
})
export class ArraySortPipe {
transform(array: Array<string>, args: string): Array<string> {
if (array !== undefined) {
console.log(array);
array.sort((a: any, b: any) => {
if (a[args] < b[args]) {
return -1;
} else if (a[args] > b[args]) {
return 1;
} else {
return 0;
}
});
}
return array;
}
}
But this doesn't seem to work. I'm aware that reactive forms array structure are different to that of a basic array since it has a nested "controls" object. Any ideas how I can go about this?
UPDATE: For reference here is the form set-up as well:
this.myForm = this.fBuilder.group({
openingHoursForm: this.fBuilder.array([])
});
this.openingHoursArray.forEach(element => {
(<FormArray>this.myForm.get('openingHoursForm')).push(this.fBuilder.group({
Id: [element.Id],
Heading: [element.Heading],
Subheading: [element.Subheading],
When: [element.When],
Times: [element.Times],
Emphasis: [element.Emphasis],
SortOrder: [element.SortOrder]
}));
});