So, I'm creating a user dialog that will fill a FormArray. Depending on the number assigned to it, it will populate a row. My problem is: how do you save temporary the data that they filled up? Is there any alternative beside in memory for saving temporary data?
ngOnInit(): void {
this.serialForm = this.fb.group({
id: [0],
deliveryReceiptDetailsId:[0],
itemCode:['', Validators.required],
qtyReceived:[0, Validators.required],
inputs: this.fb.array([this.CreateArray()]),
});
this.serialForm.get('qtyReceived').valueChanges.subscribe((res) => {
for (let i = this.displayArray.length; i < res; i++) {
this.displayArray.push(this.CreateArray());
console.log(this.displayArray);
}
});
this.dataValues = this.serialForm.get('id').setValue(this.data.id),
this.dataValues = this.serialForm.get('qtyReceived').setValue(this.data.qtyReceived),
this.dataValues = this.serialForm.get('itemCode').setValue(this.data.propertyNo)
}
get displayArray():FormArray{
return this.serialForm.get('inputs') as FormArray;
}
CreateArray():FormGroup{
return new FormGroup({
serialNum: new FormControl('', {validators: [Validators.required]}),
});
}
This is my service, where I tried to post it in memory:
@Injectable({
providedIn: 'root'
})
export class SerialCodeService {
private url = 'api/serial';
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
constructor(private http: HttpClient) { }
getHeroes (data:any): Observable<any> {
return this.http.post(this.url, data);
}
}
Here's where I tried to store the sample data:
Injectable({
providedIn: 'root'
})
export class InMemoryDataService implements InMemoryDbService {
createDb() {
const serial = [
{id: 1, deliveryReceiptDetailsId: 0, itemCode:'sample1', qtyReceived:0 ,serialNum:"" },
{id: 2, deliveryReceiptDetailsId: 0, itemCode:'sample2', qtyReceived:0 ,serialNum:"" },
{id: 3, deliveryReceiptDetailsId: 0, itemCode:'sample3', qtyReceived:0 ,serialNum:""},
];
return {serial};
}
genId(SerialBarCode: SerialCodeDTO[]): number{
return SerialBarCode.length > 0
? Math.max(...SerialBarCode.map(serial => serial.id)) + 1
: 1;
}
}