import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {NzModalService} from 'ng-zorro-antd/modal';
import {NzMessageService} from 'ng-zorro-antd';
@Component({
selector: 'app-plan',
templateUrl: './plan.component.html',
styleUrls: ['./plan.component.css']
})
export class PlanComponent implements OnInit {
constructor(private service: PlanService,
private modal: NzModalService,
private message: NzMessageService,
private viewContainerRef: ViewContainerRef) {
}
/**
* 创建编辑对话框
*/
createEditModal(plan: any): void {
const modal = this.modal.create({
nzTitle: '编辑计划',
// 对话框的内容,本例中为一个表单组件
nzContent: PlanUpdateComponent,
nzViewContainerRef: this.viewContainerRef,
// 父组件传递给对话框的数据
nzComponentParams: {
data: plan,
},
nzOnOk: (componentInstance: PlanUpdateComponent) => {
// 校验表单的正确性,若表单校验通过,则获取表单数据并调用api执行更新操作;否则返回false,不允许提交表单
const isValid: boolean = componentInstance.isValidForm();
if (isValid) {
const result: Plan = componentInstance.getFormData();
this.updatePlan(result.id, result);
return result;
}
return false;
}
});
// 当对话框开启时,打印日志
modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
// 当对话框关闭时,返回结果
modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result));
}
/**
* 调用后台api更新计划
* @param id 计划id
* @param updateData 更新数据
*/
updatePlan(id: string, updateData: Plan): void {
const messageId = this.message.loading('正在更新中...', {nzDuration: 0}).messageId;
this.service.updatePlan(id, updateData).subscribe(
// 若调用api成功,打印api返回值,提示创建更新成功,并刷新列表
(data) => {
console.log('next: ' + data);
this.message.remove(messageId);
this.message.success('更新成功!');
this.refreshData();
},
// 若调用api失败,打印失败日志,提示更新失败
(error) => {
console.log('error: ' + error);
this.message.error('更新失败!');
this.message.remove(messageId);
}
);
}
/**
* 刷新数据
*/
refreshData(): void {
this.listPlans();
}
}