// custom-form-table.component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators }
from '@angular/forms';
import { SelectItem } from 'primeng/api';
@Component({
selector: 'app-custom-form-table',
templateUrl: './custom-form-table.component.html',
styleUrls: ['./custom-form-table.component.css']
})
export class CustomFormTableComponent implements OnInit {
data: any[] = [
{ id: 1,
name: 'Shrutin',
age: 28,
status: 'active' },
{ id: 2,
name: 'Rishabh',
age: 24,
status: 'inactive' },
{ id: 3,
name: 'Nkhil',
age: 32,
status: 'pending' }
];
columns: any[] = [
{ field: 'name', header: 'Name' },
{ field: 'age', header: 'Age' },
{ field: 'status', header: 'Status' }
];
statusOptions: SelectItem[] = [
{ label: 'Active', value: 'active' },
{ label: 'Inactive', value: 'inactive' },
{ label: 'Pending', value: 'pending' }
];
dataForm!: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit(): void {
this.dataForm = this.fb.group({
name: ['', Validators.required],
age: ['', Validators.required],
status: ['active']
});
}
onSubmit() {
if (this.dataForm.valid) {
const formData = this.dataForm.value;
const newId = this.data.length + 1;
this.data.push({
id: newId,
name: formData.name,
age: formData.age,
status: formData.status
});
this.dataForm.reset();
}
}
// Function to handle changes
// in the status dropdown
onStatusChange(event: any, rowIndex: number) {
this.data[rowIndex].status = event.value;
}
}