ENI Process Project Angular
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0KB

  1. import { HttpErrorResponse } from '@angular/common/http';
  2. import { Component, OnInit } from '@angular/core';
  3. import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
  4. import { Observable, Subject } from 'rxjs';
  5. import { takeUntil } from 'rxjs/operators';
  6. import { ApiService } from 'src/app/_services';
  7. @Component({
  8. selector: 'app-veriable-form',
  9. templateUrl: './veriable-form.component.html',
  10. styleUrls: ['./veriable-form.component.sass']
  11. })
  12. export class VeriableFormComponent implements OnInit {
  13. form: FormGroup;
  14. // Sample variable list for dropdown
  15. variableList = ['Variable 1', 'Variable 2', 'Variable 3'];
  16. public componentDestroyed = new Subject();
  17. rdsValues: any;
  18. selectedParamValue: any = {};
  19. inputValue: any;
  20. constructor(private fb: FormBuilder, private api: ApiService) {
  21. this.form = this.fb.group({
  22. rows: this.fb.array([]) // Form array for multiple rows
  23. });
  24. }
  25. ngOnInit(): void {
  26. // Initialize with one row
  27. this.addRow();
  28. this.getVarbaleValusData();
  29. }
  30. get rows(): FormArray {
  31. return this.form.get('rows') as FormArray;
  32. }
  33. getColumns(row: any): FormArray {
  34. return row.get('columns') as FormArray;
  35. }
  36. isText(row: any, colIndex: number): boolean {
  37. const columns = this.getColumns(row);
  38. return columns.at(colIndex).get('type')?.value === 'text';
  39. }
  40. isVariable(row: any, colIndex: number): boolean {
  41. const columns = this.getColumns(row);
  42. return columns.at(colIndex).get('type')?.value === 'variable';
  43. }
  44. createColumn(): FormGroup {
  45. return this.fb.group({
  46. type: [''], // Type can be 'text' or 'variable'
  47. textValue: [''], // For text input
  48. variableValue: [''] // For variable select
  49. });
  50. }
  51. addColumn(rowIndex: number): void {
  52. const row = this.rows.at(rowIndex).get('columns') as FormArray;
  53. row.push(this.createColumn());
  54. }
  55. addRow(): void {
  56. const row = this.fb.group({
  57. columns: this.fb.array([this.createColumn()]) // Initialize with one column
  58. });
  59. this.rows.push(row);
  60. }
  61. getVarbaleValusData(){
  62. const imei = 869009061127230
  63. this.api.getVariableValus(imei)
  64. .pipe(takeUntil(this.componentDestroyed))
  65. .subscribe((res:any)=>{
  66. this.rdsValues = res.resultObject
  67. },(error:HttpErrorResponse)=>{
  68. });
  69. }
  70. onParamChange(event: any, rowIndex: number, colIndex: number) {
  71. const selectedParam = event.target.value;
  72. for (let item of this.rdsValues.modbus) {
  73. if (item.hasOwnProperty(selectedParam)) {
  74. // Initialize the row if not present
  75. if (!this.selectedParamValue[rowIndex]) {
  76. this.selectedParamValue[rowIndex] = {};
  77. }
  78. // Assign the selected param value to the specific row and column
  79. this.selectedParamValue[rowIndex][colIndex] = item[selectedParam];
  80. break;
  81. }
  82. }
  83. this.inputValue = this.selectedParamValue[rowIndex][colIndex]
  84. console.log(`Row: ${rowIndex}, Column: ${colIndex}, Value:`, this.selectedParamValue[rowIndex][colIndex]);
  85. }
  86. onSubmit(): void {
  87. console.log(this.form.value);
  88. }
  89. }