• Vue 表格动态添加行/删除行


    <template>
      <div class="elife-container">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAdd"
              >新增</el-button
            >
            <el-button
              :disabled="showAble"
              type="info"
              plain
              size="mini"
              @click="handleCancel"
              >取消</el-button
            >
          </el-col>
        </el-row>
    
        <!-- 表格信息 -->
        <div v-show="showTable">
          <el-table
            v-loading="loading"
            :data="dataList"
            :header-cell-style="{ 'text-align': 'center' }"
            :cell-style="{ 'text-align': 'center' }"
          >
            <el-table-column
              label="字段1"
              prop="value1"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.editable">
                  <el-input v-model="scope.row.value1"></el-input>
                </div>
                <div v-else>
                  {{ scope.row.value1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="字段2"
              prop="value2"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.editable">
                  <el-input v-model="scope.row.value2"></el-input>
                </div>
                <div v-else>
                  {{ scope.row.value2 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="字段3"
              prop="value3"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.editable">
                  <el-input v-model="scope.row.value3"></el-input>
                </div>
                <div v-else>
                  {{ scope.row.value3 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="字段4"
              prop="value4"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <div v-if="scope.row.editable">
                  <el-input v-model="scope.row.value4"></el-input>
                </div>
                <div v-else>
                  {{ scope.row.value4 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  v-show="scope.row.editable"
                  size="mini"
                  type="text"
                  @click="handleSave(scope.$index, scope.row)"
                  >保存</el-button
                >
                <el-button
                  v-show="scope.row.editable"
                  size="mini"
                  type="text"
                  @click="handleSave(scope.$index, scope.row)"
                  >取消</el-button
                >
                <el-button
                  v-show="!scope.row.editable"
                  size="mini"
                  type="text"
                  @click="handleUpdate(scope.$index, scope.row)"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  @click="handleDetele(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </template>
    
    <script>
    import * as Config from "./data.js";
    export default {
      name: "WarningManage", // 预警管理
      data() {
        return {
          showAble: true,
          // 遮罩层
          loading: false,
          // 数据集
          dataList: [],
          showTable: true,
        };
      },
      mounted() {
        this.getList();
      },
      methods: {
        // 获取数据
        getList() {
          this.loading = true;
          this.dataList = Config.warningData;
          this.dataList.map((item) => {
            item.editable = false;
          });
          this.total = this.dataList.length;
          this.loading = false;
        },
    
        handleAdd() {
          let obj = {};
          obj.editable = true;
          this.showAble = false;
          this.dataList.push(obj);
        },
    
        handleCancel() {
          this.dataList.splice(-1, 1);
          this.showAble = true;
        },
    
        handleSave(data1, data2) {
          this.showTable = false;
          this.dataList[data1].editable = false;
          this.showAble = true;
          this.showTable = true;
        },
    
        handleUpdate(data1, data2) {
          console.log("111", data1);
          this.showTable = false;
          this.dataList[data1].editable = true;
          this.showTable = true;
          // this.showAble = true;
        },
    
        handleDetele(index) {
          this.dataList.splice(index, 1);
        },
      },
    };
    </script>
    
    <style lang="scss" scoped>
    .cell-text {
      color: #1890ff;
      cursor: pointer;
    }
    </style>
    

    在这里插入图片描述

  • 相关阅读:
    检测摄像头的fps
    【Ubuntu18.04 重启后卡在[OK]界面的解决方案】
    阿里云 OSS
    自动驾驶激光雷达与域控制器
    基于函数计算自定义运行时快速部署一个 Springboot 项目
    LeetCode_贪心算法_中等_134. 加油站
    PAT 1023 Have Fun with Numbers(高精度乘法)
    spring boot正常启动之后访问controller下接口报404的解决方案
    【Rust日报】2022-09-14 使用 Rust 构建简单博客 && 华为实习生招募
    混合与剔除
  • 原文地址:https://blog.csdn.net/qq_43249953/article/details/139841546