fengxiang
2018-01-10 3517e796f650b8aed52165c1a5905456f54033ef
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { NzModalSubject } from 'ng-zorro-antd';
import { Component, OnInit } from '@angular/core';
import { SensorsService } from '@business/services/http/sensors.service';
import { PageBean,  Grid } from '@business/entity/grid';
import { Sensor } from '@business/entity/data';
import { Subject } from 'rxjs/Subject';
 
@Component({
  selector: 'app-version-sensor-config',
  templateUrl: './version-sensor-config.component.html',
  styles: []
})
export class VersionSensorConfigComponent implements OnInit {
  deviceVersionId: number;
  selectedSensorIds: number[];
  isSaving = false;
  grid: Grid<Sensor> = new Grid(null);
  private initPage() {
    const sensor: Sensor = {
      name: {
        text: '名称',
        width: '200px'
      },
      key: {
        text: '键值',
        width: '60px'
      },
      lower: {
        text: '下限值',
        width: '90px'
      },
      upper: {
        text: '上限值',
        width: '90px'
      },
      unit: {
        text: '单位',
        width: '100px'
      },
      description: {
        text: '描述'
      }
    };
    this.grid.title = '传感器';
    this.grid.setColumns(sensor);
    this.grid.pageSize = 0;
  }
  constructor(
    private subject: NzModalSubject,
    private sensorsService: SensorsService,
  ) { }
 
  ngOnInit() {
    this.initPage();
    this.load();
  }
  load() {
    // 延时加载避免ExpressionChangedAfterItHasBeenCheckedError
    setTimeout(() => {
      this.grid.loading = true;
    }, 1);
    this.sensorsService.getPagingList(this.grid, null).subscribe(
      (res: PageBean) => {
        this.grid.loading = true;
        if (res != null && res.data != null) {
          this.grid.initData(res);
          this.grid.data.map(
            (row: any) => {
               row['checked'] = this.selectedSensorIds.filter(
                (id: number) => {
                  return row.id === id;
                }
              ).length > 0;
            }
          );
          this.grid.refreshStatus();
          setTimeout(() => {
            this.grid.loading = false;
          }, 1);
        }
      }
    );
  }
  close() {
    this.subject.destroy();
  }
  save($event, value, valid) {
    $event.preventDefault();
    this.subject.next( this );
  }
 
  sort(field: string, value: string) {
    // 删除当前field
    this.grid.sorts = this.grid.sorts.filter(
      (fn: string) => {
        return fn !== field;
      }
    );
    // 如果value不为null,在排序数组最后加上filed
    if (value != null) {
      this.grid.sorts.push(field);
    }
    this.load();
  }
}