element table 表格添加行,刪除行記錄

業務開發需求.表格動態添加,也可刪除,實現如下圖:

  <el-table
                :data="tableData"
                style="width: 100%"
                border>
            <el-table-column
                    prop="bookname"
                    :label="recoveryOne"
                    width="140px">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.bookname" type="number"></el-input>
                </template>
            </el-table-column>

            <el-table-column
                    prop="bookvolume"
                    :label="recoveryTwo"
                    width="140px">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.bookvolume" type="number"></el-input>
                </template>
            </el-table-column>

            <el-table-column
                    prop="bookborrower"
                    :label="recoveryThree"
                    width="150px">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.bookborrower" type="number"></el-input>
                </template>
            </el-table-column>

            <el-table-column>
                <template slot-scope="scope">
                    <button @click="addLine"
                            class="addBtn"
                            v-if="scope.$index == tableData.length - 1">
                        <i class="el-icon-plus"></i>
                    </button>

                    <button v-if="tableData.length > 1"
                            @click="handleDelete(scope.$index, scope.row)"
                            class="del-btn">
                        <i class="el-icon-minus"></i>
                    </button>
                </template>
            </el-table-column>
        </el-table>

export default {
    data() {
        return {
            tableData:[{
                bookname: '',
                bookborrower: '',
                bookvolume:''
            }]
        }
    },
    methods:{
        addLine(){ //添加行數
            var newValue = {
                  bookname: '',
                  bookborrower: '',
                  bookvolume:''
              };
            //添加新的行數
            this.tableData.push(newValue);
        },
        handleDelete(index){ //刪除行數
            this.tableData.splice(index, 1)
        },
        save(){
          //這部分應該是保存提交你添加的內容
          console.log(JSON.stringify(this.tableData))
        }
    }
 
}

最后還需要提醒大家一點,本篇文章中的例子僅供參考學習,切誤將本篇文章中的代碼直接使用在正式項目當中。希望以上代碼對大家有所幫助。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容