Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { FormGroup, Validators, FormControl } from '@angular/forms';
  3.  
  4. interface Person{
  5. id: number,
  6. name: string,
  7. value: string,
  8. editMode: boolean
  9. }
  10.  
  11. @Component({
  12. selector: 'app-root',
  13. template: `
  14.  
  15. <form [formGroup]="form">
  16. <table style="border: 1px solid black">
  17. <thead>
  18. <tr>
  19. <td>Name</td>
  20. <td>Value</td>
  21. <td></td>
  22. </tr>
  23. </thead>
  24. <tbody>
  25. <tr *ngFor="let object of obj">
  26. <td>
  27. <label *ngIf="!object.editMode">{{ object.name }} </label>
  28. <input *ngIf="object.editMode" type="text" [name]="getName(object.id)" [id]="getName(object.id)" [value]="object.name" [formControlName]="getName(object.id)"/>
  29. <p *ngIf="getValidateName(object.id).invalid">
  30. Invalid Name
  31. </p>
  32. </td>
  33. <td>
  34. <label *ngIf="!object.editMode">{{ object.value }}</label>
  35. <input *ngIf="object.editMode" type="text" [name]="getValue(object.id)" [id]="getValue(object.id)" [value]="object.value" [formControlName]="getValue(object.id)" />
  36. <p *ngIf="getValidateValue(object.id).invalid">
  37. Invalid Value
  38. </p>
  39. </td>
  40. <td><input type="button" value="edit" (click)="onEdit(object)" [disabled]="getValidateName(object.id).invalid || getValidateValue(object.id).invalid"></td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. </form>
  45.  
  46. <p>Form Value: {{ form.value | json }}</p>
  47.  
  48. `
  49. })
  50. export class AppComponent {
  51. form: FormGroup;
  52.  
  53. obj: Array<Person> = [
  54. {id:2,name: 'John', value: 'John value',editMode : false},
  55. {id:3,name: 'Sara', value: 'Sara value',editMode : false},
  56. {id:4,name: 'Moss', value: 'Moss value',editMode : false}
  57. ];
  58.  
  59. private readonly NAME_PREFIX = 'name-';
  60. private readonly VAL_PREFIX = 'value-'
  61.  
  62. public formInit(){
  63. this.form = new FormGroup({});
  64.  
  65. this.obj.forEach(x => {
  66. const controlId = x.id;
  67. const nameValidator = [Validators.required];
  68. const valValidator = [Validators.required,Validators.minLength(5)];
  69. this.form.addControl(this.NAME_PREFIX+controlId, new FormControl(x.name, nameValidator));
  70. this.form.addControl(this.VAL_PREFIX+controlId, new FormControl(x.value, valValidator));
  71. });
  72.  
  73. }
  74.  
  75. ngOnInit() {
  76. this.formInit();
  77. }
  78.  
  79. getName(id: string): string {
  80. return this.NAME_PREFIX+id;
  81. }
  82. getValue(id: string): string {
  83. return this.VAL_PREFIX+id;
  84. }
  85.  
  86. getValidateName(id: string): FormControl{
  87. return this.form.get(this.NAME_PREFIX+id) as FormControl;
  88. }
  89.  
  90. getValidateValue(id: string): FormControl{
  91. return this.form.get(this.VAL_PREFIX+id) as FormControl;
  92. }
  93.  
  94. public onEdit(p: Person){
  95. const target = this.obj.find(x => x.name == p.name);
  96.  
  97. target.editMode = !target.editMode;
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement