【问题标题】:how to restrict instances in drop down如何在下拉列表中限制实例
【发布时间】:2013-05-26 23:03:02
【问题描述】:

我有一个包含模板和外观的实例表单。 要创建一个新表单,我有一个包含 2 个下拉列表的视图,其中包含模板和外观。现在我想将模板和外观限制为当前登录用户拥有的模板和外观。

我正在使用 tostring 方法来格式化下拉列表。

【问题讨论】:

    标签: grails scaffolding


    【解决方案1】:

    在您的控制器中,您可能希望查询经过身份验证的用户的数据。假设您的 Template.groovy 看起来像这样:

    class Template {
      String name
      static belongsTo = [owner: User]
    }
    

    然后在你的控制器的动作中:

    def create() {
       def authenticatedUser = .... // however you get the logged in user
       def templates = Template.findAllByOwner(authenticatedUser)
       [templates: templates]
    }
    

    然后在你的 create.gsp 中:

    <g:select from="${templates} ... />
    

    显然,对外观做同样的事情。

    【讨论】:

    • 感谢您的好回答!它增加了我对整个 Grails 故事的认识。
    猜你喜欢
    • 2019-10-30
    • 2018-10-23
    • 2021-04-24
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2011-03-05
    相关资源
    最近更新 更多