Using Spry to Auto-select a Form Value

So far I’ve covered how to use multi-related selects in Spry and how to dynamically update the select list upon a database update. That’s good enough for a simple ‘add record’ form but when it comes to an editing an existing record I want to be able to have the saved column’s value automatically selected in its select box.

Adobe’s samples didn’t seem to cover this scenario so it became a matter of trial and error, and a lot of wading through documentation. In the end, however, it turned out to be a matter of changing only a few lines of code.

First, let’s revisit my previous select code:

The portion we need to look at in particular is in red. ds_RowNumber reflects the position of the row currently being evaluated. ds_CurrentRowNumber is the row number of the “current row” of the data set. This works if I don’t need a specific value to be selected, but it does absolutely nothing for me if I want the saved value to be selected in the list when editing.

What I need is a way to check whether a brandID in the dsBrands dataset is equal to the brand ID of the record I’m editing. So, the first thing I need to know is how to refer to the value of the dataset’s current brand_id. This is done by enclosing the column name in single quotes, like this: ‘{brandID}’.

Now I can compare that value to the brand ID of the record I’m editing. For example, if the query object of the current record that I’m editing is named qEditProduct and the column that holds the brand id in qEditProduct is called brand_id, I would refer to it like this: #qEditProduct.brand_id# .

Compare one to the other and I have my new SELECT definition:

Now, if the value of brand ID in my select matches the value of the brand ID in the record I’m editing it will be pre-selected in the list.

To take this a step further, my original example was on multi-related selects, meaning I had subbrand select that was related to the value in the brand select. That being the case, I first have to make the same change to the subbrand select definition resulting in the following code:

Then, I also need to modify my dataset definition. Initially, dsSubbrands definition was based on the value of the brand select. The subbrands changed when the brand changed. If we’re editing, however, we want our subbrand to depend on the value stored in the current record rather than being related to the brand select. My original dsSubbrands dataset definition looked like this:

var dsSubbrands = new Spry.Data.XMLDataSet("subbrands.cfm?brandID={dsBrands::brandID}","subbrands/subbrand");

{dsBrands::brandID} is my reference to the selected item in the brands select. I need to change that so it now refers to the current record in my qEditProduct query:

var dsSubbrands = new Spry.Data.XMLDataSet("subbrands.cfm?brandID=#qEditProduct.brand_id#","subbrands/subbrand");

Now when I edit a record, the record values will be pre-selected in both the brand and subbrand dropdown. You can use the original code for ‘add record’ forms or, for more condensed code, combine them both into a conditional check based on whether the user is adding or editing a record.


4 thoughts on “Using Spry to Auto-select a Form Value

  1. Hi,

    I think you’re missing something here. The last modification to the dsSubbrands dataset in your example code breaks the mechanism of having the subbrand changed automatically as soon as the brand changes.

    It’s tied to whatever is fed into the page now, so that it load correctly initially, but you would never be able to modify that dataset based on your settings for the current brand, would you?

    Any idea to to solve that smoothly?


  2. What is the advantage of the above method over something more traditional like this below (where I am generating a select list where the options value is artist_id and the name is artist_name and I want to have the select list pre-selecting already existing artist_id’s)?


  3. Hi Ben,
    Spry is an AJAX solution that allows the developer to update sections of a page instead of reloading the entire page when a selection is made from the select list. This particular post deals with a specific issue that arose when I was implementing it.

    Shortly after this post I actually gave up on Spry in favor of Rob Gonda’s AJAXCFC which I found to be a much cleaner and easier AJAX solution.


Comments are closed.