import { SelectionModel } from '@angular/cdk/collections'; import { Component, Inject, OnInit } from '@angular/core'; import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { MatTableDataSource } from '@angular/material/table'; import { NzFormatEmitEvent } from 'ng-zorro-antd/tree'; @Component({ selector: 'app-create-test-score', templateUrl: './create-test-score.component.html', styleUrls: ['./create-test-score.component.scss'] }) export class CreateTestScoreComponent implements OnInit { constructor(public dialog: MatDialog) { } selectedTab:number = 1 //选中的选项卡 selectTab(index){ this.selectedTab = index } ngOnInit(): void { } //模拟单位数据 unitDatas:any = [ {name:'富丽华大酒店1',score:0,basicInfoScore:0,basicInfoItemScore:0,basicInfoNodes : [ { name: '单位信息', key: '0-0', type:'基本信息', expanded: true, children: [ {name: '统一社会信用代码',key: '1',isLeaf:true}, {name: '单位类型',key: '2',isLeaf:true}, {name: '联系人',key: '3',isLeaf:true}, {name: '联系电话',key: '4',isLeaf:true}, {name: '辖区中队',key: '5',isLeaf:true}, {name: '单位地址',key: '6',isLeaf:true} ] }, { name: '建筑信息', key: '0-1', type:'基本信息', expanded: true, children: [ { name: '面积', key: '0-1-0-0',isLeaf:true}, { name: '高度', key: '0-1-0-1',isLeaf:true}, { name: '层数', key: '0-1-0-2',isLeaf:true} ] }, ],aroundScore:0,aroundItemScore:0,aroundNodes :[ { name: '单位四周毗邻', key: '11', expanded: true, type:'四周毗邻', children: [ {name: '东方向',key: '55',isLeaf:true}, ] }, { name: '建筑四周毗邻', key: '22', expanded: true, type:'四周毗邻', children: [ {name: '东方向',key: '55',isLeaf:true}, ] }, ]}, // {name:'富丽华大酒店2',score:0,basicInfoScore:0} ] unitId:any = [{name:'富丽华大酒店1'}] //获取单位数据 getUnitData(){ } //阻止input事件冒泡 stopPropagation($event){ console.log($event) $event.stopPropagation() } //模拟预案数据 planDatas:any = [ {name:'5楼电路故障灾情扑救',or:'上海总队',time:'2020-09-24',isOpen:'未公开',level:'总队',scroe:0}, {name:'6楼电路故障灾情扑救',or:'上海总队',time:'2020-09-24',isOpen:'未公开',level:'总队',scroe:0}, {name:'7楼电路故障灾情扑救',or:'上海总队',time:'2020-09-24',isOpen:'未公开',level:'总队',scroe:0} ] defaultCheckedKeys = []; //指定选中复选框的树节点 key值 defaultExpandedKeys = []; //展开指定的树节点 key值 defaultSelectedKeys = []; //指定选中的树节点 key值 examScore:any = 0//整个试卷的总分 //计算分数 calculateScore(key){ //key代表第几个建筑 //计算整个单位的总分 this.unitDatas[key].score = this.unitDatas[key].basicInfoScore + this.unitDatas[key].aroundScore //计算整个试卷的总分 let examScore = 0 this.unitDatas.forEach(element => { examScore += Number(element.score) }) this.examScore = examScore } //tree的选择事件 nzEvent(event: NzFormatEmitEvent,key): void { if(event.node.origin.type == '基本信息' || (event.node.parentNode && event.node.parentNode.origin.type == '基本信息')){ let selectedNum = [] this.unitDatas[key].basicInfoNodes.forEach(item => { item.children.forEach(i => { i.checked ? selectedNum.push(i) : '' }) }) if(selectedNum.length != 0 ){ this.unitDatas[key].basicInfoScore ? this.unitDatas[key].basicInfoItemScore = this.unitDatas[key].basicInfoScore / selectedNum.length : null }else{ this.unitDatas[key].basicInfoItemScore = 0 } } if(event.node.origin.type == '四周毗邻' || (event.node.parentNode && event.node.parentNode.origin.type == '四周毗邻')){ let selectedNum = [] this.unitDatas[key].aroundNodes.forEach(item => { item.children.forEach(i => { i.checked ? selectedNum.push(i) : '' }) }) console.log(7894,selectedNum) if(selectedNum.length != 0 ){ this.unitDatas[key].aroundScore ? this.unitDatas[key].aroundItemScore = this.unitDatas[key].aroundScore / selectedNum.length : null }else{ this.unitDatas[key].aroundItemScore = 0 } } } //单位各项总分数动态计算 sumScore(type,key){ //key代表第几个建筑 type代表哪个部分 if(type == '基本信息'){ let selectedNum = 0 this.unitDatas[key].basicInfoNodes.forEach(item => { item.children.forEach(i => { i.checked ? selectedNum++ : '' }) }) if(selectedNum != 0){ this.unitDatas[key].basicInfoItemScore = this.unitDatas[key].basicInfoScore / selectedNum }else{ this.unitDatas[key].basicInfoItemScore = 0 } } if(type == '四周毗邻'){ let selectedNum = 0 this.unitDatas[key].aroundNodes.forEach(item => { item.children.forEach(i => { i.checked ? selectedNum++ : '' }) }) if(selectedNum != 0){ this.unitDatas[key].aroundItemScore = this.unitDatas[key].aroundScore / selectedNum }else{ this.unitDatas[key].aroundItemScore = 0 } } this.calculateScore(key)//更新标题栏分数 } //添加预案 addPlan(){ const dialogRef = this.dialog.open(AddPlanDialog, { width: '380px', height: '136px', data: {} }); dialogRef.afterClosed().subscribe(result => { console.log('The dialog was closed'); }); } //每条预案分数增加在试卷分数 planItemScore(item){ this.examScore += item.score } } //添加预案1 @Component({ selector: 'addplan-dialog', templateUrl: 'addPlan.html', styleUrls: ['addPlan.scss'] }) export class AddPlanDialog { constructor(public dialog: MatDialog,public dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) public data: any) {} onNoClick(): void { this.dialogRef.close(); } close(){ this.dialogRef.close(); } //选择想要添加的预案类型 (已存在或者自定义) selectType(type){ const dialogRef = this.dialog.open(AddPlanTwoDialog, { id:'addPlanTwo', data: {type} }); dialogRef.afterClosed().subscribe(result => { console.log('The dialog was closed'); }); } } const ELEMENT_DATA: any = [ {id: 1, name: '五楼电梯故障', people:'上海总队',time:'2020-06-15',level:'总队'}, {id: 2, name: '故障搜救', people:'超级管理员',time:'2020-06-15',level:'总队'}, {id: 3, name: '故障搜救', people:'超级管理员',time:'2020-06-15',level:'总队'}, {id: 4, name: '五楼电梯故障', people:'上海总队',time:'2020-06-15',level:'总队'}, ]; //添加预案2 @Component({ selector: 'addplantwo-dialog', templateUrl: 'addPlanTwo.html', styleUrls: ['addPlanTwo.scss'] }) export class AddPlanTwoDialog { constructor(public dialog: MatDialog,public dialogRef: MatDialogRef,@Inject(MAT_DIALOG_DATA) public data: any) {} planType:any = this.data.type == 1 ? true : false//显示的预案类型 displayedColumns: string[] = ['select', 'name', 'people', 'time', 'level']; dataSource = new MatTableDataSource(ELEMENT_DATA); selection = new SelectionModel(true, []); isAllSelected() { const numSelected = this.selection.selected.length; const numRows = this.dataSource.data.length; return numSelected === numRows; } masterToggle() { this.isAllSelected() ? this.selection.clear() : this.dataSource.data.forEach(row => this.selection.select(row)); } ngOnInit(): void { } onNoClick(): void { this.dialogRef.close(); } //确定 confirm(){ if(this.planType == 1){ console.log(this.selection) }else{ } } //取消 close(){ this.dialogRef.close(); } }