element-ui 表格可編輯添加刪除


 1 <template>
 2   <div id="Cold_all">
 3     <div class="Cold_Left">
 4       <el-row>
 5         <div class="font_style"> 1.輸入計算基本數據</div>
 6         <el-col :span="24">
 7          
 8           <el-table
 9             size="mini"
10             :data="master_user.data"
11             border
12             style="width: 100%;margin:auto"
13             highlight-current-row
14           >
15             <el-table-column type="index"></el-table-column>
16             <el-table-column
17               v-for="(item,index) in master_user.columns"
18               :label="item.label"
19               :prop="item.prop"
20               :width="item.width"
21               :key="index"
22             >
23               <template slot-scope="scope">
24                 <span v-if="scope.row.isSet">
25                   <el-input size="mini" placeholder="請輸入內容" v-model="master_user.sel[item.prop]"></el-input>
26                 </span>
27                 <span v-else>{{scope.row[item.prop]}}</span>
28               </template>
29             </el-table-column>
30             <el-table-column label="操作">
31               <template slot-scope="scope">
32                 <span
33                   class="Insert_Cold"
34                   style="cursor: pointer;"
35                   @click.stop="saveRow(scope.row,scope.$index)"
36                 >保存</span>
37                 <span
38                   class="Edit_Cold"
39                   style="cursor: pointer;"
40                   @click="editRow(scope.row,scope.$index)"
41                 >編輯</span>
42                 <span
43                   class="Delete_Cold"
44                   style="cursor: pointer;"
45                   @click="deleteRow(scope.$index,master_user.data)"
46                 >刪除</span>
47               </template>
48             </el-table-column>
49           </el-table>
50         </el-col>
51         <el-col :span="24">
52           <div class="el-table-add-row" style="width: 99.2%;" @click="add()">
53             <span>+ 添加</span>
54           </div>
55         </el-col>
56       </el-row>
57     </div>
58   </div>
59 </template>
<script>
export default {
  name: "",
  data() {
    return {
      master_user: {
        sel: null, //選中行
        columns: [
          
          {
            prop: "OutdoorTDB",
            label: "室外計算溫度(℃)",

          },
          {
            prop: "IndoorTDB",
            label: "室內計算溫度(℃)"
          },
          {
            prop: "TdbStartTime",
            label: "運行開始時間"
          },
          {
            prop: "TdbEndTime",
            label: "運行結束時間"
          }
        ],
        data: []
      }
}
<script>
methods: {
    //基本輸入列表
    add() {
      for (let i of this.master_user.data) {
        if (i.isSet) return this.$message.error("請先保存當前編輯項");
      }
      let j = {
        index: "",
        OutdoorTDB: "",
        IndoorTDB: "",
        TdbStartTime: "",
        TdbEndTime: "",
        isSet: true
      };
      this.master_user.data.push(j);
      this.master_user.sel = JSON.parse(JSON.stringify(j));
    },
    saveRow(row, index) {
      //保存
      let data = JSON.parse(JSON.stringify(this.master_user.sel));
      for (let k in data) {
        row[k] = data[k];
      }
      row.isSet = false;
    },
    editRow(row) {
      //編輯
      for (let i of this.master_user.data) {
        if (i.isSet) return this.$message.error("請先保存當前編輯項");
      }
      this.master_user.sel = row;
      row.isSet = true;
    },
    deleteRow(index, rows) {
      //刪除
      rows.splice(index, 1);
    },
components: {}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM