I have a Formarray that takes input from User and forms a part of JSON that needs to be hit through an POST Api.
The JSON structure is as follows:
{
"Name": "string",
"Time": 0,
"Values": [
{
"base": true,
"order": 0,
"query": "string",
"Id": "string",
"Path": "string",
"Type": "string"
},
{
"base": false,
"order": 1,
"query": "string",
"Id": "string",
"Path": "string",
"Type": "string"
}
]
}
Hence, I have defined a interface as follows:
pipeline-mapping.ts
import { PipelineConfigs } from './pipeline-configs';
export interface PipelineMapping {
configName: String;
queryTimeThresholdInMs: 0;
sources: PipelineConfigs[];
}
pipeline-configs.ts
export interface PipelineConfigs {
base: boolean;
order: any;
query: string;
Id: string;
Path: string;
Type: string;
}
Now, I am taking the input of query, Id, Path and Type from an formarray as defined in my component file below.
component.ts
public pipelineMapping: PipelineMapping;
pipelineConfigs: PipelineConfigs[];
ngOnInit() {
// initialize form here
this.pipeLineForm = this._fb.group({
pipelineConfigs: this.initPipeLineArray()
});
this.pipelineMapping = {
configName: '',
queryTimeThresholdInMs: 0,
sources: // What goes here ?
};
}
initPipeLineArray() {
let pipeArray = this._fb.array([]);
pipeArray.push(this.initPipeline());
pipeArray.push(this.initPipeline());
return pipeArray;
}
initPipeline() {
return this._fb.group({
query: ['', Validators.required],
Id: ['', Validators.required],
Path: ['', Validators.required],
Type: ['', Validators.required]
});
}
Question 1: How do I assign FormArray type to type PipelineConfigs ?
Question 2: Also, I am not sure how do I capture value of Order which is not an input, rather is just displayed on UI. base gets the value true if order is 0 else false Here is the relevant parts of the HTML :
html
<input class="form-control" name="Name" [(ngModel)]="pipelineMapping.Name" type="text">
<div class="panel panel-default clearfix" [formGroup] = "pipeLineForm">
<div class="row">
<div formArrayName="pipelineConfigs">
<div class="row" *ngFor="let pipes of pipeLineForm.controls.pipelineConfigs.controls; let i=index;">
<div [formGroupName]="i">
<select formControlName="Type">
<option disabled>Select Type</option>
<option value="Type 1">Type 1</option>
<option value="Type 2">Type 2</option>
<option value="Type 3">Type 3</option>
</select>
<input formControlName="Id" type="text">
<input formControlName="Path" type="text">
<textarea formControlName="query">{{query}}</textarea>
<div class="col-xs-2 additional-details-actions">
<div class="col-xs-4">
<label class="label-text block">Order</label>
<label class="label-value block">{{i}}</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
iwill do.