/
app.component.ts
114 lines (103 loc) · 2.98 KB
/
app.component.ts
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
112
113
114
import { Component, ViewChild } from '@angular/core';
import { DxDataGridComponent } from "devextreme-angular";
import DataSource from "devextreme/data/data_source"
import { Service } from './app.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [Service]
})
export class AppComponent {
@ViewChild("mainGrid", { static: false }) mainGrid: any = DxDataGridComponent;
title = 'project';
dataSource: any;
allValid: any;
subjects: any = [];
rowData: any = [];
constructor(service: Service) {
this.dataSource = new DataSource({
store: {
type: "array",
key: "ID",
data: service.getEmployees()
}
});
}
onInitNewRow(e: any) {
this.subjects = [];
e.data.Subjects = this.subjects;
}
onEditingStart(e: any) {
this.rowData["key"] = e.key;
this.subjects = e.data.Subjects.slice();
}
onEditorPreparing(e: any) {
if (e.dataField === "Name" && e.parentType === "dataRow") {
this.rowData["name"] = e.row.data.Name;
this.allValid = !e.row.data.Name || e.row.data.Name === "" && e.row.data.hasOwnProperty("Name") || this.subjects.length === 0;
}
}
onEditorPreparingSub(e: any) {
if (e.parentType === "dataRow") {
if (!e.row.data.SubjectCode || e.row.data.SubjectCode === "" ||
!e.row.data.SubjectName || e.row.data.SubjectName === "" ||
!e.row.data.Section || e.row.data.Section === "" || e.component.hasEditData())
this.allValid = true;
else
this.allValid = false;
}
}
customizeText(cellInfo: any) {
if (cellInfo.value) {
let cellText = "";
cellInfo.value.forEach((subject: any) => {
cellText += subject.SubjectName + ',';
})
return cellText.slice(0, cellText.length - 1);
}
return cellInfo.valueText;
}
setCellValue(newData: any, value: any) {
let column = (<any>this);
column.defaultSetCellValue(newData, value);
}
saveButton = () => {
this.mainGrid.instance.saveEditData();
this.mainGrid.instance.refresh();
}
cancelButton = () => {
this.mainGrid.instance.cancelEditData();
}
onRowValidating(e: any) {
this.allValid = e.brokenRules.length > 0 ? true : false;
}
onRowRemoved(e: any) {
setTimeout(() => {
this.allValid = this.subjects.length === 0 ? true : false;
})
}
onSaving(e: any) {
if (e.changes[0]) {
e.changes[0] = {
data: {
ID: this.rowData["key"],
Name: this.rowData["name"],
Subjects: this.subjects
},
key: this.rowData["key"],
type: "update"
}
} else {
e.changes.push({
data: {
ID: this.rowData["key"],
Name: this.rowData["name"],
Subjects: this.subjects
},
key: this.rowData["key"],
type: "update"
})
}
}
}