All files / src/app/platform/analysis/analysis-list analysis-list.component.ts

24.66% Statements 18/73
0% Branches 0/12
4.35% Functions 1/23
25.81% Lines 16/62
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 106 107 108 109 110 111 1121x 1x   1x 1x 1x 1x             1x                         1x                             1x                 1x         1x                                                       1x       1x                 1x                   1x       1x  
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
 
import { NzModalService } from 'ng-zorro-antd';
import { AnalysisService } from '../analysis.service';
import { parseResponse } from '../../../shared/util';
import { AnalysisFormComponent } from '../analysis-form/analysis-form.component';
 
@Component({
  selector: 'analysis-list',
  templateUrl: './analysis-list.component.html',
  styleUrls: ['./analysis-list.component.less']
})
export class AnalysisListComponent implements OnInit {
  _allChecked = false;
  _disabledButton = true;
  _checkedNumber = 0;
  // _displayData: string[]= [];
  _dataSet = [];
  _indeterminate = false;
  _loading = false;
 
  constructor(private router: Router,
              private modalServ: NzModalService,
              private analysisServ: AnalysisService) { }
 
  addAnalysis(): void {
    const subscription = this.modalServ.open({
      title: '新建分析',
      content: AnalysisFormComponent,
      footer: false,
    });
 
    subscription.subscribe(result => {
      if (result === 'success') {
        subscription.destroy();
        this.getAnalysisList();
      }
    });
  }
 
  getAnalysisList(pageNum?: number, pageSize?: number): void {
    this._loading = true;
    this.analysisServ.getAnalysisList(pageNum, pageSize).subscribe(data => {
      const dataObj = parseResponse(data);
      this._dataSet = dataObj.data.results;
      this._loading = false;
    }, err => console.log(err));
  }
 
  toDetail(analysisId: number): void {
    console.log(analysisId);
    this.router.navigate([`${analysisId}`]);
  }
 
  deleteAnalysis(id?: number): void {
    const that = this;
    const ids = id ? [id] : [];
    this._dataSet.forEach((data, index) => {
      if (data.checked) {
        ids.push(data.id);
      }
    });
    if (ids.length === 0) {
      const modal = this.modalServ.warning({
        title: '请选择条目'
      });
      setTimeout(() => modal.destroy(), 1000);
      return;
    }
    this.modalServ.confirm({
      title: '确认要删除?',
      content: '删除后将无法恢复',
      showConfirmLoading: true,
      onOk() {
        that.analysisServ.deleteAnalysis(ids.join(',')).subscribe(data => {
          const dataObj = parseResponse(data);
          that.getAnalysisList();
        });
      }
    });
  }
 
  _displayDataChange($event: object[]) {
    // this._displayData = $event;
  }
 
  _checkAll(value: any) {
    if (value) {
      this._dataSet.forEach(data => data.checked = true);
    } else {
      this._dataSet.forEach(data => data.checked = false);
    }
    this._refreshStatus();
  }
 
  _refreshStatus() {
    const allChecked = this._dataSet.every(value => value.checked === true);
    const allUnChecked = this._dataSet.every(value => !value.checked);
    this._allChecked = allChecked;
    // 部分选中
    this._indeterminate = (!allChecked) && (!allUnChecked);
    this._disabledButton = !this._dataSet.some(value => value.checked);
    this._checkedNumber = this._dataSet.filter(value => value.checked).length;
  }
 
  ngOnInit() {
    this.getAnalysisList();
  }
 
}