|
|
@@ -148,6 +148,7 @@
|
|
|
>
|
|
|
<template slot-scope="scope">
|
|
|
<el-button
|
|
|
+ v-if="scope.row.parentid == '-1'"
|
|
|
type="text"
|
|
|
title="添加行"
|
|
|
@click="handleAddItem(scope.row)"
|
|
|
@@ -1367,48 +1368,64 @@
|
|
|
// })
|
|
|
},
|
|
|
handleAddItem(row) {
|
|
|
- // 判断parentId是否为空,是就添加父项,否则添加子项
|
|
|
- const rowIndex = this.costAuditData.indexOf(row)
|
|
|
- if (rowIndex === -1) {
|
|
|
- this.$message.error('未找到指定行')
|
|
|
+ // 判断是否为父项(可添加子项)
|
|
|
+ if (row.parentid !== '-1') {
|
|
|
+ this.$message.error('只能在父项上添加子项')
|
|
|
return
|
|
|
}
|
|
|
- // 确定新行的parentId
|
|
|
- let parentId = row.parentId || '-1'
|
|
|
- // 计算新行的orderNum(当前行的orderNum + 1)
|
|
|
- const newOrderNum = row.orderNum + 1
|
|
|
- // 整体后移逻辑:将所有orderNum大于等于新orderNum的行,其orderNum加1
|
|
|
- // 根据父项和子项关系判断是否需要移动
|
|
|
- this.costAuditData.forEach((item) => {
|
|
|
- // 如果是相同父项的子项,或者都是父项(parentId为'-1'),则需要移动
|
|
|
- if (
|
|
|
- (item.parentId === parentId ||
|
|
|
- (item.parentId === '-1' && parentId === '-1')) &&
|
|
|
- item.orderNum >= newOrderNum
|
|
|
- ) {
|
|
|
- item.orderNum += 1
|
|
|
+ // 获取当前父项的所有子项
|
|
|
+ const parentRowId = row.rowid // 使用当前行的rowid作为父ID
|
|
|
+ const children = this.costAuditData.filter(
|
|
|
+ (item) => item.parentid === parentRowId
|
|
|
+ )
|
|
|
+
|
|
|
+ // 找到当前父项的最后一个子项的 orderNum
|
|
|
+ let maxOrderNum = 0
|
|
|
+ children.forEach((child) => {
|
|
|
+ if (child.orderNum > maxOrderNum) {
|
|
|
+ maxOrderNum = child.orderNum
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- // 创建新元素
|
|
|
+ // 新行的 orderNum 为最大子项 orderNum + 1
|
|
|
+ const newOrderNum = maxOrderNum + 1
|
|
|
+
|
|
|
+ // 创建新行:作为当前父项的子项
|
|
|
const newItem = {
|
|
|
...row,
|
|
|
- parentId,
|
|
|
+ parentId: parentRowId,
|
|
|
+ parentid: parentRowId,
|
|
|
orderNum: newOrderNum,
|
|
|
rowid: this.generateUUID(),
|
|
|
id: null,
|
|
|
rvalue: '',
|
|
|
}
|
|
|
- // 循环表头,把值置空
|
|
|
- this.costAuditcolumn.forEach((item) => {
|
|
|
- newItem[item.label] = ''
|
|
|
- newItem[item.prop] = ''
|
|
|
+ console.log('children.length', children.length)
|
|
|
+
|
|
|
+ // 清空字段值
|
|
|
+ this.costAuditcolumn.forEach((col) => {
|
|
|
+ if (col.label === '序号') {
|
|
|
+ newItem[col.prop] = children.length + 1
|
|
|
+ } else {
|
|
|
+ newItem[col.label] = ''
|
|
|
+ newItem[col.prop] = ''
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
- // 在当前行后面插入新元素
|
|
|
- this.costAuditData.splice(rowIndex + 1, 0, newItem)
|
|
|
+ // 找到父项的最后一个子项在数组中的位置
|
|
|
+ let insertIndex = this.costAuditData.length // 默认插入到末尾
|
|
|
+
|
|
|
+ // 遍历数组,找到父项的最后一个子项的位置
|
|
|
+ for (let i = 0; i < this.costAuditData.length; i++) {
|
|
|
+ // 如果是当前父项的子项,更新插入位置为该子项后一位
|
|
|
+ if (this.costAuditData[i].parentid === row.rowid) {
|
|
|
+ insertIndex = i + 1
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- this.$message.success('行添加成功')
|
|
|
+ // 在最后一个子项后面插入新数据
|
|
|
+ this.costAuditData.splice(insertIndex, 0, newItem)
|
|
|
+ this.$message.success('子项添加成功')
|
|
|
},
|
|
|
handleDeleteItem(row) {
|
|
|
// 显示确认对话框
|