首先一个小demo,请大佬教的
<template>
<view>
<el-button type="primary" @click='tableData.push(obj)'>新增行</el-button>
<el-button type="primary" @click='gettableData'>获取完整信息</el-button>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180">
<template slot-scope="scope">
<el-input v-model="scope.row.date" @change='getChange(scope.row)' placeholder="请输入内容"></el-input>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180">
<template slot-scope="scope">
<el-input v-model="scope.row.name" placeholder="请输入内容"></el-input>
</template>
</el-table-column>
<el-table-column prop="address" label="地址">
<template slot-scope="scope">
<el-input v-model="scope.row.address" placeholder="请输入内容"></el-input>
</template>
</el-table-column>
<el-table-column :prop="item.name" :label="item.title" v-for="(item,idx) in titleList" :key="idx">
<template slot-scope="scope">
<el-input v-model="scope.row[item.name]" placeholder="请输入内容"></el-input>
</template>
</el-table-column>
</el-table>
</view>
</template>
<script>
export default {
data() {
return {
obj: {
date: '',
name: '',
address: ''
},
tableData: [],
titleList: []
}
},
onLoad() {
this.getList()
},
methods: {
gettableData() {
console.log('全部数据', this.tableData)
},
getChange(val) {
console.log('当前行数据', val)
},
getList() {
//假设这里获取前段行数据
this.tableData = [{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄'
}, {
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄'
}, {
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}, {
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄'
}]
//假设行数据获取完成后获取表头
this.titleList = [{
title: '动态表头1',
name: 'ziduan1'
}, {
title: '动态表头2',
name: 'ziduan2'
}, {
title: '动态表头3',
name: 'ziduan3'
}]
//表头和行数据都获取完成之后进行预处理
this.tableData.forEach(data=>{
this.titleList.forEach(item=>{
data[item.name]=''
})
})
//拼接好的数据
console.log(this.tableData)
},
}
}
</script>
<style scoped lang="less">
</style>
接下来是根据需求写的表单+表格,由于后端给的格式不适应表格数据格式,做了很多转换,最后提交的数据也做了格式转换。代码较糙,留着看看长个记性,毕竟折磨了好多天
页面效果