Domanda

I need to save Date Time in the (oracle) database in one column, which is sqlType of timestamp (looks like 01-JAN-14 12.00.00.000000 AM). While learning grails I've been using the Joda lib with it's "time picker".

The Joda timepicker has worked well, but now that I'm looking to go primetime I'm looking for something a little more user friendly. Frankly, text boxes might be more user friendly than the drops downs joda gives you.

Anyway, I'd like to remove joda and use something like this:

http://trentrichardson.com/examples/timepicker/

but I can't figure out how to implement it in grails. In my view, if I put:

<input type="text" name="endDate" id="endDate" value="${exampleInstance?.endDate}" />

in place of the g:datePicker, it works fine (the picker that is), except nothing gets saved to the database, and no errors are generated. I hit Save and the Show view comes up with an empty endDate field. Do I need more input tags?

Is there some easy way to implement a modern looking date+time picker that I've missed?

Furthermore, I see there is a plugin for this picker here

http://grails.org/plugin/jquery-ui-timepicker

But being that there isn't any documentation, I'm not sure how to use that either (?)

ANSWER

in controller save/update put something like:

def endDate = params.date('endDate', 'yy-MM-dd h:mm')
//println "Date from Picker was "+endDate
params.endDate = endDate

No further casting was necessary being that it ended up I could format the datepicker control to a very close format as what's in the database, but had I needed to cast from one odd format, or a string, to another, I toyed with this code, which is more psuedo than anything as I was thinking through the process (I'm sure there's a totally Groovy way to do this same thing):

SimpleDateFormat inputFormat  = new SimpleDateFormat("yy-MM-dd HH:mm:ss.S");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy h.mm.ss.S a");
def v = params.endDate
Date date = inputFormat.parse(v);
String temp = sdf.format(date)
Date dateOut = sdf.parse(temp)

println dateOut 
È stato utile?

Soluzione 2

In your controller, you will need to parse the parameter value to a Date value. Something like,

def endDate = params.date('endDate', 'dd-MM-yyyy')

dd-MM-yyyy is whatever format the jquery plugin submits the date value. (println params for this or look up the plugin documentation)

If you want a date format binding to happen automatically, check the Date Formats For Data Binding in the doc http://grails.org/doc/latest/guide/single.html#dataBinding for a way to globally specify the format

Altri suggerimenti

The datepicker, is your UI component therefore, you can have any library that you wish for UI and anything else for back-end. Mostly they are easy to implement, if they provide a little bit of documentation!!.

The timepicker for jQuery ui plugin, that you provided the link, is exposing a resource called jqueryUiTimePicker which depends on jQuery and jQuery-ui. So simply by including this resource into you resources configuration you should be able to utilize it. Its no different than defining your own resource and use it.

About saving issue that you have, on your save pass parameter failOnError:true so you can see the errors if any.

I have created a sample project that utilizes this plugin hope it helps

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top