Huili_yi 2025-03-10 12:03 采纳率: 0%
浏览 8

freemarker填充表格时的<#list>并列循环问题

freemarker 生成xml文件的时候 他这里如何保持两个<#list>标签的下标保持一致

img

这里的一个是班级的数据,一个是年级的数据, 班级的数据中是一个列表中含了一个data数组 数组里是相关班级的Map

相关的json是这样的

{
    "code": 200,
    "msg": "操作成功",
    "data": {
        "totalData": {
            "comparison": "2023-2024第一学期",
            "curMissionName": "2023-2024第二学期",
            "schoolName": "实验小学"
        },
        "gradeDifferenceData": [
            {
                "preRedCount": "32",
                "preYellowCount": "28",
                "curGrade": "本期一年级",
                "curRedCount": "31",
                "preGreenCount": "0",
                "greenDifference": "2",
                "greenDifferenceType": "↑",
                "preGrade": "",
                "redDifference": "-1",
                "yellowDifference": "-1",
                "redDifferenceType": "↓",
                "curGreenCount": "2",
                "yellowDifferenceType": "↓",
                "curYellowCount": "27"
            },
            {
                "preRedCount": "38",
                "preYellowCount": "17",
                "curGrade": "本期二年级",
                "curRedCount": "33",
                "preGreenCount": "1",
                "greenDifference": "1",
                "greenDifferenceType": "↑",
                "preGrade": "",
                "redDifference": "-5",
                "yellowDifference": "4",
                "redDifferenceType": "↓",
                "curGreenCount": "2",
                "yellowDifferenceType": "↑",
                "curYellowCount": "21"
            },
            {
                "preRedCount": "39",
                "preYellowCount": "11",
                "curGrade": "本期三年级",
                "curRedCount": "44",
                "preGreenCount": "2",
                "greenDifference": "-1",
                "greenDifferenceType": "↓",
                "preGrade": "",
                "redDifference": "5",
                "yellowDifference": "-4",
                "redDifferenceType": "↑",
                "curGreenCount": "1",
                "yellowDifferenceType": "↓",
                "curYellowCount": "7"
            },
            {
                "preRedCount": "34",
                "preYellowCount": "3",
                "curGrade": "本期四年级",
                "curRedCount": "34",
                "preGreenCount": "1",
                "greenDifference": "0",
                "greenDifferenceType": "-",
                "preGrade": "",
                "redDifference": "0",
                "yellowDifference": "0",
                "redDifferenceType": "-",
                "curGreenCount": "1",
                "yellowDifferenceType": "-",
                "curYellowCount": "3"
            },
            {
                "preRedCount": "47",
                "preYellowCount": "4",
                "curGrade": "本期五年级",
                "curRedCount": "49",
                "preGreenCount": "0",
                "greenDifference": "0",
                "greenDifferenceType": "-",
                "preGrade": "",
                "redDifference": "2",
                "yellowDifference": "-2",
                "redDifferenceType": "↑",
                "curGreenCount": "0",
                "yellowDifferenceType": "↓",
                "curYellowCount": "2"
            },
            {
                "preRedCount": "37",
                "preYellowCount": "1",
                "curGrade": "本期六年级",
                "curRedCount": "36",
                "preGreenCount": "0",
                "greenDifference": "1",
                "greenDifferenceType": "↑",
                "preGrade": "",
                "redDifference": "-1",
                "yellowDifference": "0",
                "redDifferenceType": "↓",
                "curGreenCount": "1",
                "yellowDifferenceType": "-",
                "curYellowCount": "1"
            }
        ],
        "classDifferenceData": [
            {
                "curGrade": "本期一年级",
                "data": [
                    {
                        "preRedCount": "20",
                        "preYellowCount": "11",
                        "curGrade": "一年级",
                        "curRedCount": "20",
                        "preGreenCount": "0",
                        "greenDifference": "0",
                        "greenDifferenceType": "-",
                        "redDifference": "0",
                        "yellowDifference": "0",
                        "redDifferenceType": "-",
                        "class_name": "一班",
                        "curGreenCount": "0",
                        "yellowDifferenceType": "-",
                        "curYellowCount": "11"
                    },
                    {
                        "preRedCount": "12",
                        "preYellowCount": "17",
                        "curGrade": "一年级",
                        "curRedCount": "11",
                        "preGreenCount": "0",
                        "greenDifference": "2",
                        "greenDifferenceType": "↑",
                        "redDifference": "-1",
                        "yellowDifference": "-1",
                        "redDifferenceType": "↓",
                        "class_name": "二班",
                        "curGreenCount": "2",
                        "yellowDifferenceType": "↓",
                        "curYellowCount": "16"
                    }
                ],
                "preGrade": ""
            },
            {
                "curGrade": "本期二年级",
                "data": [
                    {
                        "preRedCount": "21",
                        "preYellowCount": "6",
                        "curGrade": "二年级",
                        "curRedCount": "18",
                        "preGreenCount": "1",
                        "greenDifference": "0",
                        "greenDifferenceType": "-",
                        "redDifference": "-3",
                        "yellowDifference": "3",
                        "redDifferenceType": "↓",
                        "class_name": "一班",
                        "curGreenCount": "1",
                        "yellowDifferenceType": "↑",
                        "curYellowCount": "9"
                    },
                    {
                        "preRedCount": "17",
                        "preYellowCount": "11",
                        "curGrade": "二年级",
                        "curRedCount": "15",
                        "preGreenCount": "0",
                        "greenDifference": "1",
                        "greenDifferenceType": "↑",
                        "redDifference": "-2",
                        "yellowDifference": "1",
                        "redDifferenceType": "↓",
                        "class_name": "二班",
                        "curGreenCount": "1",
                        "yellowDifferenceType": "↑",
                        "curYellowCount": "12"
                    }
                ],
                "preGrade": ""
            },
            {
                "curGrade": "本期三年级",
                "data": [
                    {
                        "preRedCount": "16",
                        "preYellowCount": "9",
                        "curGrade": "三年级",
                        "curRedCount": "24",
                        "preGreenCount": "1",
                        "greenDifference": "-1",
                        "greenDifferenceType": "↓",
                        "redDifference": "8",
                        "yellowDifference": "-7",
                        "redDifferenceType": "↑",
                        "class_name": "一班",
                        "curGreenCount": "0",
                        "yellowDifferenceType": "↓",
                        "curYellowCount": "2"
                    },
                    {
                        "preRedCount": "23",
                        "preYellowCount": "2",
                        "curGrade": "三年级",
                        "curRedCount": "20",
                        "preGreenCount": "1",
                        "greenDifference": "0",
                        "greenDifferenceType": "-",
                        "redDifference": "-3",
                        "yellowDifference": "3",
                        "redDifferenceType": "↓",
                        "class_name": "二班",
                        "curGreenCount": "1",
                        "yellowDifferenceType": "↑",
                        "curYellowCount": "5"
                    }
                ],
                "preGrade": ""
            },
            {
                "curGrade": "本期四年级",
                "data": [
                    {
                        "preRedCount": "19",
                        "preYellowCount": "0",
                        "curGrade": "四年级",
                        "curRedCount": "17",
                        "preGreenCount": "0",
                        "greenDifference": "1",
                        "greenDifferenceType": "↑",
                        "redDifference": "-2",
                        "yellowDifference": "1",
                        "redDifferenceType": "↓",
                        "class_name": "一班",
                        "curGreenCount": "1",
                        "yellowDifferenceType": "↑",
                        "curYellowCount": "1"
                    },
                    {
                        "preRedCount": "15",
                        "preYellowCount": "3",
                        "curGrade": "四年级",
                        "curRedCount": "17",
                        "preGreenCount": "1",
                        "greenDifference": "-1",
                        "greenDifferenceType": "↓",
                        "redDifference": "2",
                        "yellowDifference": "-1",
                        "redDifferenceType": "↑",
                        "class_name": "二班",
                        "curGreenCount": "0",
                        "yellowDifferenceType": "↓",
                        "curYellowCount": "2"
                    }
                ],
                "preGrade": ""
            },
            {
                "curGrade": "本期五年级",
                "data": [
                    {
                        "preRedCount": "25",
                        "preYellowCount": "3",
                        "curGrade": "五年级",
                        "curRedCount": "26",
                        "preGreenCount": "0",
                        "greenDifference": "0",
                        "greenDifferenceType": "-",
                        "redDifference": "1",
                        "yellowDifference": "-1",
                        "redDifferenceType": "↑",
                        "class_name": "一班",
                        "curGreenCount": "0",
                        "yellowDifferenceType": "↓",
                        "curYellowCount": "2"
                    },
                    {
                        "preRedCount": "22",
                        "preYellowCount": "1",
                        "curGrade": "五年级",
                        "curRedCount": "23",
                        "preGreenCount": "0",
                        "greenDifference": "0",
                        "greenDifferenceType": "-",
                        "redDifference": "1",
                        "yellowDifference": "-1",
                        "redDifferenceType": "↑",
                        "class_name": "二班",
                        "curGreenCount": "0",
                        "yellowDifferenceType": "↓",
                        "curYellowCount": "0"
                    }
                ],
                "preGrade": ""
            },
            {
                "curGrade": "本期六年级",
                "data": [
                    {
                        "preRedCount": "18",
                        "preYellowCount": "1",
                        "curGrade": "六年级",
                        "curRedCount": "18",
                        "preGreenCount": "0",
                        "greenDifference": "0",
                        "greenDifferenceType": "-",
                        "redDifference": "0",
                        "yellowDifference": "0",
                        "redDifferenceType": "-",
                        "class_name": "一班",
                        "curGreenCount": "0",
                        "yellowDifferenceType": "-",
                        "curYellowCount": "1"
                    },
                    {
                        "preRedCount": "19",
                        "preYellowCount": "0",
                        "curGrade": "六年级",
                        "curRedCount": "18",
                        "preGreenCount": "0",
                        "greenDifference": "1",
                        "greenDifferenceType": "↑",
                        "redDifference": "-1",
                        "yellowDifference": "0",
                        "redDifferenceType": "↓",
                        "class_name": "二班",
                        "curGreenCount": "1",
                        "yellowDifferenceType": "-",
                        "curYellowCount": "0"
                    }
                ],
                "preGrade": ""
            }
        ]
    }
}

img

最后生成出来的word是这种效果的
有什么办法能够让这两个list的下标保持一致吗
我之前生成的他都会多循环好几遍

  • 写回答

5条回答 默认 最新

  • 阿里嘎多学长 2025-03-10 12:03
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    Freemarker填充表格时的<#list>并列循环问题

    你遇到的问题是:在 Freemarker 生成 XML 文件时,如何保持两个 <#list> 标签的下标保持一致。

    根据你的描述,一个是班级的数据,一个是年级的数据,班级的数据中是一个列表中含了一个 data 数组。

    解决方案:

    可以使用 Freemarker 的 index 变量来保持两个 <#list> 标签的下标保持一致。例如:

    <#list classes as class>
        <#list class.data as data>
            <item>
                <class_name>${class.className}</class_name>
                <data_name>${data.name}</data_name>
            </item>
        </#list>
    </#list>
    

    在上面的代码中,我们使用 classes 列表中的每个元素 class,然后使用 class.data 列表中的每个元素 data。使用 index 变量可以保持两个 <#list> 标签的下标保持一致。

    如果你想在 XML 文件中输出每个元素的索引,可以使用 index 变量,例如:

    <#list classes as class>
        <#list class.data as data>
            <item>
                <class_name>${class.className}</class_name>
                <data_name>${data.name}</data_name>
                <index>${class_index}</index>
            </item>
        </#list>
    </#list>
    

    在上面的代码中,我们使用 class_index 变量来输出每个元素的索引。

    希望这个解决方案能够帮助你解决问题!

    评论

报告相同问题?

问题事件

  • 创建了问题 3月10日