日常开发过程 功能点记录
要求:根据层级数据计算出合计值,修改每一个子项和删除子项时 合计结果同步更新
// 计算
calculateFeiyong(row) {
const totalPrice = row.children.reduce((prev, cur) => prev + cur.totalPrice*1, 0);
return totalPrice.toFixed(2);
},
// 更新父节点的 totalPrice 字段
updateParentNode(row) {
// 获取当前行的父节点的数据
const parentRow = this.tableData.find((x) => x.inventoryDetailId === row.parentId);
if (!parentRow) {
return;
}
// 当前节点是叶子节点,总价=单价*数量
if (!row.children.length) {
// 先计算叶子节点的总价
const totalPrice = Number(row.quantities) * Number(row.unitPrice);
row.totalPrice = (totalPrice || 0);
}
// 再计算父级节点的总价=所有子节点的总价之和
parentRow.totalPrice = this.calculateFeiyong(parentRow);
// 递归调用,一直递归到顶级父节点
this.updateParentNode(parentRow);
},
// 删除每一项(使用)
deleteData(row) {
this.$refs.xTable.remove(row);
this.updateParentNode(row);
}