【问题标题】:How to show and hide buttons in grails depending on the condition?如何根据条件在grails中显示和隐藏按钮?
【发布时间】:2013-05-15 14:06:46
【问题描述】:

在我的控制器中,我有条件,例如,如果值为 80,那么我需要在我的视图中显示某个按钮,如果值为 50,那么我需要在我的视图中显示一个不同的按钮。我将如何在 grails 中执行此操作?

【问题讨论】:

标签: grails button controller conditional-statements


【解决方案1】:

它似乎不是控制器逻辑。您可以在视图中执行以下操作:

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

如果你想将 val 从控制器发送到视图,类似于:

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

}

【讨论】:

【解决方案2】:

如果您想在同一页面上执行此操作,则需要 javascript。
如果你想根据条件渲染页面,试试<g:if>标签

【讨论】:

    【解决方案3】:

    我觉得更好的方法是使用标签库而不是在 .gsp 中包含逻辑。如果您的应用程序的其他地方需要它,您也可以重用此逻辑。

    // 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" />'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多