Play! edit your dates with JQuery (editinplace + datepicker) 1/2

On my first tweet, I posted a link on a sniplet ( explaining how to mix editinplace and datepicker in JQuery.

Here, I’ll explain how to finish the job of ths sniplet (update the date picker on date field update) and integrate it in a Play! application listing a set of object (each having a date field – a very simple task example).

Create a Task model :

package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;

public class Task extends Model {
    public String action;
    public Date due;

Review the index method in Application

    public static void index() {
    	List tasks = Task.findAll();

And list the tasks in a table (modifiy the template : view/index.html) with a form to add a new task (last line of the table)

#{extends 'main.html' /}
#{set title:'Home' /}

			<th>Due date</th>
	#{list tasks, as:'task'}

	<tr>			    #{form @add()}
	        <td><input type="text" name="task.action"></td>
	        <td><input type="text" name="task.due"></td>
	        <td><input type="submit" value="+"></td>

And add the add action in Application

    public static void add(final Task task) {;

and don’t forget to activate the database settings in conf/application.conf


Now you have a basic play! application to save tasks.

basic task application

Looking at the state, the date field is not well formated. Just replace the field showing the date with the following code and we’ll just see the day part of the date without time.


Tasks with a formatted due date, using Groovy

Let’s begin adding our Ajax behaviour.

Look at /public/javascripts : there’s already JQuery installed (jquery-1.4.2.min.js, which is now the lastest version; You’ll can get latest version of JQuery to upgrade your site later). Now look at the main.html template (under app/views), the script is already included.

Adding a datepicker

To add a datepicker, we need to getJQuery UI.

The page is well designed :

  1. you choose the components you want to included, so you can just pick the ones you want for your site and have a lightweight version that just fits your needs. For this example, we just need the core (obviously) and datepicker in Widget section.
  2. you can choose the theme you want to embed in your package, on the right of the page. I’ve choose smoothness.

Once you’ve downloaded the pack, get the script (jquery-ui-1.8.5.custom.min.js) from the js folder and put it on public/javascripts one of your application and get your theme folder (the whole folder) under css in the package and copy it to /public/stylesheets.

Add the following lines just below the ones of JQuery (css and javascript ones) in app/views/main.html :
      <link rel="stylesheet" type="text/css" media="screen" href="@{'public/stylesheets/smoothness/jquery-ui-1.8.5.custom.css'}" />
        <script src="@{'/public/javascripts/jquery-ui-1.8.5.custom.min.js'}" type="text/javascript" charset="utf-8"></script>
Your main.html template should now look like this :
<!DOCTYPE html>

        <title>#{get 'title' /}</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" media="screen" href="@{'/public/stylesheets/main.css'}">
        <link rel="stylesheet" type="text/css" media="screen" href="@{'public/stylesheets/smoothness/jquery-ui-1.8.5.custom.css'}" />

        #{get 'moreStyles' /}
        <link rel="shortcut icon" type="image/png" href="@{'/public/images/favicon.png'}">
        <script src="@{'/public/javascripts/jquery-1.4.2.min.js'}" type="text/javascript" charset="utf-8"></script>
        <script src="@{'/public/javascripts/jquery-ui-1.8.5.custom.min.js'}" type="text/javascript" charset="utf-8"></script>
        #{get 'moreScripts' /}
        #{doLayout /}

Now we have to associate a datepicker with the field date in the form : first add a class tag the the field :

	        <td><input class="due" type="text" name="task.due"></td>

and add the JQuery code to associate it with a datepicker :

<script type="text/javascript" charset="utf-8">
        $(".due").datepicker({dateFormat:'dd-mm-yy', showAnim:'fadeIn'})

Now you have the basic application with a datepicker.

Adding new tasks with a date picker.

Next step is to add editinplace on existing tasks and associate a datepicker on thme for the date. All this in next post…


About this entry