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

getColumnsDataRealize

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