0

In my controller I have conditions, for example if value is 80, then I need to show certain button in my view, if value is 50 then I need to show a different button in my view. How would I do this in grails?

Test User
  • 57
  • 1
  • 11
  • 3
    [7.2.2.2 Logic and Iteration](http://grails.org/doc/latest/guide/theWebLayer.html#tagLogicAndIteration) – hsan May 15 '13 at 14:11

3 Answers3

1

Its seem not to be a controller logic. You can just in the view do something like:

<g:if test="${val == 80}">
      <input type="submit" value="Submit">
</g:if>
<g:else>
   <input type="button" value="a button">
</g:else>

If you want to send val from the controller to the view, its something like:

class TestController {
    def index = {
      ['val':80] //or [val: params.val] if you want to get it from parameters.
    }

}

abertolo
  • 36
  • 3
  • Ya I did something like that, however how do you pass val? It seems my value for val from my controller is not being recognized by the view. – Test User May 15 '13 at 17:44
  • check this out http://stackoverflow.com/questions/4624214/gsp-parameter-passing-from-controller – Mr. Cat May 15 '13 at 17:46
0

If you want to do this on the same page, you need javascript.
If you want to render page according condition, try <g:if> tag

Mr. Cat
  • 3,522
  • 2
  • 17
  • 26
0

I feel a better way to do this is to use a tag library rather than having logic in your .gsp. Also you can reuse this logic if it is needed elsewhere in your application.

// in your gsp
<lib:showButtons myValue="$val"/>

// in your tag lib
def showButtons = { attrs ->
  def myValue = attrs.myValue
  def value = "Submit"
  def type = "submit"

  if(myValue != 80) {
    value = "a button"
    type = "button"
  }

  out << '<input type="$type" value="$value" />'
}
dspano
  • 1,540
  • 13
  • 25