vue 解决el-table 表体数据发生变化时,未重新渲染问题

news2024/10/2 16:22:00
效果图

 父组件中数量改变后总数重新计算


子组件完整代码
<template>
  <el-table
    show-summary
    ref="multipleTable"
    v-bind="$props"
    @selection-change="handleSelectionChange"
    @row-click="handleRowClick"
    :summary-method="getSummaries"
    <--这里是关键-->
    :key="certKey"
  >
    <slot/>
  </el-table>
</template>

<script>
export default {
  props: ['data','certKey'],
  data() {
    return {
      selectionData: []
    }
  },

  watch: {
    certKey: {
      handler(val) { 
      }
    },
  },
  methods: {
    // 向父组件共享数据
    handleSelectionChange(val) {
      this.$emit('selection-change', val)
    },
    handleRowClick(row, column, event) {
      // selectionData -- 已选的数据
      let index = this.selectionData.findIndex(item => {
        // 判断已选数组中是否已存在该条数据
        return item.id == row.id
      })
      if (index == -1) {
        // 如果未存在,设置已选状态,并在list中添加这条数据
        this.$refs.multipleTable.toggleRowSelection(row, true); //设置复选框为选中状态
        this.selectionData.push(row)
      } else {
        // 如果已存在,设置未选状态,并在list中删除这条数据
        this.$refs.multipleTable.toggleRowSelection(row, false); //设置复选框为未选状态
        this.selectionData.splice(index, 1)
      }
    },
    //getSummaries()返回的是一个展示的数组 
      getSummaries(param) {
        const { columns, data } = param;
        const sums = []; //声明变量
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = "合计";
            return;
          }
          //计算planQuantity字段的统计
          const values = data.map((item) => Number(item.planQuantity));
          //需要显示和合计纵列
          if ( column.property == "quantity"   ) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
          }
        });
        return sums;
      },

    }
}
</script>

父组件代码 WmsTable 是子组件名 WmsTable 中的数量字段quantity修改时重新计算合计

<template>
  <div class="app-container"> 
    </el-form>
 
    <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id" v-if="true"/>
      <el-table-column label="编号" align="center" prop="materialNo"/>
      <el-table-column label="名称" align="center" prop="materialName"/>
      <el-table-column label="状态" align="center" prop="status"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:material:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:material:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料申请单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="编号" prop="materialNo">
          <el-input v-model="form.materialNo" placeholder="请输入编号"/>
        </el-form-item>
        <el-form-item label="名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="总数量" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入总数量"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>

      <div>
        <div class="ops">
          <el-button type="primary" plain="plain" size="small" @click="handleLeaderAdd(2)">添加物料</el-button>
        </div>
        <WmsTable :data="form.details" @selection-change="handleSelectionChange" ref="wmstable" :certKey="certKey">
          <el-table-column type="selection" width="100" align="center"></el-table-column>
          <el-table-column label="物料名" align="center" prop="applyMaterialName"></el-table-column>
          <el-table-column label="物料编号" align="center" prop="applyMaterialNo"></el-table-column>
          <el-table-column label="物料类型" align="center" prop="itemTypeName"></el-table-column>
          <el-table-column label="数量" align="center" prop="quantity" row-key="quantity" width="150">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.planQuantity"  @change="recalculateTotal" placeholder="计划数量" :min="1" size="small"  :max="2147483647"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <a class="red" @click="form.details.splice(scope.$index, 1)">删除</a>
            </template>
          </el-table-column>
        </WmsTable>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible="modalObj.show" :title="modalObj.title" :width="modalObj.width" @close="modalObj.cancel">
      <template v-if="modalObj.component === 'add-item'">
        <item-select ref="item-select" :data="this.form.details"></item-select>
      </template>
      <span slot="footer">
        <el-button v-if="modalObj.cancel" @click="modalObj.cancel">取消</el-button>
        <el-button v-if="modalObj.ok" type="primary" @click="modalObj.ok">确认</el-button>
      </span>
    </el-dialog>
    <!-- 添加 用户对话框 -->
    <el-dialog title="选择负责人" :visible.sync="leaderOpen" width="50%" append-to-body destroy-on-close :key="leaderOpen">
      <el-row :gutter="20">
        <el-col :span="24" :xs="24">
          <div class="relatedRoleDIv">
            <el-form :model="roleRelateObj" ref="form" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="姓名" prop="realName">
                <el-input
                  v-model="roleRelateObj.realName"
                  placeholder="请输入姓名"
                  clearable
                  size="small"
                  style="width: 240px"
                  @keyup.enter.native="getListItem"
                />
              </el-form-item>
              <el-form-item label="手机号码" prop="phonenumber">
                <el-input v-model="roleRelateObj.phonenumber" placeholder="请输入手机号码" clearable size="small"
                          style="width: 240px" @keyup.enter.native="getListItem"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getListItem">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery2">重置</el-button>
              </el-form-item>
            </el-form>
            <el-table :data="userList" style="height: 484px" @selection-change="handleSelectionChange2">
              <template v-if="addType == 1">
                <el-table-column align="center" width="65" :type="addType == 1 ? '' : 'selection'">
                  <template scope="scope">
                    <el-radio :label="scope.$index" v-model="radio" @change.native="getCurrentRow(scope.row)">
                      {{ `` }}
                    </el-radio>
                  </template>
                </el-table-column>
              </template>
              <el-table-column type="selection" width="55" v-if="addType == 2">
              </el-table-column>
              <el-table-column label="物料名"   prop="applyMaterialName"/>
              <el-table-column label="物料编号"  prop="applyMaterialNo" :show-overflow-tooltip="true"/>
              <el-table-column label="物料类型" align="center"  prop="itemTypeName"  />
              <el-table-column label="计量单位" align="center"  prop="unitName"  />
            </el-table>
            <pagination
              v-show="total2 > 0" :total="total2" :page.sync="roleRelateObj.pageNum"
              :limit.sync="roleRelateObj.pageSize"
              @pagination="getListItem"
            />
            <div style="height: 35px;margin-top:40px;display: flex;    justify-content: flex-end;">
              <el-button :loading="buttonLoading" type="primary" @click="submitLeaderForm">确 定</el-button>
              <el-button @click="leaderOpen = false">取 消</el-button>
            </div>
          </div>

        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial} from "@/api/wms/material";
import {randomId} from '@/utils/RandomUtils'
import {listUser} from "../../../api/system/user";
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/wms/item";

export default {
  name: "Material",
  data() {
    return {
      certKey: "",
      radio: "",
      leaderOpen: false,
      userList: [],
      templateSelection2: [],
      checkList: [],
      roleRelateObj: {},
      addType: "",
      total2: 0,
      modalObj: {
        show: false,
        title: '',
        width: '50%',
        component: null,
        model: {},
        ok: () => {
        },
        cancel: () => {
        }
      },
      hasSupplier: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料申请单表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNo: undefined,
        materialName: undefined,
        amount: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "id不能为空", trigger: "blur"}
        ],
        materialNo: [
          {required: true, message: "编号不能为空", trigger: "blur"}
        ],
        materialName: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "总数量不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "1采购中2已完成采购不能为空", trigger: "change"}
        ],
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getListItem();
  },
  methods: {
    //数量改变调用方法
    recalculateTotal() {
      this.certKey=!this.certKey;
      console.log(this.certKey)
    },
    //
    handleLeaderAdd(type) {
      this.addType = type;
      this.leaderOpen = true;
      this.getListItem();
    },
    submitLeaderForm() {
      console.log(this.templateSelection2)
      this.form.details=this.templateSelection2;
      this.leaderOpen = false;
    },
    getCurrentRow(row) {
      this.templateSelection = row;
      console.log(row);
      console.log(this.checkList);
      // 获取选中数据 row表示选中这一行的数据,可以从里面提取所需要的值
    },
    handleSelectionChange2(selection) {
      this.templateSelection2 = selection;
    },
    getListItem() {
      listItem().then((response) => {
        this.userList = response.rows;
        this.total2 = response.total;

        console.log(
          this.userList,
        );
      });
    },
    /** 重置按钮操作 */
    resetQuery2() {
      this.resetForm("roleRelateObj");
      this.handleQuery2();
    },
    /** 关联角色弹框重置按钮 */
    handleQuery2() {
      this.getListItem();
    },
    /** 查询物料申请单列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialNo: 'SQ-' + randomId(),
        id: undefined,
        materialName: undefined,
        amount: undefined,
        status: undefined,
        delFlag: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料申请单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getMaterial(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改物料申请单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料申请单编号为"' + ids + '"的数据项?').then(() => {
        this.loading = true;
        return delMaterial(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/material/export', {
        ...this.queryParams
      }, `material_${new Date().getTime()}.xlsx`)
    },


    confirmSelectItem() {
      const value = this.$refs['item-select'].getRightList()
      this.form.details = value.map(it => {
        return {
          id: it.id,
          prod: it,
          planQuantity: null,
          realQuantity: null,
          place: [],
          receiptOrderStatus: 0,
          delFlag: 0
        }
      })
      this.closeModal()
    },
    closeModal() {
      this.modalObj.show = false
    },
    showAddItem() {
      try {
        this.$refs['item-select'].initDetailsList(this.form.details)
      } catch (err) {

      }
      const ok = () => this.confirmSelectItem()
      const cancel = () => this.closeModal()
      this.modalObj = {
        show: true,
        title: '添加物料',
        width: '50%',
        component: 'add-item',
        model: {},
        ok,
        cancel
      }
    },

  }
};
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1395049.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

20230118-【UNITY 学习】增加攀登系统

替换脚本PlayerMovement_04.cs using System.Collections; using System.Collections.Generic; using UnityEngine;public class PlayerMovement_05 : MonoBehaviour {private float moveSpeed; // 玩家移动速度public float walkSpeed 7; // 行走速度public float sprintSpee…

基于麻雀优化算法SSA的CEEMDAN-BiLSTM-Attention的预测模型

往期精彩内容&#xff1a; 时序预测&#xff1a;LSTM、ARIMA、Holt-Winters、SARIMA模型的分析与比较-CSDN博客 风速预测&#xff08;一&#xff09;数据集介绍和预处理-CSDN博客 风速预测&#xff08;二&#xff09;基于Pytorch的EMD-LSTM模型-CSDN博客 风速预测&#xff…

网络安全 | 苹果承认 GPU 安全漏洞存在,iPhone 12、M2 MacBook Air 等受影响

1 月 17 日消息&#xff0c;苹果公司确认了近期出现的有关 Apple GPU 存在安全漏洞的报告&#xff0c;并承认 iPhone 12 和 M2 MacBook Air 受影响。 该漏洞可能使攻击者窃取由芯片处理的数据&#xff0c;包括与 ChatGPT 的对话内容等隐私信息。 安全研究人员发现&#xff0c;…

论文阅读:Vary论文阅读笔记

目录 引言整体结构图数据集构造Vary-tiny部分Document Data数据构造Chart Data构造Negative natural image选取 Vary-base部分 引言 论文&#xff1a;Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models Paper | Github | Demo 许久不精读论文了&#x…

基于Harris角点的多视角图像全景拼接算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 4.1 Harris角点检测 4.2 图像配准 4.3 图像变换和拼接 4.4 全景图像优化 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 function [ImageB…

鸿蒙开发-ArkUI框架实战【日历应用 】

对于刚刚接触OpenHarmony应用开发的开发者&#xff0c;最快的入门方式就是开发一个简单的应用&#xff0c;下面记录了一个日历应用的开发过程&#xff0c;通过日历应用的开发&#xff0c;来熟悉基本图形的绘制&#xff0c;ArkUI的组件的使用&#xff0c;UI组件生命周期&#xf…

新版AndroidStudio dependencyResolutionManagement出错

在新版AndroidStudio中想像使用4.2版本或者4.3版本的AndroidStudio来构造项目&#xff1f;那下面这些坑我们就需要来避免了&#xff0c;否则会出各种各样的问题。 一.我们先来看看新旧两个版本的不同。 1.jdk版本的不同 新版默认是jdk17 旧版默认是jdk8 所以在新版AndroidSt…

什么是SFP光学模块?

SFP光模块是一个十亿位电信号到光信号接口设备&#xff0c;是行业标准的小型可插拔千兆光收发器模块&#xff0c;集成可插拔交换机&#xff0c;路由器和其他网络设备&#xff0c;媒体转换器SFP端口&#xff0c;用于连接到光或铜线数据传输网络&#xff0c;我们通常可以在以太网…

Numpy的学习 第一课 了解以及使用

1.输入模式 1.编辑模式 绿色2.命令模式 蓝色 2.运行 直接输入jupyter notebook 3.文档注释 查看函数帮助文档命令 help(函数) 单问号与多问号 单问号显示文档 多问号显示文档代码 3.shifttab 显示参数 4.运行外部文件 %run 路径,可绝对可相对 这里运行了就相当于方法了,或者…

Python爬虫 - 网易云音乐下载

爬取网易云音乐实战&#xff0c;仅供学习&#xff0c;不可商用&#xff0c;出现问题&#xff0c;概不负责&#xff01; 分为爬取网易云歌单和排行榜单两部分。 因为网页中&#xff0c;只能显示出歌单的前20首歌曲&#xff0c;所以仅支持下载前20首歌曲&#xff08;非VIP音乐&…

Hamcrest断言:自动化测试的利器

Hamcrest断言&#xff1a;自动化测试中的可读性与表达力利器 背景&#xff1a;在软件开发中&#xff0c;自动化测试是确保软件质量和稳定性的重要环节。为了编写可靠且易于维护的自动化测试脚本&#xff0c;我们需要使用可读性强且表达力强的断言工具。Hamcrest是一个优秀的断言…

渗透测试之Hydra如何B破远程主机RDP登录M码

环境: Hydra9.3 KALI2022 问题描述: 渗透测试之hydra如何B破远程主机RDP登录M码 解决方案: Hydra是一款非常强大的网络登录P解工具。它专门用于测试和评估网络安全,通过暴力P解方式尝试多种用户名和密码组合,以获得对受测试系统的非法访问。Hydra支持各种协议的登录破…

21. 合并两个有序链表(Java)

题目描述&#xff1a; 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 输入&#xff1a; l1 [1,2,4], l2 [1,3,4] 输出&#xff1a; [1,1,2,3,4,4] 代码实现&#xff1a; 结点类&#xff1a; public class ListNode {in…

「优选算法刷题」:盛最多水的容器

一、题目 给定一个长度为 n 的整数数组 height 。有 n 条垂线&#xff0c;第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。 找出其中的两条线&#xff0c;使得它们与 x 轴共同构成的容器可以容纳最多的水。 返回容器可以储存的最大水量。 说明&#xff1a;你不能倾斜容器…

基于JavaSocket重写Dubbo网络传输层

前言 我们知道&#xff0c;位于 Serialize 层上面的是负责网络传输的 Transport 层&#xff0c;它负责调用编解码器 Codec2 把要传输的对象编码后传输、再对接收到的字节序列解码。 站在客户端的角度&#xff0c;一次 RPC 调用的流程大概是这样的&#xff1a; Invoker 发起 …

CSS实现的 Loading 效果

方式一、纯CSS实现 代码&#xff1a;根据需要复制 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>CSS Animation Library for Developers and Ninjas</title><style>/* ---------------…

操作系统课程设计-Windows 线程的互斥和同步

目录 前言 1 实验题目 2 实验目的 3 实验内容 3.1 步骤 3.2 关键代码 3.2.1 创建生产者和消费者进程 3.2.2 生产者和消费者进程 4 实验结果与分析 5 代码 前言 本实验为课设内容&#xff0c;博客内容为部分报告内容&#xff0c;仅为大家提供参考&#xff0c;请勿直接抄…

SqlAlchemy使用教程(五) ORM API 编程入门

SqlAlchemy使用教程(一) 原理与环境搭建SqlAlchemy使用教程(二) 入门示例及编程步骤SqlAlchemy使用教程(三) CoreAPI访问与操作数据库详解SqlAlchemy使用教程(四) MetaData 与 SQL Express Language 的使用SqlAlchemy使用教程(五) ORM API 编程入门 前一章用SQL表达式(SQL Expr…

机器学习之卷积神经网络

卷积神经网络是一类包含卷积计算且具有深度结构的前馈神经网络,是深度学习的代表算法之一。卷积神经网络具有表征学习能力,能够按其阶层结构对输入信息进行平移不变分类,因此又称为SIANN。卷积神经网络仿照生物的视知觉机制构建,可以进行监督学习和非监督学习,其隐含层内的…

【Internet Protocol】ip介绍,如何组局域网实现远程桌面和文件共享

文章目录 1.何为“上网”1.1 定义1.2 为什么连了WiFi就能上网了&#xff1f; 2.ip2.1 什么是ip2.2 为什么区分广域网和局域网&#xff0c;ip的唯一性2.3 如何查看设备的ip2.4 什么叫"ping"2.5 区分是否两个ip是否在同一局域网2.5.1 最稳妥的方式&#xff1a;ip&m…