首页 > 其他分享 >VUE Element Plus-table动态添加删除行

VUE Element Plus-table动态添加删除行

时间:2024-04-26 10:23:51浏览次数:17  
标签:VUE undefined form tableData Plus id Element response row

 

<template>
  <div class="app-container">
    <el-row style="margin-top: 20px">
      <el-col :span="24" style="border-left: 5px solid #1d6ced;margin-bottom: 10px">
        <label style="margin-left: 10px">追加审批</label>
      </el-col>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="bizSceneId" label="场景">
          <template slot-scope="scope">
            <el-select v-model="scope.row.bizSceneId" placeholder="请选择">
              <el-option
                v-for="dict in approvalScenes"
                :key="dict.dictValue"
                :value="dict.dictValue"
                :label="dict.dictLabel"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column prop="price" label="超越价格的审批" align="center">
          <template slot-scope="scope" v-if="scope.row.bizSceneId == '2'">
            <el-input v-model="scope.row.price" style="width:100px"></el-input>元
          </template>
        </el-table-column>

        <el-table-column prop="empId" label="追加审批人">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.empId"
              placeholder="请选择负责人"
              filterable
              style="width: 100%"
            >
              <el-option
                v-for="item in leaderOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.certNo }}</span>
              </el-option>
            </el-select>
          </template>
        </el-table-column>

        <el-table-column prop="addtoLevel" label="追加审批级别">
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.addtoLevel" align="left">
              <el-radio label="1">原审批流程第一级之前</el-radio>
              <el-radio label="2">原审批流程最后一级之前</el-radio>
              <el-radio label="3">原审批流程最后一级之后</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>

        <el-table-column prop="applyScope" label="适用范围">
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.applyScope" align="left">
              <el-radio
                label="1"
                @click.native="clickRadio($event, 1,scope.row.depts,scope.row)"
              >全公司</el-radio>
              <el-radio
                label="2"
                @click.native="clickRadio($event, 2,scope.row.depts,scope.row)"
              >指定分公司及所属部门</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click="addLine"
              type="primary"
              v-if="scope.$index == tableData.length - 1"
            >添加</el-button>

            <el-button
              type="danger"
              v-if="tableData.length > 1"
              @click="handleDelete(scope.$index, scope.row)"
              class="del-btn"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-col :span="6" :offset="12">
        <el-button type="primary" @click="saveData">保 存</el-button>
      </el-col>
    </el-row>
    <!-- 添加或修改员工基本信息对话框 -->
    <el-dialog title="指定分公司及所属部门" :visible.sync="deptOpen" width="500px">
      <div>
        <el-row>
          <el-col :span="24">
            <treeselect
              v-model="rowSelect"
              :options="empDeptOptions"
              :normalizer="normalizer"
              placeholder="选择部门"
              :multiple="true"
            />
          </el-col>
        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveTree">确 定</el-button>
        <el-button @click="deptOpen=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style lang="scss" scoped="scoped">
.treeSelect {
  /deep/ .el-form-item__content {
    margin-top: 1px;
    height: 32px;
    line-height: 30px;
  }
}
</style>
<script>
import {
  listApproveRuleAddto,
  addApproveRuleAddto
} from "@/api/crm/approveRuleAddto";
import { listEmp } from "@/api/crm/dept";
import {
  listEmployee,
  getEmployee,
  delEmployee,
  addEmployee,
  updateEmployee,
  exportEmployee,
  listDept,
  editStatus,
  editUseStatus,
  delCert,
  getStation
} from "@/api/crm/employee";
import { getLevelJiangLi, getLevelEmp } from "@/api/crm/emplevel";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "ApproveRule",
  components: { Treeselect },
  data() {
    return {
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 产品设置-审批规则表格数据
      approveRuleList: [],
      // 产品设置-审批规则表格数据
      approveEnableds: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      deptOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        comId: undefined,
        bizSceneId: undefined,
        bizSceneId: undefined,
        isForce: undefined
      },
      // 表单参数
      form: {
        listData: []
      },
      // 表单参数
      formAdd: {},
      // 表单校验
      rules: {},
      tableData: [
        {
          bizSceneId: "",
          price: "",
          empId: "",
          addtoLevel: "",
          applyScope: ""
        }
      ],
      approvalScenes: [],
      leaderOptions: [],
      //部门
      empDeptOptions: [],
      rowSelect: [],
      rowData: {}
    };
  },
  created() {
    this.getList();
    //获取审批开关字典
    this.getDicts("approve_flow_enabled").then(response => {
      this.approveEnableds = response.data;
    });
    // 获取审批场景字典;
    this.getDicts("approval_scene").then(response => {
      this.approvalScenes = response.data;
    });
    //获取员工列表
    this.getEmployeeSelect();
    listDept().then(response => {
      this.empDeptOptions = this.handleTree(response.data, "deptId");
    });
  },
  methods: {
    /*查询员工列表*/
    getEmployeeSelect() {
      listEmp(1).then(response => {
        this.leaderOptions = response.data;
      });
    },
    //审批开关码表转化
    setApprovalSceneFormatter(row, column) {
      return this.selectDictLabel(this.approvalScenes, row.type);
    },
    /** 查询产品设置-审批规则列表 */
    getList() {
      this.loading = true;
      listApproveRuleAddto(this.queryParams).then(response => {
        debugger;
        var a = this.leaderOptions;
        this.tableData = response.rows;
        for (let i = 0; i < this.tableData.length; i++) {
          this.tableData[i].addtoLevel = this.tableData[
            i
          ].addtoLevel.toString();
          this.tableData[i].applyScope = this.tableData[
            i
          ].applyScope.toString();
          this.tableData[i].empId = Number(this.tableData[i].empId);
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        comId: undefined,
        bizSceneId: undefined,
        bizSceneId: undefined,
        isForce: undefined,
        crmApproveRules: [],
        crmApproveRuleAlert: [],
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined
      };
      this.approveRuleAddtoList = this.approveRuleAddtoList;
      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.reset();
      const id = row.id || this.ids;
      getApproveRule(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品设置-审批规则";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log(this.form);
          this.form.crmApproveRules = this.crmApproveRules;
          this.form.crmApproveRuleAlert = this.crmApproveRuleAlert;
          if (this.form.id != undefined) {
            addApproveRuleInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addApproveRuleInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormAdd: function() {
      this.$refs["approveRuleAddtoList"].validate(valid => {
        if (valid) {
          if (this.approveRuleAddtoList.id != undefined) {
            addApproveRuleInfo(this.approveRuleAddtoList).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                // this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addApproveRuleInfo(this.approveRuleAddtoList).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                // this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除产品设置-审批规则编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delApproveRule(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有产品设置-审批规则数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportApproveRule(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    },
    /** 追加一行 */
    addJurisdiction(row, index) {
      this.approveRuleAddtoList.push(this.formAdd);
    },
    /** 删除一行 */
    delJurisdiction(row, index) {
      this.approveRuleAddtoList.splice(index, 1);
    },
    addLine() {
      //添加行数
      var newValue = {
        bizSceneId: "1",
        price: "",
        empId: "",
        addtoLevel: "1",
        applyScope: "1"
      };
      //添加新的行数
      this.tableData.push(newValue);
    },
    handleDelete(index) {
      //删除行数
      this.tableData.splice(index, 1);
    },
    // 保存数据
    saveData() {
      this.form.listData = this.tableData;
      addApproveRuleAddto(this.form).then(response => {
        if (response.code === 200) {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        } else {
          this.msgError(response.msg);
        }
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 打开弹出框
    clickRadio(e, index, val, row) {
      debugger;
      this.rowSelect = [];
      this.rowData = row;
      if (index == "2") {
        if (undefined != val && "" != val) {
          var arr = val.split(",");
          for (let i = 0; i < arr.length; i++) {
            this.rowSelect.push(Number(arr[i]));
          }
        }
        this.deptOpen = true;
      }
    },
    // saveTree
    saveTree() {
      debugger;
      var _this = this;
      var rowData = _this.rowData;
      var treeData = _this.rowSelect;
      for (let i = 0; i < _this.tableData.length; i++) {
        if (_this.tableData[i].id == rowData.id) {
          let str = "";
          for (let k = 0; k < treeData.length; k++) {
            str += treeData[k] + ",";
          }
          // _this.tableData[i].depts = treeData;
          // str.substring(0,str.length-1);
          str = str.substring(0, str.length - 1);
          _this.tableData[i].depts = str;
        }
      }
      this.deptOpen = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 15px;
  .requiredIcon {
    color: red;
  }
}
</style>

 

标签:VUE,undefined,form,tableData,Plus,id,Element,response,row
From: https://www.cnblogs.com/kongxiaoshuang/p/18159398

相关文章

  • vue3+vite+js 引用public文件夹中js文件
    vue的public的资源在打包时不会被编译,只会copy所以在在src路径下引入public文件夹下的图片、视频、音频,编译不会改变其路径,但是在src下引入public文件夹下的js、json,在打包时都会被编译,所以直接引入会丢失路径(因为打包时,当前页面引入的路径被hash打包,而public文件夹下只是被cop......
  • 2024年vue 开发环境 Node.js于win10环境下的安装
    2024年vue开发环境Node.js于win10环境下的安装导航2024年vue开发环境Node.js于win10环境下的安装导航一、下载node.js二、安装node.js三、测试(一)四、环境配置五、测试(二)六、安装淘宝镜像七、安装vue脚手架一、下载node.jsNode.js官方网站下载:https://nodejs.org/en......
  • 【vue3入门】-【13】class绑定
    class绑定数据绑定的一个常见需求场景是操纵元素的CSSclass列表,因为class是Attribute,我们可以和其他Attribute一样使用v-bind将它们动态的字符串绑定。但是,在处理比较复杂的绑定时,通过拼接生成字符串是麻烦且易出错的。因此,vue专门为class和v-bind用法提供了特殊的功能增强。除......
  • 【vue3入门】-【15】侦听器
    侦听器我们可以使用watch选项在每次响应式属性发生变化时触发一个函数<template><h3>侦听器</h3><!--不可以被监听,是固定的数据--><p>{{message}}</p><!--可以被监听,只能监听响应式数据(变化的数据)--><button@click="updateHandle">修改数据</button>&l......
  • 【vue3入门】-【14】style绑定
    style绑定数据绑定的一个常见需求场景式操作元素的cssstyle列表,因为style是一个Attribute,我们可以和其他Attribute一样使用v-bind,将它们和动态的字符串绑定。但是,在处理比较复杂的绑定时,通过拼接生成字符串是麻烦且容易出错的。因此,vue专门为style的v-bind用法提供了特殊的功能......
  • 【vue3入门】-【16】表单输入绑定
    表单输入绑定在前端处理表单时,我们常常需要将表单输入框的内容同步给JavaScript中相应的变量。手动连接值绑定和更改事件监听器可能会比较麻烦,v-model指令帮我们简化了这一步骤。<template><h3>表单输入绑定</h3><form><!--v-model:在页面中输入信息的同时,下......
  • 【vue3入门】-【18】组件组成
    组件组成组件最大的优势就是可复用性当使用构建步骤是,我们一般将vue组件定义在一个单独的.vue文件中,这杯叫做单文件组件(简称SFC)组件组成结构<!--承载所有的html标签,组件中必须要有的部分--><template> <div>承载标签</div></template><!--承载所有的业务逻辑,组件中可选......
  • 【vue3入门】-【17】模版引用
    模版引用虽然Vue的声明性渲染模型抽象了大部分的DOM的直接操作,但在某些情况下,我们仍然需要直接访问底层DOM元素。要实现这一点,我们可以使用特殊的refattribute挂载结束后引用都会被暴露在this.$refs只上,从this.$refs中按照js原生方法获取元素属性或变更元素属性<template>......
  • 【vue3入门】-【19】组件嵌套关系
    组件嵌套关系组件允许我们将UI划分为独立的,可重用的部分,并且可以对每个部分进行单独的思考。在实际应用中,组件常常被阻止成层层嵌套的树状结构这和我们嵌套HTML元素的方式类似,Vue实现了自己的组件模型,使我们可以在每个组件内封装自定义内容和逻辑APP.vue<template><!--主......
  • 【vue3入门】-【5】属性绑定
    属性绑定双大括号不能在htmlattributes中使用。想要响应的绑定一个attribute,应该使用v-bind指令<script>exportdefault{data(){return{msg:"active",myid:"test-id"}}}</script><template><divclass="{{msg}......