// row--当前行;newColumns--表格列配置;menu:含code,根据code区分右键复制是否含表头
function copyDataToclipboard(row, newColumns, menu) {
let copyRow = ''; // 最终粘贴板数据
let title = ''; // 表头数据
let rowData = ''; // 当前行数据
const oldColumns = cloneDeep(newColumns);
const columns = cloneDeep(newColumns);
oldColumns.forEach((item) => {
if (item.params && item.params.spliceList && item.params.spliceList.length) {
item.params.spliceList.forEach(v => {v.params = item.params});
const index = columns.findIndex((v) => v.property === item.property)
columns.splice(index, 1, ...item.params.spliceList)
}
});
columns.forEach((column) => {
// 当前行数据处理
if (column.property && column.type !== 'seq' && column.type !== 'checkbox' && column.property !== 'dropGroupProp') {
const dataCell = getValue({ row, column });
rowData += (dataCell ? dataCell.toString().replaceAll('\n', '').replaceAll('\r', '') : '') + '\t';
// 复制含表头处理
if (menu.code === 'COPY_ROW_WITH_HEADER') {
title += column.title + '\t';
}
}
});
copyRow = title === '' ? rowData : title + '\n' + rowData;
navigator.clipboard.writeText(copyRow);
}
处理每一个单元格数据,进行格式化数据
// grid表格中用于format-view插槽数据展示
export const getValue = ({ row, column }: any) => {
let result = '';
const type = column?.params?.type || '';
const deepValue = column?.params?.deepValue || null;
let value = row[column.property];
// columns中的列配置可配置成多级对象,如: aaa.bbb.ccc【有些时候,前端需要的部分列数据,后端接口会返回在一个对象中,需要深层去拿值】
value = getDeepValue(row, column.property.toString(), deepValue);
if (column?.params?.renderFunc) {
result = column.params.renderFunc(row, column);
} else if (type === 'select' || type === 'boolean') {
result = getLabelByValue(
value,
column.params.optionsConfig,
column.params.options,
column.params.type,
column.params.multiple,
);
} else if (column?.params?.spliceList && column?.optionList && column?.optionList?.length) {
// 拆分列时,若需要格式化枚举数据,需在spliceList对应的对象中添加optionList
result = column?.optionList.find((v) => v.value === value)
? column?.optionList.find((v) => v.value === value).label
: '';
} else {
if ((type === 'string' || type === 'number' || type === undefined) && column.params.formatter === 'thousandtsh') {
if (column.params.showZero || value || value === 0) {
if (column.params.showZero && !value) {
value = 0;
}
if (value !== '0' && value !== 0 && !Number(value)) {
return value;
}
let num = 2;
if (column.params.digit != undefined || column.params.digit != null) {
num = column.params.digit;
}
result = formatMoneyByRound(value, num);
} else {
result = '';
}
} else {
result = value;
}
}
return result;
};
根据当前行数据及列配置中的枚举值,转换数据;拓展出下拉框枚举是树形结构的,支持深层次遍历
export const getLabelByValue = (
value: any,
optionsConfig: { filtersLabel?: string; optionLabel?: string; optionkey?: string; optionValue?: string },
options: any[],
_type: string,
multiple: false,
) => {
let result = value ? (value === '0' || value === 0 ? value : value) : ''; // select匹配不上时,取该字段原本的值
const optionConfig = Object.assign(
{ filtersLabel: 'label', optionLabel: 'label', optionkey: 'value', optionValue: 'value', treeField: 'children' },
optionsConfig,
);
if (multiple && isArray(value)) {
const arr: Array<string> = [];
(value as Array<string>).forEach((value1: string) => {
options?.forEach((item) => {
if (item[optionConfig.optionValue] === value1) {
arr.push(item[optionConfig.optionLabel]);
return true;
}
});
});
result = arr.join(',');
} else if (Array.isArray(options)) {
for (let index = 0; index < options.length; index++) {
const item = options[index];
if (item[optionConfig.optionValue] === value) {
result = item[optionConfig.optionLabel];
break;
} else if (Object.hasOwnProperty.call(item, optionConfig.treeField)) {
// 如果下拉框枚举是树形结构的,支持深层次遍历
const res = getLabelByChildNode(value, item, optionConfig);
if (res) {
result = res;
}
}
}
}
return result;
};
// 写一个迭代,如果下拉框枚举是树形结构的,支持深层次遍历
const getLabelByChildNode = (value, item, optionConfig) => {
if (Object.hasOwnProperty.call(item, optionConfig.treeField) && item[optionConfig.treeField]?.length > 0) {
for (let index = 0; index < item[optionConfig.treeField].length; index++) {
const element = item[optionConfig.treeField][index];
if (element[optionConfig.optionValue] === value) {
return element[optionConfig.optionLabel];
} else {
const res = getLabelByChildNode(value, element, optionConfig);
if (res) {
return res;
}
}
}
}
return '';
};
// 获取深层次对象的某个值
export function getDeepValue(row, field, deepValue) {
if (field?.includes('.') && row[field] !== 0 && !row[field]) {
let fieldValue = null;
field
.split('.')
.filter((item) => item != '')
.forEach((item) => {
if (fieldValue && typeof fieldValue === 'object') {
fieldValue = fieldValue[item];
} else if (row[item]) {
// 应对行数据中,属性包含.的对象嵌套
fieldValue = row[item];
} else {
// 应对行数据中,属性包含.的字符串,理论性不会走到这里,会被方法第一步卡掉
fieldValue = row[field];
}
});
return fieldValue;
} else {
let value = '';
if (deepValue) {
deepValue.split('.').forEach((item, i) => {
value = i < 1 ? row[item] : value[item];
});
} else {
value = row[field];
}
return value;
}
}
//千分位分隔符,控制小数位数,四舍五入,12345.6格式化为12,345.60
export function formatMoneyByRound(s, n) {
let flag = false;
if (String(s).substr(0, 1) == '-') {
s = String(s).substr(1);
flag = true;
}
if(n >= 0 && n <= 20) {
n = n >= 0 && n <= 20 ? n : 2;
const time = Math.pow(10, n);
s = (Math.round(parseFloat(s) * time) / time).toFixed(n);
} else {
s = s.toString();
if(s.split('.').length === 1) {
return parseInt(s).toLocaleString();
}
}
let result = '';
if(n === 0) {
result = parseInt(s).toLocaleString();
} else {
const l = s.split('.')[0].split('').reverse(),
r = s.split('.')[1];
let t = '';
for (let i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? ',' : '');
}
result = t.split('').reverse().join('') + '.' + r;
}
if (flag) {
result = '-' + result;
}
return result;
}
Over!!!!