需求概述
表格的列来自于后台,要先把表格列循环出来,这里用本地data模拟一下;表格数据赋值的时候,要判断一下该属性是否是可编辑的,这里用到了插槽
完整代码
<template>
<div>
<el-row>
<el-col :span="8">
<el-table
:data="tableData"
style="width:100%"
align="center"
border>
<!-- 表格列来自于tableHead这个数组,要把它循环出来 -->
<template v-for="(item, index) in tableHead">
<el-table-column
:key="index"
:prop="item.prop"
:label="item.label"
:min-width="item.width"
show-overflow-tooltip
resizable>
<!-- 如果是sort或者 name列,就是可编辑的 -->
<template slot-scope="scope">
<el-input-number v-if="item.prop=='sort' ? true : false" size="small" :min="0" v-model="scope.row.sort"></el-input-number>
<template v-else-if="numberEdit(item.prop)">
<el-input v-model="scope.row.name"></el-input>
</template>
<!-- 剩下的列用v-html静态显示就好 -->
<template v-else>
<span v-html="scope.row[item.prop]"></span>
</template>
</template>
</el-table-column>
</template>
</el-table>
</el-col>
</el-row>
</div>
</template>
<script>
export default {
data() {
return {
tableHead:[], //存放表格列
tableData:[] //存放表格数据
};
},
created(){
this.init();
},
methods: {
//初始化,获取表格列和表格数据,这里本地模拟一下
init(){
this.tableHead = [
{ label:"姓名", prop:"name", align:"center", width:100 },
{ label:"学号", prop:"number", align:"center", width:100 },
{ label:"排序", prop:"sort", align:"center", width:100 }
];
this.tableData = [
{ name:'一', number:'1089', sort:'4' },
{ name:'二', number:'1080', sort:'2' },
{ name:'三', number:'1084', sort:'3' },
];
},
//判断是否是可编辑的属性
numberEdit(p){
if(p == 'name'){
return true;
}else{
return false;
}
}
}
};
</script>