fengxiang
2018-01-04 72120bbe920425f80d3beb08c08af24151246006
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
60
61
62
63
64
65
66
67
68
69
import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalSubject } from 'ng-zorro-antd';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DeviceVersion } from 'app/routes/devices/version/version.component';
 
@Component({
  selector: 'app-version-edit',
  templateUrl: './version-edit.component.html',
  styles: []
})
export class VersionEditComponent 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]
     };
     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();
        } 
       const isModified =  Object.keys(value).some(
          (key: string) => {
              return this.data[key] !== value[key];    
          } 
        );
        // 未作修改
        if (!isModified) { 
          this.close();
          return;
        }
        this.isSaving = true;
        Object.keys(value).forEach( (key: string) => {
              if ( value[key] != null ) {
                   this.data[key] = value[key];
              }
        } );
        this.subject.next( this );
      }else {
        this.validate(); 
      }
  }
  validate() {
     for (const i in this.validateForm.controls) {
       this.validateForm.controls[ i ].markAsDirty();
     }
  }
}