简体   繁体   English

GSP / Groovy-g:每个对象数组

[英]GSP/Groovy - g:each with array of objects

I'm trying to use g:each tag at a map but GSP don't recognize it. 我正在尝试在地图上使用g:each标签,但GSP无法识别它。

The code is: 代码是:

    if(previousQuestions == null)
    {
        previousQuestions = []
    }
    def obj = AnimaisTreeMap.get(curNode)

    previousQuestions.push('question':obj.nodeDescription, 'answer': getDescOptionAnswer(optionAnswered))

The g:each tag is: g:each标签为:

<g:each in="${previousQuestions}" var="row" status="i">
    <div>
        <label>${row.question}</label>
        <label>${row.answer}</label>                            
    </div>
    <br/>
</g:each>       

The output is: 输出为:

[{question=vive na água, answer=Não}, {question=tem listras, answer=Sim}]

As far as I know the object that g:each reads may be at JSON format "property:value" but my object is "property=value" 据我所知,g:each读取的对象可能采用JSON格式“ property:value”,但我的对象是“ property = value”

How could I fix it? 我该如何解决?

UPDATED 更新

After seeing this link , I found the solution. 看到此链接后 ,我找到了解决方案。 The problem itself was with "forward" function, where I passed "previousQuestions" to another action. 问题本身与“转发”功能有关,我将“ previousQuestions”传递给了另一个操作。 Instead of "params", the correct way is "model". 代替“参数”,正确的方法是“模型”。

        forward(action:'index', model: [curNode: nextNode.id,
                                        previousNode: curNode, 
                                        curQuestion: curQuestion,
                                        previousQuestions: answersTrace])                   

When I need to recover "previousQuestions", I must use "request" injected object at controller. 当我需要恢复“ previousQuestions”时,必须在控制器上使用“ request”注入的对象。 After this, my code was OK 之后,我的代码就可以了

1 : this link 1 :此链接

You appear to be adding two maps each time you call push() . 您似乎在每次调用push()时都添加了两个映射。 Try creating each map you want to add to your array of maps, instead of just passing separate key value pairs. 尝试创建要添加到地图数组的每个地图,而不只是传递单独的键值对。

I believe this is what you want: 我相信这就是您想要的:

previousQuestions.push(['question':obj.nodeDescription, 
                        'answer': getDescOptionAnswer(optionAnswered)])

Try this: 尝试这个:

//This is a far better way of checking null strings 
//shorter and also checks for '' as well as null
if(!previousQuestion)  {
   previousQuestions = []
} else {
   //this segment you have not provided so as a test ensure it 
   //matches List element above that you have declared - take a look at console
   println "this is probably your issue ${previousQuestions.getClass()} what is class here ????"
}

def obj = AnimaisTreeMap.get(curNode)

// generate a map on the fly good for iterations, 
// even though this is not iterated.

previousQuestions << ['questions': obj.nodeDescription as String, 
'answer':  getDescOptionAnswer(optionAnswered) as String]

//within genrated map above the elements have been converted to as String 
// Because you have not given the workings of getDescriptionAnswer
// in theory you should not need  as String this is just for testing

render (view: 'something', model: [previousQuestions:previousQuestions])

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

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