Home

Using a Thickbox Modal Window with AJAX

Thickbox is a great javascript plugin to jQuery that you can use to create modal windows within SilverStripe pages.  You can de-clutter your interfaces and re-use small snippets of common functions when you add in AJAX.  Before we begin there is a little homework:

  • Read the AJAX Basics Recipe on the Doc Wiki
  • Its a good idea to use some kind of naming convention with your methods and template so you don't get them confused.  I typically prepend them with lower case ajax.

For this example we will be creating a simple Clock that will be returning the time through a custom template in a Thickbox.  This is not very complex but it does illustrate the possibilities with modals and AJAX calls.

We will start with a standard Page type that has been created.  The Absolute URL to access our page is /my-page/

Lets begin

First, lets create the template we will be using to populate our Thickbox window.  You will need to create this at the top level of your template directory, as we will be using renderWith() to send the content to the browser.

/mysite/templates/ajaxClock.ss

 

Notice the javascript onClick event, the function tb_remove() is required to close the Thickbox window when using the optional modal=true setting.

Next, lets get our requirements squared away.  The best place to do this is in the init() function of the Controller you will be using Thickbox from.

/mysite/code/model/Page.php (Page_Controller class):

 

And lets add the function to return the template and data in that same class:

 

Now from our Page AJAX calls will have access to /my-page/ajaxTime after we flush.   You can use Firebug to check out what happens.

The last step is to add the proper link to our regular Page template to invoke our Thickbox Clock.

/mysite/templates/Page.ss:

 

After flushing you should see these links included in your Pages.  Clicking on the link above will bring up a nice Thickbox with your content.  Merely adding the class="thickbox" will make everything work (thanks to the magic of the jQuery team and Cody Lindley).

That's all!

Thickbox working smoothly within your SS pages with AJAX.  Go ahead and add all those nerdy frameworks and acronyms to your resume.

Note: If you want to use the Loading Bar animation that comes with Thickbox, you will have make the following change to the core jsparty library.

/jsparty/jquery/plugins/thickbox/thickbox-code/thickbox.js:

 

About the Author

Name: dalesaurus

Comments (4)

  • Can you please update this for 2.4.

    Posted by Will, 04/05/2010 6:05am (3 months ago)

  • Is there an error in /mysite/templates/ajaxClock.ss?
    I cannot find

    \\\"Notice the javascript onClick event,\\\"

    Posted by Christian, 23/03/2010 8:38am (4 months ago)

  • Interesting tutorial for people who, like me, want to start using Ajax in combination with SilverStripe.

    I did need to change a few things to get it working, though. Maybe this is of help for people having trouble to get this working:

    - the shorthand selector for jQuery gave a conflict with other javascript, so i changed $( to jQuery( in thickbox.js.

    - In my ajaxTime function, I needed to add an argument to the php date function, otherwise I get an error.

    Posted by Stijn De Lathouwer, 21/01/2010 10:15am (6 months ago)

  • Nice work!

    Posted by Craig, 14/09/2009 2:42am (11 months ago)

RSS feed for comments on this page RSS feed for all comments

Post your comment