Commit 466ca458 by Nattana Chaiyamat

ส่วนย่อย4

parent 348909e5
import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit } from '@angular/core'; import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit } from '@angular/core';
import { ColumnModel } from '@syncfusion/ej2-grids';
import { ToastrService } from 'ngx-toastr'; import { ToastrService } from 'ngx-toastr';
import { Bu6Model, MyBu6Model } from 'src/app/shared/model/bu6.model'; import { Bu6Model, MyBu6Model } from 'src/app/shared/model/bu6.model';
import { Bu7Model, MyBu7Model } from 'src/app/shared/model/bu7.model'; import { Bu7Model, MyBu7Model } from 'src/app/shared/model/bu7.model';
...@@ -18,25 +19,11 @@ interface table { ...@@ -18,25 +19,11 @@ interface table {
styleUrls: ['./sub-department-four.component.scss'] styleUrls: ['./sub-department-four.component.scss']
}) })
export class SubDepartmentFourComponent implements OnInit { export class SubDepartmentFourComponent implements OnInit {
bu7List: { check: boolean, data: Bu7Model }[] = [] bu7: { loading: boolean, select: Bu7Model, dataList: Bu7Model[] } = { loading: false, select: new MyBu7Model(), dataList: [] }
bu7ListLoading = false
bu7: Bu7Model = new MyBu7Model()
bu7Table: table = {
currentPage: 1,
page: Array.from({ length: 1 }, (_, i) => i + 1),
search: "",
pageSize: 10
}
selectedFile: File | null = null; selectedFile: File | null = null;
selectedFileName: string = 'กรุณาเลือกไฟล์'; selectedFileName: string = 'กรุณาเลือกไฟล์';
bu7Modal: table = {
currentPage: 1,
page: Array.from({ length: 1 }, (_, i) => i + 1),
search: "",
pageSize: 10
}
bu6List: Bu6Model[] = [] bu6List: Bu6Model[] = []
bu6: Bu6Model = new MyBu6Model() bu6: Bu6Model = new MyBu6Model()
bu6Modal: table = { bu6Modal: table = {
...@@ -47,9 +34,29 @@ export class SubDepartmentFourComponent implements OnInit { ...@@ -47,9 +34,29 @@ export class SubDepartmentFourComponent implements OnInit {
} }
currentModal: 'add' | 'edit' | 'delete' = "add" currentModal: 'add' | 'edit' | 'delete' = "add"
numDataListChecked = 0 columns: ColumnModel[] = [{
isDataListChecked = false field: "bu7id",
isDataListCheckedAll = false 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, constructor(private bu7Service: Bu7Service,
private bu6Service: Bu6Service, private bu6Service: Bu6Service,
private toastr: ToastrService, private toastr: ToastrService,
...@@ -73,7 +80,7 @@ export class SubDepartmentFourComponent implements OnInit { ...@@ -73,7 +80,7 @@ export class SubDepartmentFourComponent implements OnInit {
} }
const formData = new FormData(); const formData = new FormData();
formData.append('file', this.selectedFile); formData.append('file', this.selectedFile);
this.bu7ListLoading = true this.bu7.loading = true
this.fileService.uploadExcel(formData, 'mbu7').subscribe({ this.fileService.uploadExcel(formData, 'mbu7').subscribe({
next: response => { next: response => {
if (response.success) { if (response.success) {
...@@ -81,11 +88,11 @@ export class SubDepartmentFourComponent implements OnInit { ...@@ -81,11 +88,11 @@ export class SubDepartmentFourComponent implements OnInit {
this.getBu7List() this.getBu7List()
} else { } else {
this.showAlert(response.message, 'error') this.showAlert(response.message, 'error')
this.bu7ListLoading = false this.bu7.loading = false
} }
}, error: error => { }, error: error => {
this.showAlert(error.message, 'error') this.showAlert(error.message, 'error')
this.bu7ListLoading = false this.bu7.loading = false
} }
}) })
} }
...@@ -110,76 +117,62 @@ export class SubDepartmentFourComponent implements OnInit { ...@@ -110,76 +117,62 @@ export class SubDepartmentFourComponent implements OnInit {
} }
getBu7List() { getBu7List() {
this.bu7ListLoading = true this.bu7.loading = true
this.bu7Service.getList().subscribe({ this.bu7Service.getList().subscribe({
next: response => { next: response => {
this.bu7List = response.map(x => ({ check: false, data: new MyBu7Model(x) })) this.bu7.dataList = response.map(x => {
this.bu7ListLoading = false this.selectedItems.data.set(x.bu7id, false)
this.onBu7TableSearchChange() return new MyBu7Model(x)
this.isDataListCheckedAll = false })
this.dataListCheckAll() this.selectedItems.key = 'bu7id'
this.selectedItems.count = 0
this.bu7.loading = false
this.cdr.detectChanges() this.cdr.detectChanges()
}, error: error => { }, error: error => {
this.bu7ListLoading = false this.bu7.loading = false
this.cdr.detectChanges() 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) { 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() this.selectBu6()
if (this.bu7.parent) { if (this.bu7.select.parent) {
this.bu6Service.getById(this.bu7.parent).subscribe(response => { this.bu6Service.getById(this.bu7.select.parent).subscribe(response => {
this.bu6 = new MyBu6Model(response) this.bu6 = new MyBu6Model(response)
this.cdr.detectChanges() 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() { addBu7() {
this.bu7ListLoading = true this.bu7.loading = true
this.bu7Service.post({ ...this.bu7, parent: this.bu6.bu6id }).subscribe({ this.bu7Service.post({ ...this.bu7.select, parent: this.bu6.bu6id }).subscribe({
next: response => { next: response => {
if (response.success) { if (response.success) {
this.showAlert(response.message, 'success') this.showAlert(response.message, 'success')
this.getBu7List() this.getBu7List()
} else { } else {
this.showAlert(response.message, 'error') this.showAlert(response.message, 'error')
this.bu7ListLoading = false this.bu7.loading = false
} }
}, error: error => { }, error: error => {
this.showAlert(error.message, 'error') this.showAlert(error.message, 'error')
this.bu7ListLoading = false this.bu7.loading = false
} }
}) })
} }
deleteBu7() { deleteBu7() {
this.bu7ListLoading = true this.bu7.loading = true
const body = this.bu7List.filter(x => x.check).map(x => new MyBu7Model(x.data)) 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({ this.bu7Service.delete(body).subscribe({
next: response => { next: response => {
if (response.success) { if (response.success) {
...@@ -187,11 +180,11 @@ export class SubDepartmentFourComponent implements OnInit { ...@@ -187,11 +180,11 @@ export class SubDepartmentFourComponent implements OnInit {
this.getBu7List() this.getBu7List()
} else { } else {
this.showAlert(response.message, 'error') this.showAlert(response.message, 'error')
this.bu7ListLoading = false this.bu7.loading = false
} }
}, error: error => { }, error: error => {
this.showAlert(error.message, 'error') this.showAlert(error.message, 'error')
this.bu7ListLoading = false this.bu7.loading = false
} }
}) })
} }
...@@ -226,21 +219,20 @@ export class SubDepartmentFourComponent implements OnInit { ...@@ -226,21 +219,20 @@ export class SubDepartmentFourComponent implements OnInit {
}) })
} }
dataListCheckAll() {
const selectAll = this.isDataListCheckedAll;
this.filterBu7Table().forEach(x => x.check = selectAll); checkPrimary() {
this.dataListCheck(); return this.bu7.dataList.find(x => x.bu7id == this.bu7.select.bu7id)
} }
dataListCheck() { numSelectItem() {
const dataCheck = this.filterBu7Table(); const selectedKeys = Array.from(this.selectedItems.data.keys());
this.isDataListCheckedAll = dataCheck.length ? dataCheck.every(x => x.check) : false; const num = this.bu7.dataList.filter(x => selectedKeys.includes(x.bu7id) && this.selectedItems.data.get(x.bu7id)).length
this.numDataListChecked = this.bu7List.filter(x => x.check).length return num
this.isDataListChecked = Boolean(this.numDataListChecked)
} }
checkPrimary() { onSelectItemChange(arg: any) {
return this.bu7List.find(x => x.data.bu7id == this.bu7.bu7id) 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