今天在试验用Jxls 2.0导出嵌套循环数据时,第二层数据一直没有成功,最后确认是数据源结构不正确所致,现将这两种数据格式进行说明:
假设模板中批注有这样两条循环语法:
- <jx:each(items="departments" var="department" lastCell="E10")>
- <jx:each(items="" var="department.staff" lastCell="E9")>
(1)data数据如下:
"data" : {
"departments" : [
{"chief" : {
"name" : "benny",
"age": "18",
"sex": "男",
"staff": [
{
"name" : "小王",
“age” : "10",
"sex" : "男"
},
{
"name" : "小刘",
“age” : "9",
"sex" : "女"
}
]
}},
{"chief" : {
"name" : "bien",
"age": "20",
"sex": 女",
"staff": [
{
"name" : "小花",
“age” : "12",
"sex" : "女"
},
{
"name" : "小赵",
“age” : "13",
"sex" : "男"
}
]
}}
]
}
在这个data中,虽然看起来有列表嵌套,但是要注意了,第一层departments有数据循环,而第chief没有数据循环,到第三层staff时又出现了数据循环,因此在制作模板的时候得小心了,别把第三层的数据循环当作第二层的数据循环了,若是在模板中设置了第二层的循环,那么会提示department.staff不是一个集合;若要想达到第三层循环数据也能够被正常导出,那么你可以这样设置语法:将department.staff修改为department.chief.staff即可。
(2)data数据如下:
"data" : {
"departments" : [
{
"name" : "benny",
"age": "18",
"sex": "男",
"staff": [
{
"name" : "小王",
“age” : "10",
"sex" : "男"
},
{
"name" : "小刘",
“age” : "9",
"sex" : "女"
}
]
},
{
"name" : "bien",
"age": "20",
"sex": 女",
"staff": [
{
"name" : "小花",
“age” : "12",
"sex" : "女"
},
{
"name" : "小赵",
“age” : "13",
"sex" : "男"
}
]
}
]
}
此时,data只有第一层和第二层数据循环,没有第三层的数据循环了;同时,前文中的两句语法也是正确的了,适合这种数据场景导出。
20191024 闪