el-table制作的表格,有时候需要增加和删除某一行,以下为删除某一行的代码。
template
<el-table-column label="操作">
<template slot-scope="scope" >
<el-button type="text" @click="modify(scope.row)"> 修改</el-button>
<el-button type="text" @click="delResource(scope.$index)"> 删除</el-button>
</template>
</el-table-column>
methods
addResource(){
let newObj= {
name:"new resource",
};
this.resource.push(newObj);
},
delResource(x){
console.log(x)
this.resource.splice(x, 1)
},
this.resource.splice(x, 1) 这一句是核心代码。
语法:
array.splice(index, howmany, item1, …, itemX)
index 必需。整数,指定在什么位置添加/删除项目,使用负值指定从数组末尾开始的位置。
howmany 可选。要删除的项目数。如果设置为 0,则不会删除任何项目。
item1, …, itemX 可选。要添加到数组中的新项目。