import { Component, Input, OnInit, Output, EventEmitter, ElementRef } from "@angular/core"; import { NzModalService, NzMessageService } from "ng-zorro-antd"; import * as XLSX from "xlsx"; import { saveAs } from "file-saver"; // import { BcacHttpService } from 'bcac-lib/services/bcac-http.service'; import { TableService } from '../table.service'; @Component({ selector: "bcac-common-table", templateUrl: "./bcac-common-table.component.html", styleUrls: ["./bcac-common-table.component.scss"], }) export class BcacCommonTableComponent implements OnInit { constructor( private nzMessageService: NzMessageService, private elementRef: ElementRef, // private bcacHttpService: BcacHttpService private tableService: TableService ) {} redColor = "#FF0000"; greenColor = "#7ED320"; yellowColor = "#f2dd21"; grayColor = "#bab7b5"; @Output() checkBoxEmit = new EventEmitter<{ [key: string]: boolean }>(); @Output() tdClickEmit = new EventEmitter(); tdClick(field: string, data: any) { this.tdClickEmit.emit({ field: field, data: data }); console.log("tdClick", { field: field, data: data }); } tableSearchValue: string = ""; tableloading: boolean = false; @Input() set bcacLoading(value: boolean) { this.tableloading = value; } @Output() bcacAddEmit = new EventEmitter(); clickAdd() { this.bcacAddEmit.emit(1); } @Output() bcacBatchDeleteEmit = new EventEmitter(); clickBatchDelete() { let checkedIdList = []; for (let key in this.mapOfCheckedId) { if (this.mapOfCheckedId[key]) { checkedIdList.push(key); } } if (checkedIdList.length > 0) { this.bcacBatchDeleteEmit.emit(checkedIdList); } else { this.nzMessageService.info("无选中记录"); } } @Output() BcacBatchDownloadEmit = new EventEmitter(); batchDownload() { let checkedIdList = []; for (let key in this.mapOfCheckedId) { if (this.mapOfCheckedId[key]) { checkedIdList.push(key); } } let checkedObjList = this.tableData.filter(element=> checkedIdList.indexOf(element[this.tableContent.uniqueIndex] + "") > -1); // in语法有问题,舍弃in // let checkedObjList = this.tableData.filter(element=> (element[this.tableContent.uniqueIndex] + "") in checkedIdList); if (checkedObjList.length > 0) { this.BcacBatchDownloadEmit.emit(checkedObjList); } else { this.nzMessageService.info("无选中记录"); } } // row operation @Output() bcacDetailEmit = new EventEmitter(); clickDetail(data) { this.bcacDetailEmit.emit(data); } @Output() bcacEditorEmit = new EventEmitter(); clickEditor(data) { this.bcacEditorEmit.emit(data); } @Output() bcacDeleteEmit = new EventEmitter(); clickDelete(data) { this.bcacDeleteEmit.emit(data); } @Output() bcacReviewEmit = new EventEmitter(); clickReview(data) { this.bcacReviewEmit.emit(data); } @Input() tableData: any[] = []; tableTitle = { show: true, label: "基础数据表", }; @Input() set bcacShowTitle(value) { this.tableTitle.show = true, this.tableTitle.label = value; } @Input() set bcacCloseTitle(value) { this.tableTitle.show = false; } // @Input() tableIconRow = { show: true, // refreshIcon: { // show: false, // label: "刷新", // }, addIcon: { show: true, label: "新增", }, configIcon: { show: true, label: "设置", }, deleteIcon: { show: true, label: "删除", }, excelIcon: { show: true, label: "导出", }, batchDownload: { show: true, label: "下载", }, searchInput: { show: true }, batchReview: { show: false, label: "批量审核" }, }; @Input() set bcacShowBatchReviewIcon(value: boolean) { this.tableIconRow.batchReview.show = true; } @Output() bcacBatchReviewEmit = new EventEmitter(); clickBatchReview() { let checkedIdList = []; for (let key in this.mapOfCheckedId) { if (this.mapOfCheckedId[key]) { checkedIdList.push(key); } } let checkedObjList = this.tableData.filter(element=> checkedIdList.indexOf(element[this.tableContent.uniqueIndex] + "") > -1); // in语法有问题,舍弃in // let checkedObjList = this.tableData.filter(element=> (element[this.tableContent.uniqueIndex] + "") in checkedIdList); if (checkedObjList.length > 0) { this.bcacBatchReviewEmit.emit(checkedObjList); console.log("click batch review", checkedObjList); } else { this.nzMessageService.info("无选中记录"); } } // @Input() // set bcacShowTableRefreshIcon(value) { // this.tableIconRow.refreshIcon.show = true; // } @Input() set bcacCloseTableIcon(value) { this.tableIconRow.show = false; } @Input() set bcacCloseTableAddIcon(value) { this.tableIconRow.addIcon.show = false; } @Input() set bcacCloseTableConfigIcon(value) { this.tableIconRow.configIcon.show = false; } @Input() set bcacCloseTableDeleteIcon(value) { this.tableIconRow.deleteIcon.show = false; } @Input() set bcacCloseTableExcelIcon(value) { this.tableIconRow.excelIcon.show = false; } @Input() set bcacCloseBatchDownIcon(value) { this.tableIconRow.batchDownload.show =false; } @Input() set bcacCloseTableSearch(value) { this.tableIconRow.searchInput.show = false; } tableContent = { show: true, uniqueIndex: "recordId", // tableData: [], pagination: { show: true, pageSize: 10, pageSizeOptions: [10, 20, 50, 100], }, checkBox: { show: true, width: "3.8rem", checkField: "rid", }, columnOptions: [], rowOperation: { show: true, label: "操作", width: "8rem", detailIcon: { show: true, label: "详情", }, editorIcon: { show: false, label: "编辑", }, deleteIcon: { show: false, label: "删除", }, reviewIcon: { show: false, label: "审核" }, reviewRecordIcon: { show: false, label: "审核记录" }, previewIcon: { show: false, label: "预览" }, downloadIcon: { show: false, label: "下载" }, }, }; @Input() set bcacShowdownloadIcon(value: boolean) { this.tableContent.rowOperation.downloadIcon.show = true; } @Output() bcacDownloadEmit = new EventEmitter(); clickDownload(data: any) { this.bcacDownloadEmit.emit(data); console.log("click download", data); } @Input() set bcacShowPreviewIcon(value: boolean) { this.tableContent.rowOperation.previewIcon.show = true; } @Output() bcacPreviewEmit = new EventEmitter(); clickPreview(data: any) { this.bcacPreviewEmit.emit(data); console.log("click preview", data); } @Input() set bcacShowReviewRecordIcon(value: boolean) { this.tableContent.rowOperation.reviewRecordIcon.show = true; } @Output() bcacReviewRecordEmit = new EventEmitter(); clickReviewRecord(data: any) { this.bcacReviewRecordEmit.emit(data); console.log("bcacReviewRecordEmit", data); } @Input() set bcacShowReviewIcon(value) { this.tableContent.rowOperation.reviewIcon.show = true; } @Input() set bcacColumnOptions(value) { this.tableContent.columnOptions = value; } @Input() set bcacCloseRowOperation(value) { this.tableContent.rowOperation.show = false; } @Input() set bcacRowOperationWidth(value) { this.tableContent.rowOperation.width = value; } @Input() set bcacCloseDetailRowIcon(value) { this.tableContent.rowOperation.detailIcon.show = false; } @Input() set bcacShowEditorRowIcon(value) { this.tableContent.rowOperation.editorIcon.show = true; } @Input() set bcacShowDeleteRowIcon(value) { this.tableContent.rowOperation.deleteIcon.show = true; } @Input() set bcacUniqueIndex(value) { this.tableContent.uniqueIndex = value; } @Input() set bcacClosePagination(value) { this.tableContent.pagination.show = false; } @Input() set bcacPageSize(value) { this.tableContent.pagination.pageSize = value; } @Input() set bcacPageSizeOptions(value) { this.tableContent.pagination.pageSizeOptions = value; } @Input() set bcacCloseCheckBox(value) { this.tableContent.checkBox.show = false; } @Input() set bcacCheckBoxWidth(value) { this.tableContent.checkBox.width = value; } @Input() set bcacCheckField(value) { this.tableContent.checkBox.checkField = value; } // 分页函数 isIndeterminate = false; isAllDisplayDataChecked: boolean = false; listOfDisplayData: any[] = []; mapOfCheckedId: { [key: string]: boolean } = {}; currentPageDataChange($event: Array<{ rid: string }>): void { this.listOfDisplayData = $event; this.refreshStatus(); } refreshStatus(): void { this.isAllDisplayDataChecked = this.listOfDisplayData.every( (item) => this.mapOfCheckedId[item[this.tableContent.uniqueIndex]] ); this.isIndeterminate = this.listOfDisplayData.some( (item) => this.mapOfCheckedId[item[this.tableContent.uniqueIndex]] ) && !this.isAllDisplayDataChecked; this.checkBoxEmit.emit(this.mapOfCheckedId); } checkAll(value: boolean): void { this.listOfDisplayData.forEach( (item) => (this.mapOfCheckedId[item[this.tableContent.uniqueIndex]] = value) ); this.refreshStatus(); } //列操作排序容器 mapOfSort: { [key: string]: string | null } = {}; initMapOfSort() { this.tableContent.columnOptions.forEach((element) => { let column = element.value; this.mapOfSort[column] = null; }); } // sort事件 sortName: string | null = null; sortValue: string | null = null; sort(sortName: string, value: string): void { this.sortName = sortName; //排序字段 this.sortValue = value; //排序方式 // 互斥排序模式 for (const key in this.mapOfSort) { this.mapOfSort[key] = key === sortName ? value : null; } this.search(); } filterListOfMap = {}; filterList = []; listOfFilterValueOfMap = {}; initFilterListMap() { this.tableContent.columnOptions.forEach((element) => { let list = Array.from( new Set(this.tableData.map((item) => item[element.value])) ).map((e) => { if (element.tdType == "nzBadge" || element.tdType == "bgColor") { return { text: element.tooltipMap[e], value: e } } else { return { text: e, value: e }; } }); let column = element.value; this.filterListOfMap[column] = list; this.filterList.push(column); this.listOfFilterValueOfMap[column] = []; }); } filter(event, field) { this.listOfFilterValueOfMap[field] = event; this.search(); } searchDropMenuVisible: boolean = false; columnSearchValueMap = {}; columnSearchList = []; initColumnSearchValueMap() { this.tableContent.columnOptions.forEach((element) => { let column = element.value; this.columnSearchValueMap[column] = ""; this.columnSearchList.push(column); }); } reset(value): void { this.columnSearchValueMap[value] = ""; this.search(); } // checkedcol = []; columnCheckChange(value: object[]): void { // this.checkedcol = this.tableContent.columnOptions // .filter((e) => e.checked === true) // .map((e) => e.value); } deleteRowCancel(): void { // this.nzMessageService.info("点击取消"); } @Input() set tabledatas(value) { this.tableData = value; console.log(value); console.log("input"); this.tableDataInit(); } initTableSearchValue() { this.tableSearchValue = ""; } ngOnInit() { this.tableDataInit(); } tableDataInit() { this.tableData_bak = this.tableData; // this.checkedcol = this.tableContent.columnOptions // .filter((e) => e.checked === true) // .map((e) => e.value); this.initTableSearchValue(); this.initMapOfSort(); this.initFilterListMap(); this.initColumnSearchValueMap(); this.initSearchDropMenuVisibleMap(); console.log("Oninit") } searchDropMenuVisibleMap: { [key: string]: boolean | null } = {}; initSearchDropMenuVisibleMap () { this.tableContent.columnOptions.forEach((element) => { let column = element.value; this.searchDropMenuVisibleMap[column] = false; }); } tableData_bak = []; search(): void { //列选择过滤搜索函数 const filterFunc = (item) => { let filterResult = true; for (let i = 0; i < this.filterList.length; i++) { let column = this.filterList[i]; if ( this.listOfFilterValueOfMap[column].length && this.listOfFilterValueOfMap[column].every( (name) => item[column].indexOf(name) === -1 ) ) { filterResult = false; break; } } return filterResult; }; const filterFuncAll = (item) => { let filterAllResult = false; for (let i = 0; i < this.tableContent.columnOptions.length; i++) { let column = this.tableContent.columnOptions[i].value; if ( item[column] && new String(item[column]).indexOf(this.tableSearchValue) !== -1 ) { filterAllResult = true; break; } } return filterAllResult; }; // 列模糊搜索函数 const singleColumnSearch = (item) => { let columnSearchResult = true; for (let i = 0; i < this.columnSearchList.length; i++) { let column = this.columnSearchList[i]; if ( this.columnSearchValueMap[column] && item[column].indexOf(this.columnSearchValueMap[column]) === -1 ) { columnSearchResult = false; break; } } return columnSearchResult; }; const complexFilter = (item) => { return ( filterFunc(item) && filterFuncAll(item) && singleColumnSearch(item) ); }; let filterData = this.tableData_bak; filterData = this.tableData_bak.filter((item) => complexFilter(item)); // if ((this.listOfSearchMnName.length || this.listOfSearchAdmin.length) || this.searchValue || this.addrSearchValue) { // } else { // filterData = this.tableData_bak.filter((item)=> item); // } if (this.sortValue != null) { this.tableData = filterData.sort((a, b) => // this.sortValue === "ascend" // ? a[this.sortName!] > b[this.sortName!] // ? 1 // : -1 // : b[this.sortName!] > a[this.sortName!] // ? 1 // : -1 this.sortValue === "ascend" ? isNaN(a[this.sortName!] - b[this.sortName!])? a[this.sortName!] > b[this.sortName!]? 1: -1: a[this.sortName!] - b[this.sortName!] > 0? 1: -1 : isNaN(b[this.sortName!] - a[this.sortName!])? b[this.sortName!] > a[this.sortName!]? 1: -1: b[this.sortName!] - a[this.sortName!] > 0? 1: -1 ); } else { this.tableData = filterData; } this.searchDropMenuVisible = false; // this.nzModalService.closeAll(); // this.addrSearchDropMenuVisible=false; } allChecked = false; indeterminate = true; updateAllChecked(): void { this.indeterminate = false; if (this.allChecked) { this.tableContent.columnOptions = this.tableContent.columnOptions.map( (item) => { return { ...item, checked: true, }; } ); } else { this.tableContent.columnOptions = this.tableContent.columnOptions.map( (item) => { return { ...item, checked: false, }; } ); } // this.checkedcol = this.tableContent.columnOptions // .filter((e) => e.checked === true) // .map((e) => e.value); } updateSingleChecked(): void { if (this.tableContent.columnOptions.every((item) => !item.checked)) { this.allChecked = false; this.indeterminate = false; } else if (this.tableContent.columnOptions.every((item) => item.checked)) { this.allChecked = true; this.indeterminate = false; } else { this.indeterminate = true; } } exportExcel() { let exportData = []; let tempData = []; if (this.tableContent.checkBox.show) { let checkedIdList = []; for (let key in this.mapOfCheckedId) { if (this.mapOfCheckedId[key]) { checkedIdList.push(key); } } let checkedObjList = this.tableData.filter(element=> checkedIdList.indexOf(element[this.tableContent.uniqueIndex] + "") > -1); tempData = checkedObjList; } else { tempData = this.tableData; } tempData.forEach((element) => { let dataObj = {}; this.tableContent.columnOptions.forEach((column)=> { if (column.checked) { dataObj[column.label] = element[column.value] } }); exportData.push(dataObj); }); if (exportData.length === 0) { let dataObj = {}; this.tableContent.columnOptions.forEach((column) => { if (column.checked) { dataObj[column.label] = ""; } }); exportData.push(dataObj); } const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(exportData); const workbook: XLSX.WorkBook = { Sheets: { data: worksheet }, SheetNames: ["data"], }; const excelBuffer: any = XLSX.write(workbook, { bookType: "xlsx", type: "array", }); const blob = new Blob([excelBuffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8", }); if (this.tableTitle.label) { saveAs(blob, this.tableTitle.label); } else { saveAs(blob, "数据表"); } } columnConfigPanelWidth = "14rem"; @Input() set columnConfigWidth(value: string) { this.columnConfigPanelWidth = value; } switchLoadingMap = {}; switchChange(value) { console.log("value", value); } clickSwitch(data: any, index: number, field: string, switchInfo: any) { this.switchLoadingMap[index + field] = true; this.tableService.accessInterface(switchInfo.interfaceUrl, switchInfo.method, switchInfo.token, data) .subscribe((res: any)=> { this.switchLoadingMap[index + field] = false; data[field] = !data[field] }, error=> { this.nzMessageService.error("操作失败"); this.switchLoadingMap[index + field] = false; }); } }