// 新增数据行的函数
async function addOne (index, row) { // 增加空白行,并改变焦点
let maxID
let maxID1
let maxID2
await getMethod(data.url,).then(res =>{
// 计算网页中表格现有数据的id字段,最大的ID值,并加1,作为空白行的ID值
if (data.searchResult.length >= 1) {
maxID1 = Math.max.apply(Math, data.searchResult.map(item => { return item.id })) + 1
} else {
maxID1 = 1
}
// 从后端获取最大的ID值
if (res.length >= 1) {
maxID2 = Math.max.apply(Math, res.map(item => { return item.id })) + 1
} else {
maxID2 = 1
}
// 比较两个id值大小1,取最大值,为空白数据行的 id
maxID = maxID1 > maxID2 ? maxID1 : maxID2
})
// 调用自定义函数 emptyFields()生成空白对象, 并添加到数据,以达到表格新增空白数据行的效果
data.searchResult.push(emptyFields( data.url, maxID)) // data.searchResult 是表格的所有数据 的组成的数组
// -------------------------------------------------
// 问题出在下面2条语句 ,await nextTick()...,
// 当表格中没有数据,并且只新增一行空白数据时,然后点取消,即删除刚才新增的行(执行下面的函数addCancle),再执行本函数报错。
// 接上面, 我刷新页面,再执行本函数又不报错了,
// 表格中有多条数据时执行本函数不报错
// 注释了下面2条语句执行本函数不报错
await nextTick()
editFocus.value.input.focus() // 光标焦点切换至该空白行,editFocus为 ref绑定的input元素
// 改变行数和页数
page.totalCount = page.totalCount + 1 // 总行数
page.pageCount = Math.ceil(data.searchResult.length / page.pageSize) // 总页数为当前页
page.currentPage = page.pageCount // 显示到最后一页
}
取消新增行的函数
function addCancle (index, row) { // 取消新增,即删除当前行
data.searchResult = data.searchResult.filter(x => x.id !== row.id)
page.totalCount = page.totalCount - 1
page.pageCount = Math.ceil(data.searchResult.length / page.pageSize)
page.currentPage = page.pageCount
}
报错信息如下
runtime-core.esm-bundler.js:6584 [Vue warn]: Unhandled error during execution of native event handler
at <ElAffix offset=300 >
at <CategoryOne onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy {…} > key=0 >
at <RouterView>
at <ElMain>
at <ElContainer>
at <Settings onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy {…} > key=0 >
at <RouterView>
at <ElTabs type="border-card" modelValue="/home/settings" onUpdate:modelValue=fn ... >
at <Home onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy {…} > >
at <RouterView>
at <App>
warn2 @ runtime-core.esm-bundler.js:6584
logError @ runtime-core.esm-bundler.js:6758
handleError @ runtime-core.esm-bundler.js:6750
(匿名) @ runtime-core.esm-bundler.js:6713
Promise.catch(异步)
callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:6712
invoker @ runtime-dom.esm-bundler.js:350
CategoryOne.vue:228 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'input')
at Proxy.addOne (CategoryOne.vue:228)
await getMethod(data.url,).then
把这个括号的逗号去掉