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]
           		}
     	  	}))
      	})       

	}

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

### 回答1: Ant Design Vue 表格Table)组件支持通过 `row-class-name` 属性来设置行的样式类名。你可以使用该属性根据行数据的属性值来动态设置样式。 例如,假设你有一个表格,其中有一个 `status` 字段,它的值为 1 表示正常,值为 0 表示异常。你可以在 `row-class-name` 中设置一个函数,根据 `status` 的值来返回不同的样式类名,如下所示: ```html <template> <a-table :row-class-name="getRowClassName" :columns="columns" :data-source="dataSource"></a-table> </template> <script> export default { data() { return { columns: [ // 列定义 ], dataSource: [ // 数据源 ] }; }, methods: { getRowClassName(record) { return record.status === 0 ? 'error-row' : ''; }, }, }; </script> <style> .error-row { background-color: #ffcccc; } </style> ``` 在上面的代码中,`getRowClassName` 方法接收一个参数 `record`,它是当前行的数据对象。该方法根据 `record.status` 的值来判断是否应该添加 `error-row` 样式类名。当 `status` 的值为 0 时,该行将会添加 `error-row` 类名,从而显示为红色背景。 ### 回答2: 在Ant Design Vue中,我们可以使用Table组件来展示数据,并根据需求定制表格行的样式。具体的步骤如下: 1. 首先,在使用Table组件时,我们需要通过定义columns数组来描述表格的列信息,包括列的标题、字段名等。示例如下: ``` const columns = [ { title: '姓名', dataIndex: 'name', }, { title: '年龄', dataIndex: 'age', }, // 其他列信息 ]; ``` 2. 如果想要为特定行设置样式,可以通过定义rowClassName属性来实现。我们可以给rowClassName传入一个函数,该函数会接收当前行数据作为参数,并返回一个字符串作为该行的类名。示例如下: ``` const rowClassName = (record, index) => { if (record.age > 30) { return 'highlight-row'; } return ''; }; ``` 3. 在CSS里定义highlight-row类,来设置需要突出显示的行的样式。示例如下: ```css .highlight-row { background-color: yellow; font-weight: bold; } ``` 4. 将定义好的columns和rowClassName应用到Table组件中。示例如下: ``` <template> <a-table :columns="columns" :data-source="data" :row-class-name="rowClassName"></a-table> </template> ``` 这样,当data中的某一行的age大于30时,该行的背景颜色会变成黄色,并加粗显示。 总结:通过定义columns和rowClassName属性,我们可以在Ant Design VueTable组件中设置表格行的样式,以满足个性化的需求。 ### 回答3: ant design vue表格行样式可以通过自定义的scoped样式和slot-scope来实现。 首先,在样式中添加特定的class来定义表格行的样式,例如定义一个名为"custom-row"的class: ```css .custom-row { background-color: #f5f5f5; } ``` 接下来,在表格组件中使用slot-scope来获取每一行的数据,并通过判断条件来添加自定义的class: ```html <template> <a-table :data-source="dataSource"> <a-table-column title="姓名"> <template slot-scope="text, record"> <a :class="{ 'custom-row': record.age > 30 }"> {{ record.name }} </a> </template> </a-table-column> <a-table-column title="年龄" dataIndex="age"></a-table-column> </a-table> </template> ``` 在上述代码中,通过slot-scope获取到每一行的数据,然后在a标签上使用:class来绑定自定义的class,使用条件判断来确定是否添加该class。 通过以上的代码,当表格中的某一行的年龄大于30时,该行的背景颜色会变为灰色。 备注:以上是使用scoped样式和slot-scope实现表格行样式的一种方法,当然也可以根据具体需求使用其他方法来实现。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值