简体   繁体   English

Grails,从gsp更改域类属性

[英]Grails, change domain class property from gsp

what's the method used to change a domain classes property within the gsp? 在gsp中更改域类属性的方法是什么?

Ex: 例如:

a domain class project has a dueDate which is of type date. 域类项目的DueDate类型为date。 I want to within the gsp, set its date without using the tag The reason is, I'm using jquery's datepicker which is nice since instead of having an ugly dropdown for mm/dd/yyyy it has a nice little calendar for one to click on. 我想在gsp内设置日期而不使用标签原因是,我使用的是jquery的datepicker,这很不错,因为它没有mm / dd / yyyy的丑陋下拉菜单,而是可以点击一个漂亮的小日历上。 Anywho, any ideas? 任何人有什么想法吗?

Thanks in advance :D :D :D 在此先感谢:D:D:D

Well, Grails uses a MVC pattern and therefore you should not directly change a domain class property within a GSP page. 好吧,Grails使用MVC模式,因此您不应直接在GSP页面中更改域类属性。

Of course you can use the JQuery date picker but you should provide a controller action to update your domain class property 当然,您可以使用JQuery日期选择器,但应提供控制器操作来更新您的域类属性

def updateDateUsingAjax() {
  def domain = MyDomain.load(params.id)

  /*
  Lets pretend the content of params.date has the format MM/dd/yyyy  
  You can use Date.parse method of the Groovy JDK to create a java.util.Date instance of a String. 
  http://groovy.codehaus.org/groovy-jdk/java/util/Date.html#parse(java.lang.String, java.lang.String)
  */

  domain.myDate = Date.parse('MM/dd/yyyy', params.date)

  domain.save()
}

Now all you have to write is an Ajax call to this controller action and you successfully separated the presentation layer from the rest of your application. 现在,您只需要编写对该控制器操作的Ajax调用,就可以成功地将表示层与应用程序的其余部分分离。

And here is how your GSP could look like. 这就是您的GSP的外观。

<!doctype html>
<html>
<head>
    <meta name="layout" content="main"/>
    <title>Welcome to Grails</title>
    <r:require module="jquery-ui"/>
</head>
<body>
<div>
    <g:formRemote name="myForm" url="[controller: 'standard', action: 'updateDateUsingAjax']" onSuccess="showDialog()">
        <p>Your date: <g:textField name="pick"/> </p>
        <p><g:hiddenField name="id" value="your id"/></p>
        <input type="submit" value="Delete Book!" />
    </g:formRemote>

</div>

<div id="dialog" title="Success" style="display:none;">
    <p>You have successfully updated your date</p>
</div>

<script type="text/javascript">
    $(document).ready(function()
    {
        $("#pick").datepicker({dateFormat: 'yy/mm/dd'});
    })

    function showDialog() {
        $("#dialog").dialog();
    }
</script>
</body>

There is a Grails JQuery UI plugin which might suit your needs. 有一个Grails JQuery UI插件可能适合您的需求。 See http://grails.org/plugin/jquery-ui for more information. 有关更多信息,请参见http://grails.org/plugin/jquery-ui

Even if this particular plugin doesn't suit your needs I would think there will be a plugin out there that will. 即使这个特定的插件无法满足您的需求,我也会认为那里会有一个插件。

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

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