diff --git a/src/pages/examine/record/components/printStyle.css b/src/pages/examine/record/components/printStyle.css index ad05641..d2cee73 100644 --- a/src/pages/examine/record/components/printStyle.css +++ b/src/pages/examine/record/components/printStyle.css @@ -1,6 +1,5 @@ /* 打印样式 */ @media print { - /* 隐藏不需要打印的元素 */ .ant-pro-card-header, .ant-pro-table-search, @@ -21,7 +20,7 @@ td { border: 1px solid #000; padding: 8px; - text-align: left; + text-align: center; } /* 页面设置 */ @@ -53,4 +52,21 @@ margin: 20px 0; white-space: pre-wrap; } + + /* 确保iframe内的打印内容可见 */ + iframe { + display: block !important; + visibility: visible !important; + position: absolute; + left: 0; + top: 0; + width: 100%; + height: 100%; + } + + /* 确保打印内容可见 */ + .print-content, .print-content * { + visibility: visible !important; + display: block !important; + } } \ No newline at end of file diff --git a/src/pages/examine/record/components/recordDetail.tsx b/src/pages/examine/record/components/recordDetail.tsx index b877485..68133fe 100644 --- a/src/pages/examine/record/components/recordDetail.tsx +++ b/src/pages/examine/record/components/recordDetail.tsx @@ -38,7 +38,7 @@ const RecordDetail = ({ parentRow, show, detailType }: DetailProps) => { const columns: any = [ { title: "考核分类", - dataIndex: "name" + dataIndex: "parentName" }, { title: "考核子类", @@ -121,7 +121,7 @@ const RecordDetail = ({ parentRow, show, detailType }: DetailProps) => { } // 把需要打印的数组拿到一层中来 - const handleGetTableRes = (list: any) => { + const handleGetTableRes = (list: any, fieldObj?: any) => { console.log('list', list); let res: any = []; @@ -132,13 +132,14 @@ const RecordDetail = ({ parentRow, show, detailType }: DetailProps) => { item.question.forEach((questionItem: any) => { res.push({ ...questionItem, - parentId: item.parentId ? item.parentId : "-" + parentId: item.parentId ? item.parentId : "-", + parentName: fieldObj && item.parentId ? fieldObj[item.parentId] : "-" }); }); } if (item.children && item.children.length > 0) { // 如果有子项,递归处理子项并合并结果 - const childResults = handleGetTableRes(item.children); + const childResults = handleGetTableRes(item.children, fieldObj); if (childResults && childResults.length > 0) { res = [...res, ...childResults]; } @@ -161,61 +162,100 @@ const RecordDetail = ({ parentRow, show, detailType }: DetailProps) => { // 从categoryId获取分类名称 if (item.question && item.question.categoryId) { // 这里需要根据实际情况获取分类名称 - // 可以从缓存或其他地方获取 - // const categoryInfo = getCategoryInfo(item.question.categoryId); item.name = examineObj && item.parentId ? examineObj[item.parentId] : "" item.categoryName = item?.question.title || ''; item.parentCategoryName = item?.name || ''; } }); + // 按照考核分类和考核子类排序,确保相同的分类和子类相邻 + processedData.sort((a, b) => { + // 先按考核分类排序 + if (a.parentCategoryName !== b.parentCategoryName) { + return a.parentCategoryName.localeCompare(b.parentCategoryName); + } + // 再按考核子类排序 + return a.categoryName.localeCompare(b.categoryName); + }); + // 计算合并信息 let currentCategory = ''; let currentSubCategory = ''; - let categorySpan = 0; - let subCategorySpan = 0; + let categoryStartIndex = 0; + let subCategoryStartIndex = 0; processedData.forEach((item: any, index: number) => { + // 初始化合并标记 + item._categoryMerged = false; + item._subCategoryMerged = false; + // 处理考核分类合并 - if (item.parentCategoryName === currentCategory) { - categorySpan++; - item._categoryMerged = true; // 标记为已合并 - } else { - // 更新前一个分类的合并信息 - if (categorySpan > 0 && index > 0) { - processedData[index - categorySpan]._categoryRowSpan = categorySpan + 1; + if (index === 0 || item.parentCategoryName !== currentCategory) { + // 如果是新的分类,更新前一个分类的合并信息 + if (index > 0) { + const span = index - categoryStartIndex; + if (span > 1) { // 只有当跨度大于1时才需要合并 + processedData[categoryStartIndex]._categoryRowSpan = span; + // 标记除第一行外的其他行为已合并 + for (let i = categoryStartIndex + 1; i < index; i++) { + processedData[i]._categoryMerged = true; + } + } } + // 记录新分类的开始位置 + categoryStartIndex = index; currentCategory = item.parentCategoryName; - categorySpan = 0; - item._categoryRowSpan = 1; // 默认占一行 + // 重置子类信息,因为新的分类开始了 + currentSubCategory = ''; + subCategoryStartIndex = index; } // 处理考核子类合并 - if (item.categoryName === currentSubCategory) { - subCategorySpan++; - item._subCategoryMerged = true; // 标记为已合并 - } else { - // 更新前一个子类的合并信息 - if (subCategorySpan > 0 && index > 0) { - processedData[index - subCategorySpan]._subCategoryRowSpan = subCategorySpan + 1; + if (index === 0 || item.categoryName !== currentSubCategory || item.parentCategoryName !== currentCategory) { + // 如果是新的子类或新的分类,更新前一个子类的合并信息 + if (index > 0 && currentSubCategory) { + const span = index - subCategoryStartIndex; + if (span > 1) { // 只有当跨度大于1时才需要合并 + processedData[subCategoryStartIndex]._subCategoryRowSpan = span; + // 标记除第一行外的其他行为已合并 + for (let i = subCategoryStartIndex + 1; i < index; i++) { + processedData[i]._subCategoryMerged = true; + } + } } + // 记录新子类的开始位置 + subCategoryStartIndex = index; currentSubCategory = item.categoryName; - subCategorySpan = 0; - item._subCategoryRowSpan = 1; // 默认占一行 } }); // 处理最后一组数据的合并信息 - if (categorySpan > 0 && processedData.length > 0) { - const targetIndex = processedData.length - categorySpan - 1; - if (targetIndex >= 0 && targetIndex < processedData.length && processedData[targetIndex]) { - processedData[targetIndex]._categoryRowSpan = categorySpan + 1; + const lastIndex = processedData.length - 1; + if (lastIndex >= 0) { + // 处理最后一个分类的合并 + const categorySpan = lastIndex - categoryStartIndex + 1; + if (categorySpan > 1) { + processedData[categoryStartIndex]._categoryRowSpan = categorySpan; + for (let i = categoryStartIndex + 1; i <= lastIndex; i++) { + processedData[i]._categoryMerged = true; + } + } else { + // 如果只有一行,不需要合并 + processedData[categoryStartIndex]._categoryRowSpan = 1; + processedData[categoryStartIndex]._categoryMerged = false; } - } - if (subCategorySpan > 0 && processedData.length > 0) { - const targetIndex = processedData.length - subCategorySpan - 1; - if (targetIndex >= 0 && targetIndex < processedData.length && processedData[targetIndex]) { - processedData[targetIndex]._subCategoryRowSpan = subCategorySpan + 1; + + // 处理最后一个子类的合并 + const subCategorySpan = lastIndex - subCategoryStartIndex + 1; + if (subCategorySpan > 1) { + processedData[subCategoryStartIndex]._subCategoryRowSpan = subCategorySpan; + for (let i = subCategoryStartIndex + 1; i <= lastIndex; i++) { + processedData[i]._subCategoryMerged = true; + } + } else { + // 如果只有一行,不需要合并 + processedData[subCategoryStartIndex]._subCategoryRowSpan = 1; + processedData[subCategoryStartIndex]._subCategoryMerged = false; } } @@ -250,73 +290,58 @@ const RecordDetail = ({ parentRow, show, detailType }: DetailProps) => { // 打印表格方法 const handlePrintTable = () => { - // 创建打印内容 - const printWindow = window.open('', '_blank'); - if (!printWindow) { - Modal.error({ title: '打印失败', content: '无法创建打印窗口,请检查浏览器设置。' }); - return; - } - // 获取表格数据 const tableData = proTableData || []; - // 构建HTML内容 + // 创建一个iframe用于打印,这样可以避免CSS冲突 + const printIframe = document.createElement('iframe'); + printIframe.style.position = 'absolute'; + printIframe.style.width = '0'; + printIframe.style.height = '0'; + printIframe.style.border = '0'; + document.body.appendChild(printIframe); + + // 构建打印内容 let printContent = ` - - - 考核记录打印 - - - + + + + 打印页面 + + + +