Creating A Basic Spry Data Set and Dynamic Region

In our first Spry example, I'll go over how to create a basic Spry data set and bind the data set to a dynamic region of the web page. In future examples we will add additional Spry features.

Step 1 - Create a CFC method that returns the data you want as XML (GetInfo.cfc in my example). Make sure you set the output to false for the component and for the function. Return type must be XML (which works beginning with CFMX 7.0.1).

Step 2 - Test calling the CFC via a URL. You should see your data and if you view source you should see the XML formatting. Note the name of the root node (the first node) and the node below the root that repeats for each set of data. In my example the root node is NewDataSet and Table is the node below that and repeats for each row of data.

Step 3 - Create the web page used to display the data. Include the two JavaScript files: SpryData.js and xpath.js.

Step 4 - Inside a script type=text/javascript tag create an object of the Spry data set. The call to the Spry XMLDataSet method takes two arguments. The first is the file that provides the XML data and the second is the root node/first node. In my example:

<InvalidTag type="text/javascript">
var dsCityInfo = new Spry.Data.XMLDataSet("getInfo.cfc?method=getInfoByCity&city=Phoenix", "NewDataSet/Table");

Step 5 - Create a div block

The class is optional. The value for spry:region is the name of the XMLDataSet object we created above

<div class="docBlock" spry:region="dsCityInfo">

Step 6 - Inside the div block create a table and have a row use the spry:repeat attribute. That table row will be repeated for each row of data in the XMLDataSet object

<div class="docBlock" spry:region="dsCityInfo">


<th>Area Code</th>
<th>Time Zone</th>


<tr spry:repeat="dsCityInfo">




Some things to note:

Spry is case sensitive. So you must match the case of your XML nodes.

You don't have to use every node in your output. You can just specify the nodes you want to use.

Lastly, we can add some nice-to-have features to the Spry data set.

While Spry is creating the XMLDataSet object we should let the user know that something is going on. We can use the Spry:State attribute for a div tag to have a specific message (or animated GIF) appear while Spry is loading the data set.

<div spry:state="loading" id="notification2">Loading cities, please wait ...</div>

You put this div inside the div with the spry:region attribute. While Spry is loading the data set for that region the div will be visible.

You can specify that columns can be sorted. Add to the tag this code:


When the user clicks on the column heading, Spry will sort the data set in by the node specified. The toggle argument to the sort function means that the sort can go from ascending to descending and back.

You can view the example page here. Use View Source to the source code.

In our next example, we will add the ability for the user to select the city and based on the user's city selection we will have Spry update our table containing the information about that city (state, zip, area code, etc) without refreshing the page.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Can getInfo.cfc be in a components directory?
I'd like to put it in xyz.components.getInfo.cfc.
If I were to Create and Object, I would say
<cfset Obj = CreateObject("component","xyz.component.getInfo").Init()>
# Posted By Phillip Senn | 3/2/07 10:03 PM
Yes, you can put getInfo.cfc in another directory. If you look at the spry examples on adobe labs you'll see how they provide the path to the cfc when they create a data set.
# Posted By Bruce | 3/3/07 6:14 AM
Can getInfo.cfc be in a components directory?
# Posted By stop smoking | 12/29/07 8:25 AM
Hi Bruce

Any chance we could see an example of GetInfo.cfc, i'm faitly new to CF and would love to find the best way to return XML from a cfc.


# Posted By Tim | 6/11/08 5:17 AM
Tim - check my blog for an entry I wrote about how to return XML to both Flex and Spry. I think I included an explanation and code with how to create a CFC that returns XML.
# Posted By Bruce | 6/11/08 5:39 AM
Cheers Bruce, works a treat. Nice one!
# Posted By Tim | 6/11/08 9:40 AM
Try appending "&_cf_nodebug=true" to your URL's that request data/xml from a CFC. It turns off debug output on CF 8 for the CFC call that can break xml formatting.
# Posted By Scott Jibben | 8/12/08 9:35 PM
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner