0

what's the method used to change a domain classes property within the gsp?

Ex:

a domain class project has a dueDate which is of type 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. Anywho, any ideas?

Thanks in advance :D :D :D

Riveascore
  • 1,724
  • 4
  • 26
  • 46

3 Answers3

1

Well, Grails uses a MVC pattern and therefore you should not directly change a domain class property within a GSP page.

Of course you can use the JQuery date picker but you should provide a controller action to update your domain class property

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.

And here is how your GSP could look like.

<!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>

saw303
  • 8,051
  • 7
  • 50
  • 90
  • So how would I pass a "new Date()" to this controller call. Because even without ajax, I could just called the controller method with teh following syntax: ${updateDateUsingAjax()} Right? But the thing is, how do I pass a new date to that? I'm a tad lost, I see your idea and it seems genius but I'm missing a few details. Because right now all I can do is use the built in tag. Thank you for your idea, I just would like to understand it more. – Riveascore Apr 20 '12 at 08:28
  • Just a sec I am trying to provide you an example view. so you get the whole context – saw303 Apr 20 '12 at 09:00
  • Ok, thank you so much, because I'm super lost. In fact, now I have no idea how to really call a controller in gsp. All our stuff had scaffolding set to true, then we deleted that, and everything was kind of created automatically. I'm trying to read through the documentation and it's just not making sense how to do this kind of stuff. – Riveascore Apr 20 '12 at 09:12
  • Now you find the GSP code that possibly could solve your problem – saw303 Apr 20 '12 at 11:32
  • Omg, thank you so much, there are so many details in here that you helped clarify for me. I didn't think you could affect a grails tag via its name, I thought you could only affect html elements that you had already given an id yourself. – Riveascore Apr 20 '12 at 19:23
  • So it doesn't work. I'm still getting the error: Error 500: Executing action [updateDateUsingAjax] of controller [survey.ProjectController] caused exception: java.text.ParseException: Unparseable date: "Tue Apr 24 22:14:23 CDT 2012" – Riveascore Apr 24 '12 at 03:11
  • If I remove the parsing part of the controller I get the following: Error 500: Executing action [updateDateUsingAjax] of controller [survey.ProjectController] caused exception: Cannot invoke method save() on null object – Riveascore Apr 24 '12 at 03:21
0

There is a Grails JQuery UI plugin which might suit your needs. See http://grails.org/plugin/jquery-ui for more information.

Even if this particular plugin doesn't suit your needs I would think there will be a plugin out there that will.

David
  • 1,940
  • 3
  • 17
  • 30
  • This just didn't work at all, I'm getting submission errors, it's not submitting as type date or something of the sort. – Riveascore Apr 24 '12 at 03:24
0

Maybe you should try Joda-Time plugin: http://grails.org/plugin/joda-time

And take a look on this blog post: Rendering Grails Joda-Time date inputs cross browser with HTML5, jQuery and Modernizr

Roberto Perez Alcolea
  • 1,408
  • 11
  • 11