关于el-form+el-table+表头部分内容和表单都是动态生成(动态绑定v-model)

首先一个小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>

接下来是根据需求写的表单+表格,由于后端给的格式不适应表格数据格式,做了很多转换,最后提交的数据也做了格式转换。代码较糙,留着看看长个记性,毕竟折磨了好多天

页面效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值