我正在查看一个 XML 标记生成器脚本,该脚本遍历一个 json 数组(数据从外部 api 中提取)并将数据显示为 HTML 表,如下所示:
import groovy.json.JsonSlurper
import groovy.xml.MarkupBuilder
def jsonString ="""
{
"result": [
{
"u_change_record.number": "CHG0010042",
"u_change_record.state": "Draft",
"u_change_record.short_description": "test app req 5"
},
{
"u_change_record.number": "CHG0010061",
"u_change_record.state": "Draft",
"u_change_record.short_description": "test"
},
{
"u_change_record.number": "CHG0016010",
"u_change_record.state": "Draft",
"u_change_record.short_description": "Test Jira"
},
{
"u_change_record.number": "CHG0010057",
"u_change_record.state": "Draft",
"u_change_record.short_description": "tesst"
}
]
}
"""
def json = new JsonSlurper().parseText(jsonString)
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.style{
mkp.yieldUnescaped """
table.aui {
border-collapse: collapse;
width: 100%
}
table.aui>thead {
font-family: arial, sans-serif;
border-bottom: 2px solid #dfe1e6
}
table.aui>tbody>tr, table.aui>tfoot>tr {
background: white;
color: #172b4d;
font-family: arial, sans-serif;
}
table.aui>tbody>tr>td,table.aui>tbody>tr>th,table.aui>tfoot>tr>td,table.aui>tfoot>tr>th,table.aui>thead>tr>td,table.aui>thead>tr>th {
padding: 7px 10px;
text-align: left;
vertical-align: top
}
table.aui>tbody>tr>th,table.aui>thead>tr>th {
color: #7a869a;
font-size: 12px;
font-weight: 600;
line-height: 1.66666667;
letter-spacing: 0;
text-transform: none
}
table.aui:not(.aui-table-list)>tbody>tr>td,table.aui:not(.aui-table-list)>tbody>tr>th,table.aui:not(.aui-table-list)>tfoot>tr>td,table.aui:not(.aui-table-list)>tfoot>tr>th {
border-bottom: 1px solid #dfe1e6
}
"""
}
xml.table(class:'aui'){
thead{
tr{
json.result[0].keySet().each{
th it.split(/\./)[1]
}
}
}
tbody{
json.result.each{ res->
tr{
res.values().each{
td it
}
}
}
}
}
writer.toString()
代码返回此表:https ://i.stack.imgur.com/nFCIP.png
我还想对表格做一件事......我想在数字列的每个值中嵌入一个链接,并且我想为每一行引用链接本身中的数字值。因此,如果一行的数值是 CHG0010042,那么嵌入的链接将是“my.test.com/CHG0010042”,那么对于下一行值 (CH0010061),链接将是“my.test.com/CH0010042” , 等等等等。
为了成功做到这一点,我需要向tbody块添加什么?