Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

danka-edit.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Component, inject } from '@angular/core';
  2. import {
  3. FormBuilder,
  4. FormGroup,
  5. FormControl,
  6. FormArray,
  7. ReactiveFormsModule,
  8. Validators,
  9. } from '@angular/forms';
  10. import { ActivatedRoute } from '@angular/router';
  11. import { AppHeader } from '../../share/header/app-header';
  12. import { AppSideMenu } from '../../share/side-menu/app-side-menu';
  13. import { DankaService } from '../../services/dankaService';
  14. import { Danka } from '../../models/danka';
  15. @Component({
  16. selector: 'app-danka-edit',
  17. imports: [AppHeader, AppSideMenu, ReactiveFormsModule],
  18. templateUrl: './danka-edit.html',
  19. styleUrl: './danka-edit.scss',
  20. })
  21. export class DankaEdit {
  22. danka: Danka | undefined;
  23. dankaForm = new FormGroup({
  24. householdName: new FormControl('鈴木家'),
  25. householder: new FormControl('鈴木 太郎'),
  26. postalCode: new FormControl('123-4567'),
  27. address: new FormControl('市内 1-2-3'),
  28. phones: new FormArray([
  29. this.createPhoneForm('03-xxxx-xxxx', '自宅'),
  30. this.createPhoneForm('090-xxxx-xxxx', '世帯主'),
  31. this.createPhoneForm('0584-xx-xxxx', '寺報連絡'),
  32. ]),
  33. });
  34. constructor(
  35. private dankaService: DankaService,
  36. private route: ActivatedRoute,
  37. ) {
  38. const id = this.route.snapshot.params['id'];
  39. if (id) {
  40. this.danka = this.dankaService.getDankaById(id);
  41. if (this.danka) {
  42. this.dankaForm.patchValue({
  43. householdName: this.danka.householdName,
  44. householder: this.danka.householder,
  45. postalCode: this.danka.postalCode,
  46. address: this.danka.address,
  47. });
  48. this.phones.clear();
  49. for (const phone of this.danka.phones) {
  50. this.phones.push(this.createPhoneForm(phone.tel, phone.note));
  51. }
  52. }
  53. }
  54. console.log(this.danka);
  55. }
  56. get phones() {
  57. return this.dankaForm.get('phones') as FormArray;
  58. }
  59. createPhoneForm(string = '', note = '') {
  60. return new FormGroup({
  61. tel: new FormControl(string),
  62. note: new FormControl(note),
  63. });
  64. }
  65. addPhone() {
  66. this.phones.push(this.createPhoneForm());
  67. }
  68. removePhone(index: number) {
  69. this.phones.removeAt(index);
  70. }
  71. saveDanka() {
  72. if (!this.danka) {
  73. return;
  74. }
  75. const formValue = this.dankaForm.value;
  76. const updatedDanka = {
  77. id: this.danka.id,
  78. householdName: formValue.householdName ?? '',
  79. householder: formValue.householder ?? '',
  80. postalCode: formValue.postalCode ?? '',
  81. address: formValue.address ?? '',
  82. phones: (formValue.phones ?? []).map((phone) => ({
  83. tel: phone.tel ?? '',
  84. note: phone.note ?? '',
  85. })),
  86. };
  87. this.dankaService.saveDanka(updatedDanka);
  88. console.log(this.dankaService.getDankaList());
  89. }
  90. }