Reputation: 1949
Hello here is what i'm trying to do. When user presses link it adds value to the list or set inside params. for example on the first click it adds myId=1 then it becomes /?myId=1 then on the second click on some other link it adds myId=25 then it should become /?myId=1&myId=25
I have tried this:
<g:link action="test" params="${params+['myId':'1']}"> first</g:link>
<g:link action="test" params="${params+['myId':'25']}"> second</g:link>
but it just switches between /?myId=1 or /?myId=25 and never becomes /?myId=1&myId=25.
How do i do this?
Upvotes: 0
Views: 1170
Reputation: 1949
Have found other possible solution. Downside is the list or set has to have one dummy value which can be ignored. For example 0.
<g:link action="test" params="${['ids': (params.ids as Set) + 'ab' ]}"> + ab</g:link> <br>
<g:link action="test" params="${['ids' : (params.ids as Set) + 'cd' ]}"> + cd</g:link> <br>
In the controller:
if(!params.ids || params.ids?.size()==1) params.ids= [0]
as Set
is used because duplicate value is not wanted.
Reason why i'm using dummy value in (list or set) is that when first value has two or more characters it becomes seperated seperated. For example if "+ ab" link is pressed first time it becomes [a,b] not [ab]. Then if it is pressed again it becomes [a,b,ab] which is not a desired result. But when it already has 0
in it, it becomes [0, ab]. Later in the controller 0
can be just ignored.
<g:link action="test" params="${['ids' : params.ids - ['ab']]}"> - ab</g:link> <br>
<g:link action="test" params="${['ids' : params.ids - ['cd']]}"> - cd</g:link> <br>
Answered my own question just in case another noobie like me has come across same problem
Upvotes: 0
Reputation:
That's because you are trying to add the key myId
to params
. Since it already have the key, the value is updated.
You can separe your values with something like ,
, remaining with one single key.
<g:link action="test" params="${params.myId ? [myId: params.myId+',1'] : [myId:1]}"> first</g:link>
<g:link action="test" params="${params.myId ? [myId: params.myId+',15'] : [myId:15]}"> second</g:link>
In your controller you can transform myId in a list with tokenize()
println params.myId.tokenize(',')
Upvotes: 1