沈斌
2018-03-08 f320f1970e5d80a423f377fb3253628bd1e7f5bf
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
import { NzModalSubject } from 'ng-zorro-antd';
import { Sensor } from '@business/entity/data';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { SensorsService } from '@business/services/http/sensors.service';
 
@Component({
  selector: 'app-sensor-edit',
  templateUrl: './sensor-edit.component.html',
  styles: []
})
export class SensorEditComponent implements OnInit {
  data: Sensor;
  isSaving = false;
  validateForm: FormGroup;
  constructor(    
    private subject: NzModalSubject,
    private formBuilder: FormBuilder,
    private organizationService: SensorsService
    ) { }
 
  ngOnInit() {
     const data = this.data;
     const validates: Sensor  = {
          name: [data.name, [Validators.required] ],
          sensorKey: [data.sensorKey, [Validators.required] ],
          lower: [data.lower ],
          upper: [data.upper ],
          unit: [data.unit ],
          description: [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();
        } 
        this.isSaving = true;
        Object.keys(value).forEach( (key: string) => {
              // '_'为前缀的为自定义属性
              if (!key.startsWith('_') && 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();
     }
  }
  
 
}