freemarker List 遍历换行

本文介绍了一种处理列表数据的方法,使其能够以两列的形式展示,并在列表项数量为奇数时添加空行以保持布局整齐。文章通过具体的代码示例展示了如何使用Freemarker模板语言实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:将list中的值遍历为两列,且当list个数为奇数时添加一个空行列    

<#list (orders.attributeItems["xx"])?split(",") as item>
                                    
                                    <#if (item_index+1) % 2 == 0>
                                            <td><button>${(item_index+1)}档:${(item)!''}元
                                            ${((orders.attributeItems["xx"])?split(","))?size}</button></td>
                                           </tr>
                                       <#elseif (((orders.attributeItems["xx"])?split(","))?size  % 2 == 1 )  
                                       &&  ((item_index+1) % ((orders.attributeItems["xx"])?split(","))?size ==0)>
                                           <tr>
                                            <td><button>${(item_index+1)}档:${(item)!''}元</button></td>
                                           <td> </td>
                                           </tr>
                                       <#else>
                                           <tr>
                                            <td><button>${(item_index+1)}档:${(item)!''}元</button></td>
                                       </#if>
 </#list>

Ps:

(orders.attributeItems["xx"])?split(","):将字符串划分为list集合。

((orders.attributeItems["xx"])?split(","))?size  % 2 == 1 :通过集合长度判断是否为奇数列

item_index+1:集合下标(从1开始)

转载于:https://my.oschina.net/u/3530220/blog/1559689

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值