提交 23174415 authored 作者: 姚鑫国's avatar 姚鑫国

getColumnsDataRealize

优化 方法,新增映射合并 新增合并主键
上级 62236816
...@@ -54,16 +54,18 @@ const uj = { ...@@ -54,16 +54,18 @@ const uj = {
* rowSpanData 列合并数据,colSpanData行合并数据 * rowSpanData 列合并数据,colSpanData行合并数据
* */ * */
export function getSpanJson({ export function getSpanJson({
mergeKey, mergeKey = [],
data, data,
eliminateRow = [], eliminateRow = [],
eliminateCol = [] eliminateCol = [],
mergePrimaryRow,
mergeMappingRow = {} // 某一列的合并数据 复制 到任意其他列
}) { }) {
const dataList = deepCopy(data); const dataList = deepCopy(data);
let rowSpanData, colSpanData = null; let rowSpanData, colSpanData = null;
try { try {
rowSpanData = getRowSpanJson(mergeKey, dataList, eliminateRow); rowSpanData = getRowSpanJson(mergeKey, dataList, eliminateRow, mergePrimaryRow);
} catch (err) { } catch (err) {
console.table("rowSpanData", err); console.table("rowSpanData", err);
} }
...@@ -72,13 +74,24 @@ export function getSpanJson({ ...@@ -72,13 +74,24 @@ export function getSpanJson({
} catch (err) { } catch (err) {
console.table("colSpanData", err); console.table("colSpanData", err);
} }
try {
const newMergeMappingRow = Object.keys(mergeMappingRow).filter(item1 => mergeKey.some(item2 => item2 === item1));
newMergeMappingRow.map((item) => {
const mappingList = mergeMappingRow[item].filter(item1 => !mergeKey.some(item2 => item2 === item1));
mappingList.map((mItem) => {
rowSpanData[mItem] = rowSpanData[item];
});
});
} catch (e) {
console.table("mergeMappingRow", e);
}
return { return {
rowSpanData, rowSpanData,
colSpanData, colSpanData,
}; };
} }
function getRowSpanJson(cloumnNames, data, eliminateStr = []) { function getRowSpanJson(cloumnNames, data, eliminateStr = [], mergePrimaryRow) {
data = deepCopy(data); data = deepCopy(data);
//纵向合并 //纵向合并
// 获取列的 dataIndex 集合 // 获取列的 dataIndex 集合
...@@ -89,20 +102,32 @@ function getRowSpanJson(cloumnNames, data, eliminateStr = []) { ...@@ -89,20 +102,32 @@ function getRowSpanJson(cloumnNames, data, eliminateStr = []) {
var countMap = {}; var countMap = {};
//该name第一次出现的位置 //该name第一次出现的位置
var startIndexMap = {}; var startIndexMap = {};
for (var idx = 0; idx < data.length; idx++) { // 是否主键模式
var isPrimary = false;
for (var idx = 0; idx < data.length; idx++) { // 循环数据
var dataItem = data[idx]; var dataItem = data[idx];
for (var idj = 0; idj < cloumnNames.length; idj++) { var dataItemRowValue;
var dataItemPreviousRowValue;
if (mergePrimaryRow) {
isPrimary = true;//开启主键模式
dataItemRowValue = dataItem[mergePrimaryRow];
dataItemPreviousRowValue = data[idx - 1 > 0 ? idx - 1 : 0][mergePrimaryRow];
}
for (var idj = 0; idj < cloumnNames.length; idj++) { // 循环表头
var keyItem = cloumnNames[idj]; var keyItem = cloumnNames[idj];
var dataItemValue = dataItem[keyItem]; var dataItemValue = dataItem[keyItem];
if (idx === 0) { if (idx === 0) {
beforeNameMap[keyItem] = dataItem[keyItem]; beforeNameMap[keyItem] = dataItemValue;
countMap[keyItem] = 1; countMap[keyItem] = 1;
startIndexMap[keyItem] = 0; startIndexMap[keyItem] = 0;
rowSpanJson[keyItem] = new Array(data.length); rowSpanJson[keyItem] = new Array(data.length);
rowSpanJson[keyItem][0] = 1; rowSpanJson[keyItem][0] = 1;
continue; continue;
} }
if (dataItemValue === beforeNameMap[keyItem] && eliminateStr.indexOf(dataItemValue) === -1) { if (
(!isPrimary ? (dataItemValue === beforeNameMap[keyItem]) : (dataItemRowValue + dataItemValue === dataItemPreviousRowValue + beforeNameMap[keyItem])) // 与上一个值一致时
&& eliminateStr.indexOf(dataItemValue) === -1 // 排除特定的值合并
) {
countMap[keyItem] = countMap[keyItem] + 1; countMap[keyItem] = countMap[keyItem] + 1;
rowSpanJson[keyItem][startIndexMap[keyItem]] = countMap[keyItem]; rowSpanJson[keyItem][startIndexMap[keyItem]] = countMap[keyItem];
rowSpanJson[keyItem][idx] = 0; rowSpanJson[keyItem][idx] = 0;
...@@ -110,7 +135,6 @@ function getRowSpanJson(cloumnNames, data, eliminateStr = []) { ...@@ -110,7 +135,6 @@ function getRowSpanJson(cloumnNames, data, eliminateStr = []) {
countMap[keyItem] = 1; countMap[keyItem] = 1;
beforeNameMap[keyItem] = dataItemValue; beforeNameMap[keyItem] = dataItemValue;
startIndexMap[keyItem] = idx; startIndexMap[keyItem] = idx;
rowSpanJson[keyItem][idx] = 1; rowSpanJson[keyItem][idx] = 1;
} }
} }
...@@ -132,7 +156,6 @@ function getColSpanJson(cloumnNames, data, eliminateStr = []) { ...@@ -132,7 +156,6 @@ function getColSpanJson(cloumnNames, data, eliminateStr = []) {
let keyItem = cloumnNames[idj]; let keyItem = cloumnNames[idj];
let dataItemValue = dataItem[keyItem]; let dataItemValue = dataItem[keyItem];
let dataItemValueNext = idj < cloumnNames.length - 1 ? dataItem[cloumnNames[idj + 1]] : null; let dataItemValueNext = idj < cloumnNames.length - 1 ? dataItem[cloumnNames[idj + 1]] : null;
//console.log("dataItemValue",dataItemValue,idj)
if (idx === 0) { if (idx === 0) {
colSpanJson[keyItem] = new Array(data.length); colSpanJson[keyItem] = new Array(data.length);
} }
...@@ -164,7 +187,6 @@ function getColSpanJson(cloumnNames, data, eliminateStr = []) { ...@@ -164,7 +187,6 @@ function getColSpanJson(cloumnNames, data, eliminateStr = []) {
} }
} }
} }
//console.log("colSpanJson",colSpanJson)
return colSpanJson; return colSpanJson;
} }
...@@ -214,7 +236,7 @@ export function getColumns(columns) { ...@@ -214,7 +236,7 @@ export function getColumns(columns) {
} }
return res; return res;
} catch (e) { } catch (e) {
console.table("getColumnsData", e); console.table("getColumnsDatas", e);
return []; return [];
} }
} }
...@@ -436,6 +458,8 @@ export function getColumnsDataRealize({ ...@@ -436,6 +458,8 @@ export function getColumnsDataRealize({
ext = 'xlsx', ext = 'xlsx',
eliminateRow = [], eliminateRow = [],
eliminateCol = [], eliminateCol = [],
mergeMappingRow = {},// *映射mergeKey 纵向合并结果,key必须在mergeKey数组里面
mergePrimaryRow,// 所有合并都会在当前mergePrimary 的范围下合并
}) { }) {
let content = getColumnsData({ let content = getColumnsData({
data, data,
...@@ -447,6 +471,8 @@ export function getColumnsDataRealize({ ...@@ -447,6 +471,8 @@ export function getColumnsDataRealize({
ext, ext,
eliminateRow, eliminateRow,
eliminateCol, eliminateCol,
mergeMappingRow,// *映射mergeKey 纵向合并结果,key必须在mergeKey数组里面
mergePrimaryRow,// 所有合并都会在当前mergePrimary 的范围下合并
}); });
if (styles) { if (styles) {
content.styles = styles; content.styles = styles;
...@@ -561,7 +587,9 @@ export function getCustomDataRealize({ ...@@ -561,7 +587,9 @@ export function getCustomDataRealize({
* ext : 导出后缀 目前xecl、dbf * ext : 导出后缀 目前xecl、dbf
* url : 接口地址 * url : 接口地址
* eliminateRow : 列需要忽略合并的数据 * eliminateRow : 列需要忽略合并的数据
* eliminateCol : 行需要忽略合并的数据 * eliminateCol : 行需要忽略合并的数据
* mergeMappingRow : *映射mergeKey 纵向合并结果,key必须在mergeKey数组里面,value不可以在mergeKey里。
* mergePrimary : 所有合并都会在当前mergePrimary 的范围下合并
* *
*/ */
export function getColumnsData({ export function getColumnsData({
...@@ -575,6 +603,8 @@ export function getColumnsData({ ...@@ -575,6 +603,8 @@ export function getColumnsData({
eliminateRow = [], eliminateRow = [],
eliminateCol = [], eliminateCol = [],
docxSheet = {}, docxSheet = {},
mergeMappingRow = {},// *映射mergeKey 纵向合并结果,key必须在mergeKey数组里面
mergePrimaryRow,// 所有合并都会在当前mergePrimary 的范围下合并
}) { }) {
columns = deepCopy(columns); columns = deepCopy(columns);
data = deepCopy(data); data = deepCopy(data);
...@@ -591,7 +621,9 @@ export function getColumnsData({ ...@@ -591,7 +621,9 @@ export function getColumnsData({
mergeKey, mergeKey,
data, data,
eliminateRow, eliminateRow,
eliminateCol eliminateCol,
mergeMappingRow,
mergePrimaryRow
}); });
// 处理数据 // 处理数据
let bodyData = []; let bodyData = [];
...@@ -608,7 +640,17 @@ export function getColumnsData({ ...@@ -608,7 +640,17 @@ export function getColumnsData({
bodyData.push(nowArroy); bodyData.push(nowArroy);
}); });
// 处理合并数据 // 处理合并数据
mergeKey.forEach((mv, mi) => { let newMergeKey = mergeKey;
try {
const newMergeMappingRow = Object.keys(mergeMappingRow).filter(item1 => mergeKey.some(item2 => item2 === item1));
newMergeMappingRow.map((item) => {
const mappingList = mergeMappingRow[item].filter(item1 => !mergeKey.some(item2 => item2 === item1));
newMergeKey = [...newMergeKey, ...mappingList];
});
} catch (e) {
console.table("newMergeKey", e);
}
Object.keys(rowSpanData).forEach((mv) => {
rowSpanData[mv].forEach((cv, ci) => { rowSpanData[mv].forEach((cv, ci) => {
if (cv > 1) { if (cv > 1) {
//合并表格的 开始行、结束行、开始列、结束列 //合并表格的 开始行、结束行、开始列、结束列
...@@ -616,6 +658,8 @@ export function getColumnsData({ ...@@ -616,6 +658,8 @@ export function getColumnsData({
dataMerges.push([columnsHeight + ci, columnsHeight + ci + cv - 1, rowIndex, rowIndex]); dataMerges.push([columnsHeight + ci, columnsHeight + ci + cv - 1, rowIndex, rowIndex]);
} }
}); });
})
Object.keys(colSpanData).forEach((mv) => {
colSpanData[mv].forEach((cv, ci) => { colSpanData[mv].forEach((cv, ci) => {
if (cv > 1) { if (cv > 1) {
//合并表格的 开始行、结束行、开始列、结束列 //合并表格的 开始行、结束行、开始列、结束列
...@@ -623,7 +667,7 @@ export function getColumnsData({ ...@@ -623,7 +667,7 @@ export function getColumnsData({
dataMerges.push([columnsHeight + ci, columnsHeight + ci, rowIndex, rowIndex + cv - 1]); dataMerges.push([columnsHeight + ci, columnsHeight + ci, rowIndex, rowIndex + cv - 1]);
} }
}); });
}); })
} catch (err) { } catch (err) {
console.table("getColumnsData", err); console.table("getColumnsData", err);
} }
...@@ -724,7 +768,7 @@ export function getCustomData({ ...@@ -724,7 +768,7 @@ export function getCustomData({
}); });
}); });
} catch (err) { } catch (err) {
console.table("getColumnsData", err); console.table("getCustomData", err);
} }
const columnsDataCells = [...columnsList, ...bodyData]; const columnsDataCells = [...columnsList, ...bodyData];
...@@ -732,7 +776,7 @@ export function getCustomData({ ...@@ -732,7 +776,7 @@ export function getCustomData({
let newBottomMerges = []; let newBottomMerges = [];
if (bottomList && bottomList.length > 0) { // 新增底部数据 if (bottomList && bottomList.length > 0) { // 新增底部数据
if (columnsKey.length != bottomList[0].length) { if (columnsKey.length != bottomList[0].length) {
console.log("bottomList数据列数和表头列数不一致"); console.table("bottomList数据列数和表头列数不一致");
return return
} }
newBottomMerges = bottomMerges.map((v, i) => { newBottomMerges = bottomMerges.map((v, i) => {
...@@ -1064,7 +1108,7 @@ export function upObjKey(obj, setKey = {}, defaultValue = "") { ...@@ -1064,7 +1108,7 @@ export function upObjKey(obj, setKey = {}, defaultValue = "") {
return null; return null;
} }
} catch (error) { } catch (error) {
console.log("upObjKey", error); console.table("upObjKey", error);
return undefined; return undefined;
} }
} }
...@@ -1157,7 +1201,6 @@ function FormdataWrapper(obj, fd, pre) { ...@@ -1157,7 +1201,6 @@ function FormdataWrapper(obj, fd, pre) {
} }
export async function downloadFile(url, params, fileName = '导出文件', ext = 'xlsx') { export async function downloadFile(url, params, fileName = '导出文件', ext = 'xlsx') {
// console.log("downloadFile",url);
const { transformApi, headersApi } = await getTransformApi(url); const { transformApi, headersApi } = await getTransformApi(url);
let headers = { let headers = {
Authorization: `bearer ${localStorage.getItem('antd-pro-token-onestop')}`, Authorization: `bearer ${localStorage.getItem('antd-pro-token-onestop')}`,
...@@ -1192,7 +1235,7 @@ export async function downloadFile(url, params, fileName = '导出文件', ext = ...@@ -1192,7 +1235,7 @@ export async function downloadFile(url, params, fileName = '导出文件', ext =
} }
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.table(err);
}) })
.finally(() => { .finally(() => {
return true; return true;
...@@ -1201,7 +1244,6 @@ export async function downloadFile(url, params, fileName = '导出文件', ext = ...@@ -1201,7 +1244,6 @@ export async function downloadFile(url, params, fileName = '导出文件', ext =
export async function downloadFileAndShowModal(url, params, fileName = '导出文件', ext = 'xlsx') { export async function downloadFileAndShowModal(url, params, fileName = '导出文件', ext = 'xlsx') {
// console.log("downloadFile",url);
const { transformApi, headersApi } = await getTransformApi(url); const { transformApi, headersApi } = await getTransformApi(url);
let headers = { let headers = {
Authorization: `bearer ${localStorage.getItem('antd-pro-token-onestop')}`, Authorization: `bearer ${localStorage.getItem('antd-pro-token-onestop')}`,
...@@ -1256,7 +1298,7 @@ export async function downloadFileAndShowModal(url, params, fileName = '导出 ...@@ -1256,7 +1298,7 @@ export async function downloadFileAndShowModal(url, params, fileName = '导出
} }
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.table(err);
}) })
.finally(() => { .finally(() => {
return true; return true;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论