需求:公司人员需要批量处理一些数据,所以想要能从excel复制过来的一些数据可以直接粘贴到页面的表格上
先放图看一下大致效果
1.选中excel想要复制的内容,ctrl+c
2.鼠标点击页面要复制位置的单元格,ctrl+v
3.这边是粘贴到存货编码,然后根据粘贴到的存货编码去调用接口,获取到每一个存货编码对应下的存货名称、单位、税码。
一、html代码
因为复制有可能覆盖复制,从头或从中间,所以需要传入某一行的index去做判断
<template #default="{ row, rowIndex }">
<div @paste="copyData($event, rowIndex)">{{ row.wareId }}</div>
</template>
二、js代码
async copyData(e, rowIndex) {
this.copyDatas = e.clipboardData.getData('Text/plain') || window.clipboardData.getData('Text/plain') // 获取粘贴板的内容
const wareIdSTR = this.copyDatas.trim().split(/\s+/) // 因为获取到的是字符串,所以需要处理数据格式,这里是得到对应的数组
try {
const res = await this.paramsInentorys(payload)
if (res.length === 0) return this.$message.warning('当前存货编码下没有数据,请重新复制')
for (let i = 0; i < res.length; i++) {
this.tableData[i + rowIndex].wareId = res[i].itemCode // 存货编码
this.tableData[i + rowIndex].wareName = res[i].itemName // 存货名称
this.tableData[i + rowIndex].unit = res[i].salUnitMsr // 单位
this.tableData[i + rowIndex].vatGroup = res[i].vatGourpSa // 税码
if (i + rowIndex + 1 === this.tableData.length) {
this.tableData.push(Object.assign({}, TABLECONST)) // 因为表格没有新增行的按钮,所以需要判断下面是否要出来新的一行空白数据
// TABLECONST为表格新增的行数,类型为Object,内容是根据自己的表格列而变化的,但是我觉得应该都会有新增行按钮的,毕竟方便写。。。。。。。这里表示有泪~
}
}
} catch (error) {
return
}
}