Kyle Hayes

Tech, Musings, Life

Spry: Empty Option in Dataset-controlled Select

| Comments

With Adobe Spry, you can dynamically populate a select box (aka. dropdown) with a dataset very easily:

`

var ds = new Spry.Data.JSONDataSet(“/some/jsonobject.js”);

{ds::name}

`

If you are familiar with Spry, than the above makes complete sense. I came across a problem this evening, however, when using very similar code as a filter mechanism for a separate dataset. The problem with the above code is that once it is populated, the select box will by default choose the first value as selected. If you are filtering data, you want the initial/reset state to be blank and not have any values.

Since I am using spry:repeatchildren to repeat my option tags, I cannot simply drop in a blank option tag above the current one because than it would also repeat for all the other rows in the dataset.

Thanks to the built in variables for each dataset, we can get special information during the loop of the dataset rows. This is valuable information since we technically need to drop the blank option just above the first row of the dataset.

We modify the above code to be the following instead:

`

var ds = new Spry.Data.JSONDataSet(“/some/jsonobject.js”);

{ds::name}

`

The spry:if simply checks if the loop is on row number 0 and if it is, it will drop in that option tag. Otherwise, it gets left out on all of the other rows.

Comments