Commit 466ca458 by Nattana Chaiyamat

ส่วนย่อย4

parent 348909e5
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit } from '@angular/core';
import { ColumnModel } from '@syncfusion/ej2-grids';
import { ToastrService } from 'ngx-toastr';
import { Bu6Model, MyBu6Model } from 'src/app/shared/model/bu6.model';
import { Bu7Model, MyBu7Model } from 'src/app/shared/model/bu7.model';
......@@ -18,25 +19,11 @@ interface table {
styleUrls: ['./sub-department-four.component.scss']
})
export class SubDepartmentFourComponent implements OnInit {
bu7List: { check: boolean, data: Bu7Model }[] = []
bu7ListLoading = false
bu7: Bu7Model = new MyBu7Model()
bu7Table: table = {
currentPage: 1,
page: Array.from({ length: 1 }, (_, i) => i + 1),
search: "",
pageSize: 10
}
bu7: { loading: boolean, select: Bu7Model, dataList: Bu7Model[] } = { loading: false, select: new MyBu7Model(), dataList: [] }
selectedFile: File | null = null;
selectedFileName: string = 'กรุณาเลือกไฟล์';
bu7Modal: table = {
currentPage: 1,
page: Array.from({ length: 1 }, (_, i) => i + 1),
search: "",
pageSize: 10
}
bu6List: Bu6Model[] = []
bu6: Bu6Model = new MyBu6Model()
bu6Modal: table = {
......@@ -47,9 +34,29 @@ export class SubDepartmentFourComponent implements OnInit {
}
currentModal: 'add' | 'edit' | 'delete' = "add"
numDataListChecked = 0
isDataListChecked = false
isDataListCheckedAll = false
columns: ColumnModel[] = [{
field: "bu7id",
headerText: "รหัสฝ่าย",
type: "string",
isPrimaryKey: true,
},
{
field: "tdesc",
headerText: "รายละเอียดฝ่าย(ไทย)",
type: "string"
},
{
field: "edesc",
headerText: "รายละเอียดฝ่าย(อังกฤษ)",
type: "string"
}]
searchSettings = {
fields: ['bu7id', 'tdesc', 'edesc'],
operator: 'contains',
ignoreCase: false
};
search = ''
selectedItems: { key: string, count: number, data: Map<string, boolean> } = { key: '', count: 0, data: new Map<string, boolean>() };
constructor(private bu7Service: Bu7Service,
private bu6Service: Bu6Service,
private toastr: ToastrService,
......@@ -73,7 +80,7 @@ export class SubDepartmentFourComponent implements OnInit {
}
const formData = new FormData();
formData.append('file', this.selectedFile);
this.bu7ListLoading = true
this.bu7.loading = true
this.fileService.uploadExcel(formData, 'mbu7').subscribe({
next: response => {
if (response.success) {
......@@ -81,11 +88,11 @@ export class SubDepartmentFourComponent implements OnInit {
this.getBu7List()
} else {
this.showAlert(response.message, 'error')
this.bu7ListLoading = false
this.bu7.loading = false
}
}, error: error => {
this.showAlert(error.message, 'error')
this.bu7ListLoading = false
this.bu7.loading = false
}
})
}
......@@ -110,76 +117,62 @@ export class SubDepartmentFourComponent implements OnInit {
}
getBu7List() {
this.bu7ListLoading = true
this.bu7.loading = true
this.bu7Service.getList().subscribe({
next: response => {
this.bu7List = response.map(x => ({ check: false, data: new MyBu7Model(x) }))
this.bu7ListLoading = false
this.onBu7TableSearchChange()
this.isDataListCheckedAll = false
this.dataListCheckAll()
this.bu7.dataList = response.map(x => {
this.selectedItems.data.set(x.bu7id, false)
return new MyBu7Model(x)
})
this.selectedItems.key = 'bu7id'
this.selectedItems.count = 0
this.bu7.loading = false
this.cdr.detectChanges()
}, error: error => {
this.bu7ListLoading = false
this.bu7.loading = false
this.cdr.detectChanges()
}
})
}
onBu7TableSearchChange() {
this.bu7Table.currentPage = 1
this.bu7Table.page = Array.from({ length: Math.ceil(this.filterBu7Table().length / 10) }, (_, i) => i + 1);
this.dataListCheck()
}
filterBu7Table() {
return this.bu7List.filter(x => {
const data = x.data
return data.bu7id.toLowerCase().includes(this.bu7Table.search.toLowerCase()) ||
data.tdesc.toLowerCase().includes(this.bu7Table.search.toLowerCase()) ||
data.edesc.toLowerCase().includes(this.bu7Table.search.toLowerCase())
})
}
selectBu7(bu7?: Bu7Model) {
this.bu7 = new MyBu7Model(bu7 || {})
if (bu7) {
this.bu7.select = new MyBu7Model(bu7)
} else if (this.currentModal == 'add') {
this.bu7.select = new MyBu7Model()
} else if (this.currentModal == 'edit') {
this.bu7.select = new MyBu7Model({ bu7id: this.bu7.select.bu7id })
}
this.selectBu6()
if (this.bu7.parent) {
this.bu6Service.getById(this.bu7.parent).subscribe(response => {
if (this.bu7.select.parent) {
this.bu6Service.getById(this.bu7.select.parent).subscribe(response => {
this.bu6 = new MyBu6Model(response)
this.cdr.detectChanges()
})
}
}
onBu7ModalSearchChange() {
this.bu7Modal.currentPage = 1
this.bu7Modal.page = Array.from({ length: Math.ceil(this.filterBu7Modal().length / 10) }, (_, i) => i + 1);
}
filterBu7Modal() {
return this.bu7List.filter(x => {
const data = x.data
return data.bu7id.toLowerCase().includes(this.bu7Table.search.toLowerCase()) ||
data.tdesc.toLowerCase().includes(this.bu7Table.search.toLowerCase()) ||
data.edesc.toLowerCase().includes(this.bu7Table.search.toLowerCase())
})
}
addBu7() {
this.bu7ListLoading = true
this.bu7Service.post({ ...this.bu7, parent: this.bu6.bu6id }).subscribe({
this.bu7.loading = true
this.bu7Service.post({ ...this.bu7.select, parent: this.bu6.bu6id }).subscribe({
next: response => {
if (response.success) {
this.showAlert(response.message, 'success')
this.getBu7List()
} else {
this.showAlert(response.message, 'error')
this.bu7ListLoading = false
this.bu7.loading = false
}
}, error: error => {
this.showAlert(error.message, 'error')
this.bu7ListLoading = false
this.bu7.loading = false
}
})
}
deleteBu7() {
this.bu7ListLoading = true
const body = this.bu7List.filter(x => x.check).map(x => new MyBu7Model(x.data))
this.bu7.loading = true
const selectedKeys = Array.from(this.selectedItems.data.keys());
const body = this.bu7.dataList.filter(x => selectedKeys.includes(x.bu7id) && this.selectedItems.data.get(x.bu7id)).map(x => new MyBu7Model(x))
this.bu7Service.delete(body).subscribe({
next: response => {
if (response.success) {
......@@ -187,11 +180,11 @@ export class SubDepartmentFourComponent implements OnInit {
this.getBu7List()
} else {
this.showAlert(response.message, 'error')
this.bu7ListLoading = false
this.bu7.loading = false
}
}, error: error => {
this.showAlert(error.message, 'error')
this.bu7ListLoading = false
this.bu7.loading = false
}
})
}
......@@ -226,21 +219,20 @@ export class SubDepartmentFourComponent implements OnInit {
})
}
dataListCheckAll() {
const selectAll = this.isDataListCheckedAll;
this.filterBu7Table().forEach(x => x.check = selectAll);
this.dataListCheck();
checkPrimary() {
return this.bu7.dataList.find(x => x.bu7id == this.bu7.select.bu7id)
}
dataListCheck() {
const dataCheck = this.filterBu7Table();
this.isDataListCheckedAll = dataCheck.length ? dataCheck.every(x => x.check) : false;
this.numDataListChecked = this.bu7List.filter(x => x.check).length
this.isDataListChecked = Boolean(this.numDataListChecked)
numSelectItem() {
const selectedKeys = Array.from(this.selectedItems.data.keys());
const num = this.bu7.dataList.filter(x => selectedKeys.includes(x.bu7id) && this.selectedItems.data.get(x.bu7id)).length
return num
}
checkPrimary() {
return this.bu7List.find(x => x.data.bu7id == this.bu7.bu7id)
onSelectItemChange(arg: any) {
this.selectedItems = arg
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment