ant desgin vue table动态循环生成表格

场景: 根据选择日期年月份进行动态往表格添加数项

在这里插入图片描述

话不多说,下面直接上代码

一、html代码
<!-- 表格 -->
<div class="admin_table_list">
    <a-spin tip="Loading..." :spinning="spinning">
        <a-table bordered size="small" 
                 :rowkey="id" 
                 :components="components" 
                 :columns="columns"
                 :data-source="orderList" 
                 :pagination="false" 
                 :scroll="{x:750 | true, y: 440 }">
        </a-table>
    </a-spin>
</div>
二、js代码
// 点击查询按钮
getQuery() {
	this.spinning = true
    var Columns = this.columns = [{
        title: '序号',
        dataIndex: 'xuhao',
        width: 50,
        fixed: 'left',
        align: 'center'
    },
    {
        title: '备货单位/客户类型',
        dataIndex: 'customer',
        width: 180,
        ellipsis: true,
        align: 'center',
    },
    {
        title: '存货编码',
        dataIndex: 'inventoryNumber',
        width: 180,
        ellipsis: true,
        align: 'center',
    },
    {
        title: '存货名称',
        dataIndex: 'inventoryName',
        width: 180,
        ellipsis: true,
        align: 'center',
     },
     {
         title: '规格',
         dataIndex: 'specifications',
         width: 180,
         ellipsis: true,
         align: 'center',
     },
     {
         title: '主计量',
         dataIndex: 'measurement',
         width: 180,
         ellipsis: true,
         align: 'center',
     }];
    let columns = [{
        title: '备货/订单数量',
        dataIndex: 'plan',
    },
    {
        title: '发货数量',
        dataIndex: 'ship',
    },
    {
        title: '出库数量',
        dataIndex: 'out',
    },
    {
        title: '退货数量',
        dataIndex: 'back',
    },
    {
        title: '退库数量',
        dataIndex: 'red',
    }]
    
    // 获取表单数据
    for (var i = Number(this.beginmonth); i<=Number(this.endmonth); i++) {
        let time = this.beginyears + (i < 10? "0"+ i : i)
        //动态循环添加表头
        Columns.push({
            title: this.beginyears + '年' + i + '月',
            align: "center",
            dataIndex: 'result',
            key: 'result',
            //动态匹配表头项对应值
            children: columns?.map(el => ({
            	...el,
            	key: el?.dataIndex,
            	width: 120,
            	align: "center",
            	customRender: (text, record) => {
                    return record.result[time][el?.dataIndex] ?? 0
                  }
        	}))
    	})
     }
     this.form.validateFields((err, values) => {
         this.page = 1;
         this.pageSize = 10;
         this.getcgList(...)}) //请求表格列表
         //添加合计
     	 Columns.push({
         	title: '合计',
         	align: 'center',
         	dataIndex: 'result',
         	key: 'result',
            //动态循环匹配合计数值
         	children: columns?.map(el => ({
         		...el,
         		key: el?.dataIndex,
         		width: 120,
         		align: "center",
         		customRender: (text, record) => {
             		return record.result['heji'][el?.dataIndex]
           		}
     	  	}))
      	})       

	}

有什么不懂,欢迎留言,一起相互学习探讨技术交流!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值