简体   繁体   English

迭代 Json map,创建带有嵌入式链接的 html 表

[英]Iterate over Json map, create html table with embedded links

I'm reviewing an XML markupbuilder script that iterates through a json array (data from which is pulled from an external api) and displays the data in as an HTML table as follows:我正在查看一个 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()

The code returns this table: https://i.stack.imgur.com/nFCIP.png代码返回此表: https://i.stack.imgur.com/nFCIP.png

There's an additional thing I want to do with the table... I want to embed a link in each value in the number column, and I want to reference the number value in the link itself for each respective row.我还想对表格做一件事......我想在数字列的每个值中嵌入一个链接,并且我想为每一行引用链接本身中的数字值。 So if the number value for a row was CHG0010042, then the embedded link would be "my.test.com/CHG0010042", then for the next row value (CH0010061), the link would be "my.test.com/CH0010042", and so on and so forth.因此,如果一行的数值是 CHG0010042,那么嵌入的链接将是“my.test.com/CHG0010042”,那么对于下一行值 (CH0010061),链接将是“my.test.com/CH0010042” , 等等等等。

What would I need to add to the tbody block in order to successfully do that?为了成功做到这一点,我需要向tbody块添加什么?

Thanks @daggett!谢谢@daggett!

I was able to expand on the iterator for the result values and reference the condition on the column header:我能够扩展结果值的迭代器并引用 header 列上的条件:

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
  }
  """
}
def headerMaps = [
  'u_change_record.number': 'Number',
  'u_change_record.state' : 'State',
  'u_change_record.short_description' : 'Short Description'
]
def number = json["result"]*."u_change_record.number"
xml.table(class:'aui'){
  thead {
    tr{
      json.result[0].keySet().each{
        th headerMaps[it] ?: it.split(/\./)[1]
      }
    }
  }
tbody {
  json.result.each{ res->
    tr{
      res.values().eachWithIndex{val, idx->
        def colHeader= res.keySet()[idx]
        if( colHeader == 'u_change_record.number'){
          td {
            a (href:"https://test.com/$val",target:'_blank') {
              mkp.yield val
              }
          }
        } else {
            td val
          }
      }
    }  
  }
}       
}
writer.toString()

Results are now as expected: view here结果现在符合预期:在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM