酸奶检验记录,鲜奶检验记录、生乳检验记录页面导出功能,奶产量分析页面报错修复
This commit is contained in:
@@ -16,16 +16,16 @@
|
||||
|
||||
<!-- 操作按钮行 -->
|
||||
<div class="button-group">
|
||||
<el-button type="success" @click="handleExport">导出</el-button>
|
||||
|
||||
<el-popover placement="bottom" width="400" trigger="click">
|
||||
<el-checkbox-group v-model="selectedFields" class="checkbox-columns">
|
||||
<el-checkbox v-for="col in allColumns" :key="col.prop" :label="col.prop">{{ col.label }}</el-checkbox>
|
||||
<!-- 使用 :value 替代 :label -->
|
||||
<el-checkbox v-for="col in allColumns" :key="col.prop" :value="col.prop">{{ col.label }}</el-checkbox>
|
||||
</el-checkbox-group>
|
||||
<template #reference>
|
||||
<el-button type="info">展示列</el-button>
|
||||
</template>
|
||||
</el-popover>
|
||||
<el-button type="success" @click="handleExport">导出</el-button>
|
||||
</div>
|
||||
|
||||
<!-- 数据表格 -->
|
||||
@@ -55,8 +55,9 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { listSheepMilkAnalysis, exportSheepMilkAnalysis } from "@/api/dairyProducts/sheepMilkAnalysis/sheepMilkAnalysis.js";
|
||||
import { listSheepMilkAnalysis } from "@/api/dairyProducts/sheepMilkAnalysis/sheepMilkAnalysis.js";
|
||||
import { format } from 'date-fns';
|
||||
import * as XLSX from 'xlsx'; // 导入xlsx库
|
||||
|
||||
export default {
|
||||
name: "SheepMilkAnalysis",
|
||||
@@ -126,7 +127,6 @@ export default {
|
||||
getList() {
|
||||
this.loading = true;
|
||||
listSheepMilkAnalysis(this.queryParams).then(response => {
|
||||
// 兼容可能的 axios wrapper:有的返回 { data: { rows, total } }, 有的直接返回 { rows, total }
|
||||
const res = response && response.data ? response.data : response;
|
||||
this.list = res.rows || res;
|
||||
this.total = res.total || (Array.isArray(this.list) ? this.list.length : 0);
|
||||
@@ -155,21 +155,33 @@ export default {
|
||||
this.queryParams.pageSize = pageSize;
|
||||
this.getList();
|
||||
},
|
||||
// 前端导出方法
|
||||
handleExport() {
|
||||
exportSheepMilkAnalysis(this.queryParams).then(response => {
|
||||
const data = response && response.data ? response.data : response;
|
||||
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.setAttribute('download', '羊奶分析数据.xlsx');
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}).catch(err => {
|
||||
this.$message.error('导出失败,请检查后端是否正确返回文件流');
|
||||
// 准备导出数据
|
||||
const exportData = this.list.map(item => {
|
||||
const row = {};
|
||||
this.visibleColumns.forEach(col => {
|
||||
let value = item[col.prop];
|
||||
// 处理日期格式化
|
||||
if (col.formatter && typeof col.formatter === 'function') {
|
||||
value = col.formatter(item);
|
||||
} else if (value instanceof Date) {
|
||||
value = format(new Date(value), 'yyyy-MM-dd');
|
||||
}
|
||||
row[col.label] = value;
|
||||
});
|
||||
return row;
|
||||
});
|
||||
|
||||
// 创建工作簿和工作表
|
||||
const worksheet = XLSX.utils.json_to_sheet(exportData);
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "羊奶产量分析");
|
||||
|
||||
// 生成Excel文件并下载
|
||||
XLSX.writeFile(workbook, "羊奶产量分析数据.xlsx");
|
||||
|
||||
this.$message.success('导出成功');
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -189,4 +201,4 @@ export default {
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
Reference in New Issue
Block a user