fengxiang
2017-12-28 55da782025f5728051fea9fff49f9e6b6f602a1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { DeviceVersion } from './../version.component';
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalSubject } from 'ng-zorro-antd';
import { DateService } from '@core/services/date.service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
 
@Component({
  selector: 'app-add-or-edit',
  templateUrl: './add-or-edit.component.html',
  styles: []
})
export class AddOrEditComponent implements OnInit {
 
  cols: DeviceVersion;
 
  data: DeviceVersion;
  isSaving = false;
  validateForm: FormGroup;
  constructor(    
    private subject: NzModalSubject,
    private formBuilder: FormBuilder
    ) { }
 
  ngOnInit() {
     const validates: DeviceVersion = {
          name: [this.data.name, [Validators.required] ],
          version: [this.data.version == null ? 1 : this.data.version, [Validators.required] ],
          createTime: [this.data.createTime, [Validators.required] ],
          description: [this.data.description, [Validators.required] ]
     };
     this.validateForm = this.formBuilder.group(
      validates
     );
  }
  close() {
       this.subject.destroy();
  }
  save($event, value, valid) {
      $event.preventDefault();
      if (valid) {
        for (const i in this.validateForm.controls) {
          this.validateForm.controls[ i ].disable();
        } 
        this.isSaving = true;
        this.data = value;
        this.subject.next( this );
      }else {
          for (const i in this.validateForm.controls) {
          this.validateForm.controls[ i ].markAsDirty();
         } 
      }
  }
  validate() {
     for (const i in this.validateForm.controls) {
       this.validateForm.controls[ i ].markAsDirty();
     }
  }
}