如何创建一个带有闭包的表模型放置多个变量形式循环

时间:2018-03-09 17:15:57

标签: groovy closures

我看到这个代码用于创建一个带闭包的表

https://varomorf.wordpress.com/2014/09/22/update-jtable-using-groovy/

但现在我需要创建一个带闭包的表

但是使用文本标题来创建所有闭包变量

像这样;此代码获取表格,但在此案例4

中使用了最后一个xbn值
 theTable = table(){
        tableModel(){
var1="fecha"
xbn=0
stx="date;product;quant;weight;price".split(";")
println it
while(xbn<4) {
          closureColumn(header:stx[xbn], read:{it[stx[xbn]]})   ;xbn=xbn+1  }



        }
      }

通常我的代码没有循环

看起来像这样

 theTable = table(){
        tableModel(){
var1="fecha"
xbn=0
stx="date;product;quant;weight;price".split(";")
println it

          closureColumn(header:"date", read:{it["date"]})    
          closureColumn(header:"product", read:{it["product"]})    
              closureColumn(header:"quant", read:{it["quant"]})    
              closureColumn(header:"weight", read:{it["weight"]})    
              closureColumn(header:"price", read:{it["price"]})    

        }
      }

请帮帮我

1 个答案:

答案 0 :(得分:1)

大多数DSL都不会阻止您使用常规的groovy东西。所以你可以迭代乘法次数,但你必须命名你的闭包循环变量(例如你的外循环是tableModel,它隐含地命名为it)。

...
tableModel() { // it ->
    ...
    "date;product;quant;weight;price".split(";").each { hdr -> // name the loop var
        closureColumn(header:hdr, read:{it[hdr]})    
    }
    ...
}
...