import { Component, OnInit, Input } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { NzModalSubject } from 'ng-zorro-antd'; import { System, Table } from '../../shared/platform.model'; import { PlatformService } from '../../shared/platform.service'; import { AnalysisService } from '../shared/analysis.service'; @Component({ selector: 'analysis-form', templateUrl: './analysis-form.component.html', styleUrls: ['./analysis-form.component.less'] }) export class AnalysisFormComponent implements OnInit { validateForm: FormGroup; systems: System[]; currentSystem: System; tables: Table[]; currentTableId: number; loading = false; // 编辑分析传过来的值 @Input() id: number; @Input() name: string; @Input() description: string; @Input() dataBaseId: number; @Input() tableId: number; constructor( private fb: FormBuilder, private platformServ: PlatformService, private analysisServ: AnalysisService, private subject: NzModalSubject) { } onSubmit() { // 去掉databaseId属性 const { databaseId, ...submitValue } = this.validateForm.value; for (const i in this.validateForm.controls) { this.validateForm.controls[i].markAsDirty(); } if (!this.validateForm.valid) return; // 编辑分析 if (this.id) { this.analysisServ.updateAnalysis(this.id, submitValue).subscribe(data => { this.subject.next('success'); }); // 新增分析 } else { this.analysisServ.addAnalysis(submitValue).subscribe(data => { this.subject.next('success'); }); } } handleCancel() { this.subject.destroy(); } getFormControl(name: string) { return this.validateForm.get(name); } ngOnInit() { this.validateForm = this.fb.group({ name: [this.name, [Validators.required]], description: [this.description], databaseId: [this.dataBaseId], tableId: [this.tableId, [Validators.required]], }); this.getSystemList(); } getSystemList() { this.loading = true; this.platformServ.getSystemList().subscribe(result => { this.systems = result.data; this.loading = false; // 编辑分析情况,放在这里增加loading体验效果 if (this.dataBaseId) this.getTableList(this.dataBaseId); }); } getTableList(systemId: number) { this.loading = true; this.platformServ.getTableList((systemId)) .subscribe(data => { this.tables = data.data; this.currentTableId = this.tableId || this.tables[0].id; this.loading = false; }); } }