项目场景:
表格实现编辑功能。在实际开发过程中,我们表格的表头往往很多,有的还会发生变化,所以本文参考一位博主的代码结合自己的项目进行了改编。
参考原文链接:vue element-ui实现table表格可编辑修改
代码展示
<template>
<div class="st-table">
<el-table :data="tableData" border style="width: 100%">
<el-table-column v-for="(item, index) in columns" :key="index" fixed :prop="item.prop" :label="item.label" width="180">
<template slot-scope="scope">
<input type="text" v-model="scope.row[item.prop]" v-show="scope.row.iseditor" />
<span v-show="!scope.row.iseditor">{{scope.row.date}}</span>
</template>
</el-table-column>
<el-table-column label="操作" width="180">
<template slot-scope="scope">
<el-button type="warning" @click="edit(scope.row, scope)">编辑</el-button>
<el-button type="danger" @click="save(scope.row)">保存</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
columns:[
{prop:'date',label:'时间',width:'150'},
{prop:'name',label:'姓名',width:'150'},
{prop:'address',label:'地址',width:'150'},
],
tableData: [
{
date: "2016-05-02",
name: "王小虎",
address: "上海市普陀区金沙江路 1518 弄",
iseditor: false
},
{
date: "2016-05-04",
name: "王小虎",
address: "上海市普陀区金沙江路 1517 弄",
iseditor: false
},
{
date: "2016-05-01",
name: "王小虎",
address: "上海市普陀区金沙江路 1519 弄",
iseditor: false
},
{
date: "2016-05-03",
name: "王小虎",
address: "上海市普陀区金沙江路 1516 弄",
iseditor: false
}
]
}
},
methods:{
edit(row, index) {
row.iseditor = true;
},
save(row, index) {
row.iseditor = false;
}
}
}
</script>
<style scoped>
.st-table {
padding: 10px;
}
</style>
结果图展示: