VUE3中Element table表头动态展示合计信息(不是表尾合计)

一、背景

原型上需要对两个字段动态合计,输出摘要信息

原先想到是的Element的 :summary-method,发现不是动态,所以换监听来实现

二、vue代码

javascript 复制代码
   <el-table v-model="loading" :data="itemList">
          <el-table-column label="药品名称" prop="drugName" fixed min-width="100px" :show-overflow-tooltip="true"/>
          <el-table-column label="规格" prop="drugSpec" :show-overflow-tooltip="true"/>
          <el-table-column label="批号" prop="batchNo" :show-overflow-tooltip="true"/>
          <el-table-column label="账面数" prop="batchStockDesc" min-width="90px"/>
          <el-table-column label="盘存数" align="center">
            <el-table-column prop="stocktakeQty" min-width="150px">
              <template v-slot="scope">
                <el-input-number :disabled="!canEdit"
                                 v-model="scope.row.stocktakeQty"
                                 :min="0"
                                 controls-position="right"
                                 size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="单位" prop="unit" min-width="90px">
              <template #default="scope">
                <dict-tag :options="bd_plat_drug_unit" :value="scope.row.unit" :showValue="false"/>
              </template>
            </el-table-column>
            <el-table-column prop="stocktakeTinyqty" min-width="150px">
              <template v-slot="scope">
                <el-input-number :disabled="!canEdit"
                                 v-model="scope.row.stocktakeTinyqty"
                                 :min="0"
                                 controls-position="right"
                                 size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="小单位" prop="tinyUnit" min-width="90px">
              <template #default="scope">
                <dict-tag :options="bd_plat_drug_unit" :value="scope.row.tinyUnit" :showValue="false"/>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="零售" align="center">
            <el-table-column label="零售价" prop="retailPrice" min-width="100px" :show-overflow-tooltip="true"
                             align="right"/>
            <el-table-column label="盘前金额" prop="totalRetail" min-width="100px" :show-overflow-tooltip="true"
                             align="right"/>
            <el-table-column label="盘后金额" prop="afterTotalRetail" min-width="100px" :show-overflow-tooltip="true"
                             align="right">
              <template v-slot="scope">
                {{
                  scope.row.afterTotalRetail = computeTotalMoney(scope.row.stocktakeQty, scope.row.stocktakeTinyqty, scope.row.packageQty, scope.row.retailPrice)
                }}
              </template>
            </el-table-column>
            <el-table-column label="成本损溢金额" prop="totalLossoverRetail" min-width="120px" align="right">
              <template v-slot="scope">
                {{
                  scope.row.totalLossoverRetail = computeDifferenceMoney(scope.row.stocktakeQty, scope.row.stocktakeTinyqty, scope.row.packageQty, scope.row.retailPrice, scope.row.totalRetail)
                }}
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="成本" align="center">
            <el-table-column label="采购价" prop="purchasePrice" min-width="100px" :show-overflow-tooltip="true"
                             align="right"/>
            <el-table-column label="盘前金额" prop="totalPurchase" min-width="100px" :show-overflow-tooltip="true"
                             align="right"/>
            <el-table-column label="盘后金额" prop="afterTotalPurchase" min-width="100px" :show-overflow-tooltip="true"
                             align="right">
              <template v-slot="scope">
                {{
                  scope.row.afterTotalPurchase = computeTotalMoney(scope.row.stocktakeQty, scope.row.stocktakeTinyqty, scope.row.packageQty, scope.row.purchasePrice)
                }}
              </template>
            </el-table-column>
            <el-table-column label="成本损溢金额" prop="totalLossoverPurchase" min-width="120px"
                             :show-overflow-tooltip="true" align="right">
              <template v-slot="scope">
                {{
                  scope.row.totalLossoverPurchase = computeDifferenceMoney(scope.row.stocktakeQty, scope.row.stocktakeTinyqty, scope.row.packageQty, scope.row.purchasePrice, scope.row.totalPurchase)
                }}
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="生产企业" prop="firmName" min-width="80px" :show-overflow-tooltip="true"/>
          <el-table-column label="产地" prop="producerName" min-width="80px" :show-overflow-tooltip="true"/>
          <el-table-column label="库位码" prop="locationCode" min-width="100px" :show-overflow-tooltip="true"/>
          <el-table-column label="操作" fixed="right" min-width="60px" align="center" v-if="canEdit"
                           class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="primary" icon="Delete" title="删除" @click="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>

其中代码,赋值给totalLossoverRetail 才能保证,后期监听时数据有发生变化

javascript 复制代码
                {{
                  scope.row.totalLossoverRetail = computeDifferenceMoney(scope.row.stocktakeQty, scope.row.stocktakeTinyqty, scope.row.packageQty, scope.row.retailPrice, scope.row.totalRetail)
                }}

三、方法代码

javascript 复制代码
watch(itemList, () => {
  console.log(itemList.value, 'itemList')
  let totalLossoverRetail = 0
  let totalLossoverPurchase = 0
  itemList.value.forEach(item => {
    totalLossoverRetail = Number(totalLossoverRetail) + Number(item.totalLossoverRetail);
    totalLossoverPurchase = Number(totalLossoverPurchase) + Number(item.totalLossoverPurchase);
  })
  sumDescription.value = '成本损溢金额 ' + totalLossoverPurchase + ' 零售损溢金额  ' + totalLossoverRetail
}, {deep: true});

其中开启深度监听

四、效果

相关推荐
peachSoda79 分钟前
随手记:鼠标触顶方法
前端·javascript·vue.js
疯狂的沙粒9 分钟前
Vue项目开发 formatData 函数有哪些常用的场景?
前端·javascript·vue.js
逆旅行天涯10 分钟前
【功能实现】bilibili顶部鼠标跟随效果怎么实现?
前端·javascript·vue
毛毛三由13 分钟前
【10分钟学习Vue自定义指令开发】鼠标放置提示指令
前端·javascript·vue.js
一秒美工助手15 分钟前
鼠标经过遮罩效果 详情页阿里巴巴国际站外贸跨境电商装修运营 详情页装修无线端装修手机装修设计代码证书滚动特效效果代码,自定义内容代码模板模块设计设置装修
前端·javascript·html·计算机外设
桑榆肖物1 小时前
将 .NET Aspire 添加到现有应用:前端 JavaScript 项目处理
前端·javascript·.net
Wh1teR0se3 小时前
[极客大挑战 2019]Secret File--详细解析
前端·web安全·网络安全
ZhaiMou4 小时前
HTML5拖拽API学习 托拽排序和可托拽课程表
前端·javascript·学习·html5
code_shenbing7 小时前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计
白臻8 小时前
使用element-plus el-table中使用el-image层级冲突table表格会覆盖预览的图片等问题
前端·vue.js·elementui