原理:(1)通过针对点击的行添加一个css标记,控制该单元格的编辑器进行显示和隐藏,可通过highlight-current-row标记控制
(2)通过编辑获取点击修改的index控制该行单元格的编辑器进行显示和隐藏
代码差不多,下面的是第二种方法如下:
css部分:
/* 可编辑表格 */
.el-tb-edit .el-input,
.el-tb-edit .el-input-number,
.el-tb-edit .el-select,
.el-tb-edit .el-input__inner{ display: none !important;width: 100%;}
.el-tb-edit .current-row .el-input,
.el-tb-edit .current-row .el-input-number,
.el-tb-edit .current-row .el-select,
.el-tb-edit .current-row .el-input__inner{display: inline-flex !important;}
.el-tb-edit .current-row .el-input+span,
.el-tb-edit .current-row .el-input-number+span,
.el-tb-edit .current-row .el-select+span,
.el-tb-edit .current-row .el-input__inner+span{display: none !important;}
html部分:
<el-table class="comtb"
v-loading="loading2" element-loading-text="Loading......"
element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 1)"
ref="singleTable" :data="list" tooltip-effect="dark"
:row-class-name="tableRowClassName" empty-text="暂无数据"
@row-click="clickrow"
style="width: 100%">
<el-table-column prop="UnitName" label="名词"></el-table-column>
<el-table-column prop="UnitCode" label="代码"></el-table-column>
<el-table-column prop="UserName" label="操作用户"></el-table-column>
<el-table-column prop="OperateTime" label="操作时间"></el-table-column>
<el-table-column width="150px" label="参数值">
<template scope="scope">
<el-input v-if="scope.$index ===clickindex" v-model="scope.row.HourCapacity"
@blur="BlurText($event,scope.row.HourCapacity)">
</el-input>
<span v-if="scope.$index !=clickindex">{{scope.row.HourCapacity}}</span>
</template>
</el-table-column>
<el-table-column label="备注">
<template scope="scope">
<el-input v-if="scope.$index ===clickindex" v-model="scope.row.Remark"></el-input>
<span v-if="scope.$index !=clickindex">{{scope.row.Remark}}</span>
</template>
</el-table-column>
<el-table-column width="100px" label="操作">
<template slot-scope="scope">
<el-button v-if="scope.$index !=clickindex" size="mini" type="primary" icon="el-icon-edit"
v-on:click="setCurrent(scope.$index)">编辑</el-button>
<el-button v-if="scope.$index ===clickindex" size="mini" type="danger" icon="el-icon-success"
v-on:click="saveData(scope.row)">保存</el-button>
</template>
</el-table-column>
</el-table>
JS部分:
var mainvue = new Vue({
data: function () {
return {
loading2:false, clickindex:-1,//选中的行
}
},
created:function () {},
methods: {
/*验证输入为正整数*/
BlurText(e,oldval){
var isok = new RegExp("^[1-9][0-9]*$").test(e.target.value);
if(!isok){this.$message.warning("请输入正整数"); e.target.value=oldval;}
},
tableRowClassName ({row, rowIndex}){row.index = rowIndex;},
setCurrent(index) {this.clickindex = index;},
clickrow(row, event, column){if(row.index!=this.clickindex && this.clickindex>=0)this.clickindex=-1;},
},
filters:{ }
}).$mount('#app');