vxeTable 复刻区域选择、拖动柄
介绍
vxetable是一个基于 Vue(支持 Vue3) 的 PC 端全功能表格组件,满足你对 table 绝大多数需求,可与任意组件库完美兼容。VXE Table面向现代浏览器,高效的简洁 API 设计,模块化表格、按需加载、扩展接口,为单行编辑表格而设计,支持增删改查及更多扩展,强大的功能的同时兼具性能。
Vxe-table 提供诸多功能:虚拟滚动、懒加载、快捷菜单、数据校验、树形结构、打印导出、表单渲染、数据分页、虚拟列表、模态窗口、自定义模板、渲染器,每一个功能做细了都不容易。
需要的直接点击传送门:https://vxetable.cn/
缘起
我们的系统的表格,需要支持像excel那样区域选择,进行复制粘贴,拖拽区域的右下方需要有一个拖动柄可以进行复制。使用的vxe-table是有这个功能的,但是这个功能需要收费,所以就自己参考着写(CV)了一个。
这些代码仅供学习交流 。如果这些代码能帮助您一点点思路,欢迎点赞收藏,如果觉得侵犯了您的权益,可以联系作者删除。
参考
1. https://blog.csdn.net/wanghanlu_/article/details/132376047?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170591700916800222863864%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=170591700916800222863864&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-132376047-null-null.142^v99^pc_search_result_base9&utm_term=vxetable%E5%8C%BA%E5%9F%9F%E9%80%89%E6%8B%A9&spm=1018.2226.3001.4187
2. https://blog.csdn.net/weixin_45366616/article/details/133739353?spm=1001.2014.3001.5501
实现
实现拖动区域选择,如果是vue2版本可以使用参考1。如果是vue3版本的可以使用参考2。使用了上述的代码,你的vxe-table表格就可以实现区域选择。
我是vue3版本的,所以使用的是参考2。复制下来改吧改吧就能区域选择,但是需要加上按键复制粘贴和拖动柄的功能。接下来就直接上代码。
<template>
<!-- 正常区域的框 -->
<div class="vxe-table--cell-area" ref="cellarea">
<!-- 选择显示的框 -->
<span class="vxe-table--cell-main-area">
<!-- 拖动柄的类名 -->
<span class="vxe-table--cell-main-area-btn"></span>
</span>
<span class="vxe-table--cell-active-area"></span>
<span class="vxe-table--cell-extend-area"></span>
</div>
<!-- 左侧fixed区域的框 -->
<div class="vxe-table--cell-area" ref="leftfixedcellarea">
<span class="vxe-table--cell-main-area">
<span class="vxe-table--cell-main-area-btn"></span>
</span>
<span class="vxe-table--cell-active-area"></span>
<span class="vxe-table--cell-extend-area"></span>
</div>
<!-- 右侧fixed区域的框 -->
<div class="vxe-table--cell-area" ref="rightfixedcellarea">
<span class="vxe-table--cell-main-area">
<span class="vxe-table--cell-main-area-btn"></span>
</span>
<span class="vxe-table--cell-active-area"></span>
<span class="vxe-table--cell-extend-area"></span>
</div>
<div class="apptable table-padding-7">
<vxe-grid
v-bind="tableConfig"
ref="tableCom"
@edit-closed="editCollectClosedEvent"
@current-change="currentCollectChangeEvent"
@checkbox-change="selectCollectChangeEvent"
@checkbox-all="selectCollectChangeEvent"
>
</vxe-grid>
</div>
</template>
let isSelecting = ref(false);
let selectionStart = reactive({ rowIndex: -1, cellIndex: -1 });
let selectionEnd = reactive({ rowIndex: -1, cellIndex: -1 });
let cellarea = ref();
let leftfixedcellarea = ref();
let rightfixedcellarea = ref();
const getTablexGrid = () => {
return tableCom.value;
};
const tableCopy = event => {
if (event.ctrlKey && event.key === 'c') {
console.log('Ctrl + C event triggered!');
if (selectionEnd.rowIndex !== -1) {
copyEvent(event);
}
} else if (event.ctrlKey && event.key === 'v') {
window.addEventListener('paste', event => {
if (selectionEnd.rowIndex === -1) {
} else {
event.preventDefault();
let paste = (event.clipboardData || window.clipboardData).getData('text');
if (isEditView.value) {
pasteEvent(paste);
}
}
});
}
};
const addListener = () => {
nextTick(() => {
if (getTablexGrid().$el) {
window.addEventListener('mouseup', tbodymouseup);
window.addEventListener('keydown', tableCopy);
let tbody = getTablexGrid() && getTablexGrid().$el.querySelector('.vxe-table--main-wrapper table tbody');
if (tbody) {
tbody.addEventListener('mousedown', tbodymousedown);
tbody.addEventListener('mousemove', tbodymousemove);
tbody.addEventListener('mouseout', throttle(tbodymouseout, 50));
tbody.addEventListener('click', tableCellClick);
tbody.oncontextmenu = tableCellMenuClick;
}
let bodyWrapper = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper');
if (bodyWrapper) {
bodyWrapper.appendChild(cellarea.value);
}
setTimeout(() => {
let leftfixedtbody = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper table tbody');
if (leftfixedtbody) {
leftfixedtbody.addEventListener('mousedown', tbodymousedown);
leftfixedtbody.addEventListener('mousemove', tbodymousemove);
leftfixedtbody.addEventListener('mouseout', throttle(tbodymouseout, 50));
leftfixedtbody.addEventListener('click', tableCellClick);
leftfixedtbody.oncontextmenu = tableCellMenuClick;
}
let leftFixedBodyWrapper = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper');
if (leftFixedBodyWrapper) {
leftFixedBodyWrapper.appendChild(leftfixedcellarea.value);
}
let rightfixedtbody = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper table tbody');
if (rightfixedtbody) {
rightfixedtbody.addEventListener('mousedown', tbodymousedown);
rightfixedtbody.addEventListener('mousemove', tbodymousemove);
rightfixedtbody.addEventListener('mouseout', throttle(tbodymouseout, 50));
rightfixedtbody.addEventListener('click', tableCellClick);
rightfixedtbody.oncontextmenu = tableCellMenuClick;
}
let rightFixedBodyWrapper = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper');
if (rightFixedBodyWrapper) {
rightFixedBodyWrapper.appendChild(rightfixedcellarea.value);
}
}, 100);
}
});
};
const removeListener = () => {
nextTick(() => {
if (getTablexGrid().$el) {
window.removeEventListener('mouseup', tbodymouseup);
window.removeEventListener('keydown', tableCopy);
window.removeEventListener('paste', event => {
if (selectionEnd.rowIndex === -1) {
} else {
event.preventDefault();
let paste = (event.clipboardData || window.clipboardData).getData('text');
if (isEditView.value) {
pasteEvent(paste);
}
}
});
let tbody = getTablexGrid() && getTablexGrid().$el.querySelector('.vxe-table--main-wrapper table tbody');
if (tbody) {
tbody.removeEventListener('mousedown', tbodymousedown);
tbody.removeEventListener('mousemove', tbodymousemove);
tbody.removeEventListener('mouseout', throttle(tbodymouseout, 50));
tbody.removeEventListener('click', tableCellClick);
tbody.oncontextmenu = tableCellMenuClick;
}
let bodyWrapper = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper');
if (bodyWrapper) {
bodyWrapper.appendChild(cellarea.value);
}
setTimeout(() => {
let leftfixedtbody = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper table tbody');
if (leftfixedtbody) {
leftfixedtbody.removeEventListener('mousedown', tbodymousedown);
leftfixedtbody.removeEventListener('mousemove', tbodymousemove);
leftfixedtbody.removeEventListener('mouseout', throttle(tbodymouseout, 50));
leftfixedtbody.removeEventListener('click', tableCellClick);
leftfixedtbody.oncontextmenu = tableCellMenuClick;
}
let leftFixedBodyWrapper = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper');
if (leftFixedBodyWrapper) {
leftFixedBodyWrapper.appendChild(leftfixedcellarea.value);
}
let rightfixedtbody = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper table tbody');
if (rightfixedtbody) {
rightfixedtbody.removeEventListener('mousedown', tbodymousedown);
rightfixedtbody.removeEventListener('mousemove', tbodymousemove);
rightfixedtbody.removeEventListener('mouseout', throttle(tbodymouseout, 50));
rightfixedtbody.removeEventListener('click', tableCellClick);
rightfixedtbody.oncontextmenu = tableCellMenuClick;
}
let rightFixedBodyWrapper = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper');
if (rightFixedBodyWrapper) {
rightFixedBodyWrapper.appendChild(rightfixedcellarea.value);
}
}, 100);
}
});
};
let isExtendSelecting = ref(false);
let selectionExtendOldStart = reactive({ rowIndex: -1, cellIndex: -1 });
let selectionExtendOldEnd = reactive({ rowIndex: -1, cellIndex: -1 });
let selectionExtendStart = reactive({ rowIndex: -1, cellIndex: -1 });
let selectionExtendEnd = reactive({ rowIndex: -1, cellIndex: -1 });
const extendmousedown = (event: MouseEvent) => {
event.stopPropagation();
if (event.button === 0) {
selectionExtendOldStart = cloneDeep(selectionStart);
selectionExtendOldEnd = cloneDeep(selectionEnd);
selectionExtendStart = cloneDeep(selectionStart);
isExtendSelecting.value = true;
}
};
const tbodymousedown = (event: MouseEvent) => {
event.stopPropagation();
if (event.button === 0) {
selectionStart = getCellPosition(event.target);
if (!isExtendSelecting.value) {
isSelecting.value = true;
}
}
};
const tbodymousemove = (event: MouseEvent) => {
if (event.button === 0) {
if (isExtendSelecting.value) {
selectionExtendEnd = getCellExtendPosition(event.target);
if (selectionExtendEnd) {
console.log('设置样式 打开extend弹窗');
setselectedExtendCellArea();
} else {
destroyExtendAreaBox();
}
return;
}
if (isSelecting.value) {
selectionEnd = getCellPosition(event.target);
setselectedCellArea();
}
return;
}
};
const isUp = ref(false);
const isLeft = ref(false);
const fillDatas = () => {
console.log(selectionExtendOldStart, selectionExtendOldEnd);
console.log(selectionStart, selectionEnd);
let tableData = getTablexGrid().getTableData().visibleData;
let tableColumn = getTablexGrid().getTableColumn().visibleColumn;
let copydata = getDataByArea(selectionExtendOldStart, selectionExtendOldEnd, true);
const lines = copydata.length;
const cells = copydata[0].length;
console.log(lines, cells);
if (selectionExtendOldEnd.cellIndex === selectionEnd.cellIndex) {
if (selectionExtendOldEnd.rowIndex < selectionEnd.rowIndex) {
const needFillStartSelectionRowIndex = selectionExtendOldEnd.rowIndex + 1;
const needFillLines = selectionEnd.rowIndex - selectionExtendOldEnd.rowIndex;
for (let i = 0; i < needFillLines; i++) {
let line = copydata[i % lines];
if (selectionStart.rowIndex + i > tableData.length - 1) break;
let row = tableData[needFillStartSelectionRowIndex + i];
for (let j = 0; j < line.length; j++) {
if (selectionStart.cellIndex + j > tableColumn.length - 1) break;
let column = tableColumn[selectionStart.cellIndex + j];
if (column.editRender) {
row[column.field] = line[j];
}
}
}
} else if (selectionEnd.rowIndex < selectionExtendOldEnd.rowIndex) {
isUp.value = true;
console.log('往上复制');
const needFillStartSelectionRowIndex = selectionStart.rowIndex - 1;
const needFillLines = selectionStart.rowIndex - selectionEnd.rowIndex;
for (let i = needFillStartSelectionRowIndex; i >= selectionEnd.rowIndex; i--) {
let line = copydata[(selectionStart.rowIndex - i + 1) % lines];
if (i < 0) break;
let row = tableData[i];
for (let j = 0; j < line.length; j++) {
if (selectionStart.cellIndex + j > tableColumn.length - 1) break;
let column = tableColumn[selectionStart.cellIndex + j];
if (column.editRender) {
row[column.field] = line[j];
}
}
}
}
} else {
if (selectionExtendOldEnd.cellIndex < selectionEnd.cellIndex) {
const needFillStartSelectionCellIndex = selectionExtendOldEnd.cellIndex + 1;
const needFillLines = selectionEnd.cellIndex - selectionExtendOldEnd.cellIndex;
for (let i = 0; i < lines; i++) {
if (selectionStart.rowIndex + i > tableData.length - 1) break;
let row = tableData[selectionStart.rowIndex + i];
for (let j = 0; j < needFillLines; j++) {
if (needFillStartSelectionCellIndex + j > tableColumn.length - 1) break;
let column = tableColumn[needFillStartSelectionCellIndex + j];
if (column.editRender) {
row[column.field] = copydata[i][j % cells];
}
}
}
} else if (selectionEnd.cellIndex < selectionExtendOldEnd.cellIndex) {
isLeft.value = true;
console.log('往左复制');
const needFillStartSelectionCellIndex = selectionExtendOldStart.cellIndex - 1;
const needFillLines = selectionExtendOldStart.cellIndex - selectionEnd.cellIndex;
for (let i = 0; i < lines; i++) {
if (selectionStart.rowIndex + i > tableData.length - 1) break;
let row = tableData[selectionStart.rowIndex + i];
for (let j = 0; j < needFillLines; j++) {
if (needFillStartSelectionCellIndex + j > tableColumn.length - 1) break;
let column = tableColumn[needFillStartSelectionCellIndex - j];
if (column.editRender) {
row[column.field] = copydata[i][(selectionExtendOldStart.cellIndex - j + 1) % cells];
}
}
}
}
}
nextTick(() => {
afterPasteMethodFunC(getTablexGrid().getUpdateRecords());
});
};
const tbodymouseup = (event: MouseEvent) => {
if (event.button === 0) {
isSelecting.value = false;
if (isExtendSelecting.value) {
if (isUp.value) {
selectionStart = selectionExtendOldEnd;
} else {
selectionStart = selectionExtendOldStart;
}
if (isLeft.value) {
selectionStart = selectionExtendOldEnd;
} else {
selectionStart = selectionExtendOldStart;
}
if (selectionExtendEnd) {
selectionEnd.rowIndex = selectionExtendEnd.rowIndex;
selectionEnd.cellIndex = selectionExtendEnd.cellIndex;
fillDatas();
setselectedCellArea();
destroyExtendAreaBox();
}
isExtendSelecting.value = false;
isUp.value = false;
isLeft.value = false;
}
}
};
let outevent = ref();
const tbodymouseout = (event: MouseEvent) => {
outevent.value = event;
if (isSelecting.value) {
const timer = setInterval(() => {
if (isSelecting.value) {
var table = getTablexGrid().$el.querySelector('.vxe-table--body-wrapper table');
if (outevent.value.clientX > table.parentElement.getBoundingClientRect().right - 30) {
var maxScrollPosition = table.parentElement.scrollWidth - table.parentElement.clientWidth;
if (table.parentElement.scrollLeft < maxScrollPosition) {
table.parentElement.scrollLeft += 10;
}
} else if (outevent.value.clientX < table.parentElement.getBoundingClientRect().left + 30) {
if (table.parentElement.scrollLeft > 0) {
table.parentElement.scrollLeft -= 10;
}
}
} else {
clearInterval(timer);
}
}, 200);
}
};
const throttle = (fn: Function, delay: number) => {
const canRun = ref(true);
return (...args: any[]) => {
if (!canRun.value) return;
canRun.value = false;
setTimeout(() => {
fn(...args);
canRun.value = true;
}, delay);
};
};
const getCellPosition = (cell: any) => {
while (cell.tagName !== 'TD') {
cell = cell.parentElement;
}
let visibleColumn = getTablexGrid().getTableColumn().visibleColumn;
const cellIndex = visibleColumn.findIndex((col: { id: any }) => {
return col.id == cell.getAttribute('colid');
});
let visibleData = getTablexGrid().getTableData().visibleData;
const rowIndex = visibleData.findIndex((row: { Guid: any }) => {
return row.Guid == cell.parentElement.getAttribute('rowid');
});
return { rowIndex, cellIndex };
};
const getCellExtendPosition = (cell: any) => {
while (cell.tagName !== 'TD') {
cell = cell.parentElement;
}
let visibleColumn = getTablexGrid().getTableColumn().visibleColumn;
const cellIndex = visibleColumn.findIndex((col: { id: any }) => {
return col.id == cell.getAttribute('colid');
});
let visibleData = getTablexGrid().getTableData().visibleData;
const rowIndex = visibleData.findIndex((row: { Guid: any }) => {
return row.Guid == cell.parentElement.getAttribute('rowid');
});
if (cellIndex === selectionExtendOldEnd.cellIndex || rowIndex === selectionExtendOldEnd.rowIndex) {
return { rowIndex, cellIndex };
} else {
return false;
}
};
const setselectedCellArea = () => {
var activeElement = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-active-area');
var mainElement = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area');
var mainElementBtn = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area-btn');
var leftFixedActiveElement = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-active-area');
var leftFixedMainElement = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area');
var leftFixedMainElementBtn = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area-btn');
var rightFixedActiveElement = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-active-area');
var rightFixedMainElement = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area');
var rightFixedMainElementBtn = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area-btn');
var elements = [activeElement, mainElement, leftFixedActiveElement, leftFixedMainElement, rightFixedActiveElement, rightFixedMainElement];
var elementsContens = [mainElementBtn, leftFixedMainElementBtn, rightFixedMainElementBtn];
let area = getAreaBoxPosition();
if (area) {
var { width, height, left, top, right } = area;
} else {
return;
}
elements.forEach((element, index) => {
if (element) {
element.style.width = `${width}px`;
element.style.height = `${height}px`;
element.style.top = `${top}px`;
element.style.display = 'block';
if (index <= elements.length - 1 - 2) {
element.style.left = `${left}px`;
} else {
element.style.right = `${right}px`;
}
}
});
elementsContens.forEach(element => {
if (element) {
element.style.display = 'block';
if (isEditView.value) {
element.addEventListener('mousedown', extendmousedown);
}
}
});
openAreaBox();
};
const setselectedExtendCellArea = () => {
var extendElement = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-extend-area');
var leftFixedExtendElement = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-extend-area');
var rightFixedExtendElement = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-extend-area');
var elements = [extendElement, leftFixedExtendElement, rightFixedExtendElement];
let area = getAreaBoxExtendPosition();
if (area) {
var { width, height, left, top, right } = area;
} else {
return;
}
elements.forEach((element, index) => {
if (element) {
element.style.width = `${width}px`;
element.style.height = `${height}px`;
element.style.top = `${top}px`;
element.style.display = 'block';
if (index <= elements.length - 1 - 1) {
element.style.left = `${left}px`;
} else {
element.style.right = `${right}px`;
}
}
});
};
const removeExtendListener = () => {
nextTick(() => {
var mainElementBtn = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area-btn');
var leftFixedMainElementBtn = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area-btn');
var rightFixedMainElementBtn = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-main-area-btn');
var elementsContens = [mainElementBtn, leftFixedMainElementBtn, rightFixedMainElementBtn];
elementsContens.forEach(element => {
if (element) {
element.removeEventListener('mousedown', extendmousedown);
element.style.display = 'none';
}
});
});
};
const destroyExtendAreaBox = () => {
var element = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-extend-area');
if (element) {
element.style.display = 'none';
}
element = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-extend-area');
if (element) {
element.style.display = 'none';
}
element = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-extend-area');
if (element) {
element.style.display = 'none';
}
};
const getAreaBoxPosition = () => {
let startRowIndex = selectionStart.rowIndex;
let endRowIndex = selectionEnd.rowIndex;
let startColumnIndex = selectionStart.cellIndex;
let endColumnIndex = selectionEnd.cellIndex;
let visibleColumn = getTablexGrid().getTableColumn().visibleColumn;
let visibleData = getTablexGrid().getTableData().visibleData;
if (startColumnIndex < 0 || endColumnIndex < 0 || startRowIndex < 0 || endRowIndex < 0) return;
var maxColumnIndex = visibleColumn.length - 1;
var maxRowIndex = visibleData.length - 1;
if (endColumnIndex > maxColumnIndex) {
endColumnIndex = maxColumnIndex;
}
if (endRowIndex > maxRowIndex) {
endRowIndex = maxRowIndex;
}
let width = 0,
height = 0,
left = 0,
top = 0,
right = 0;
visibleColumn.forEach((col: { renderWidth: number }, index: number) => {
if (startColumnIndex <= endColumnIndex) {
if (index < startColumnIndex) {
left += col.renderWidth;
}
if (index > endColumnIndex) {
right += col.renderWidth;
}
if (startColumnIndex <= index && index <= endColumnIndex) {
width += col.renderWidth;
}
} else {
if (index < endColumnIndex) {
left += col.renderWidth;
}
if (index > startColumnIndex) {
right += col.renderWidth;
}
if (startColumnIndex >= index && index >= endColumnIndex) {
width += col.renderWidth;
}
}
});
if (startRowIndex <= endRowIndex) {
height = (endRowIndex - startRowIndex + 1) * 36;
top = startRowIndex * 36;
} else {
height = (startRowIndex - endRowIndex + 1) * 36;
if (isUp.value) {
height = height + (selectionExtendOldEnd.rowIndex - selectionExtendOldStart.rowIndex) * 36;
}
top = endRowIndex * 36;
}
return { width, height, left, top, right };
};
const destroyAreaBox = () => {
var element = getTablexGrid().$el.querySelector('.vxe-table--main-wrapper .vxe-table--body-wrapper .vxe-table--cell-area');
if (element) {
element.style.display = 'none';
}
element = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-left-wrapper .vxe-table--body-wrapper .vxe-table--cell-area');
if (element) {
element.style.display = 'none';
}
element = getTablexGrid().$el.querySelector('.vxe-table--fixed-wrapper .vxe-table--fixed-right-wrapper .vxe-table--body-wrapper .vxe-table--cell-area');
if (element) {
element.style.display = 'none';
}
};
const tableCellClick = (e: MouseEvent) => {
if (!isSelecting.value) {
try {
selectionStart = getCellPosition(e.target);
selectionEnd = selectionStart;
console.log(selectionStart, selectionEnd);
setselectedCellArea();
} catch (error) {}
}
};
const tableCellMenuClick = (e: MouseEvent) => {
if (!isSelecting.value) {
let currentCellPosition = getCellPosition(e.target);
var horizontalFlag;
var verticalFlag;
if (selectionStart.cellIndex <= selectionEnd.cellIndex) {
horizontalFlag = selectionStart.cellIndex <= currentCellPosition.cellIndex && currentCellPosition.cellIndex <= selectionEnd.cellIndex;
} else {
horizontalFlag = selectionEnd.cellIndex <= currentCellPosition.cellIndex && currentCellPosition.cellIndex <= selectionStart.cellIndex;
}
if (selectionStart.rowIndex <= selectionEnd.rowIndex) {
verticalFlag = selectionStart.rowIndex <= currentCellPosition.rowIndex && currentCellPosition.rowIndex <= selectionEnd.rowIndex;
} else {
verticalFlag = selectionEnd.rowIndex <= currentCellPosition.rowIndex && currentCellPosition.rowIndex <= selectionStart.rowIndex;
}
if (horizontalFlag && verticalFlag) {
} else {
selectionStart = getCellPosition(e.target);
selectionEnd = selectionStart;
setselectedCellArea();
}
}
};
const getDataByArea = (start, end, event) => {
const data = [];
for (let i = start.rowIndex; i <= end.rowIndex; i++) {
const row = [];
for (let j = start.cellIndex; j <= end.cellIndex; j++) {
let data = getTablexGrid() && getTablexGrid().getTableData().visibleData[i];
let head = getTablexGrid() && getTablexGrid().getTableColumn().visibleColumn[j];
row.push(data[head.field]);
}
data.push(row);
}
return data;
};
const copyEvent = async (event: any) => {
console.log('是否正在进行滑动选中操作:', isSelecting.value);
console.log('单元格起始位置:索引:', selectionStart);
console.log('单元格结束位置:索引:', selectionEnd);
if (tableCom.value) {
const data = getDataByArea(selectionStart, selectionEnd, event);
const result = data.reduce((a: string, b) => a + b.join('\t') + '\n', '').slice(0, -1);
if (XEClipboard.copy(result)) {
VXETable.modal.message({
status: 'success',
content: '复制成功'
});
}
if (getTablexGrid() && getTablexGrid().$el && getTablexGrid().$el.querySelector('.vxe-table--render-wrapper')) {
selectionStart.cellIndex = -1;
selectionStart.rowIndex = -1;
selectionEnd.cellIndex = -1;
selectionEnd.rowIndex = -1;
destroyAreaBox();
}
}
};
const pasteEvent = async (text: any) => {
if (text && tableCom.value) {
let tableData = getTablexGrid() && getTablexGrid().getTableData().visibleData;
let tableColumn = getTablexGrid() && getTablexGrid().getTableColumn().visibleColumn;
text = text.replace(/^\r\n+|\r\n+$/g, '');
var snsArr = text.split(/\r\n+/);
var tArr = snsArr.map(value => {
return value.split('\t');
});
for (var i = 0; i < tArr.length; i++) {
let line = tArr[i];
if (selectionStart.rowIndex + i > tableData.length - 1) break;
let row = tableData[selectionStart.rowIndex + i];
for (var j = 0; j < line.length; j++) {
if (selectionStart.cellIndex + j > tableColumn.length - 1) break;
let column = tableColumn[selectionStart.cellIndex + j];
if (column.editRender) {
row[column.field] = line[j];
}
}
}
}
};
onBeforeUnmount(() => {
removeListener();
if (getTablexGrid() && getTablexGrid().$el && getTablexGrid().$el.querySelector('.vxe-table--render-wrapper')) {
removeExtendListener();
}
});