<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>newschuyl - Spry</title>
			<link>http://www.mischefamily.com/nathan/index.cfm</link>
			<description>a blog by nathan mische</description>
			<language>en-us</language>
			<pubDate>Mon, 06 Sep 2010 20:16:23-0400</pubDate>
			<lastBuildDate>Thu, 17 Jan 2008 16:57:00-0400</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>nmische@gmail.com</managingEditor>
			<webMaster>nmische@gmail.com</webMaster>
			
			<item>
				<title>Spry AutoSuggest With Multiple Parameters</title>
				<link>http://www.mischefamily.com/nathan/index.cfm/2008/1/17/Spry-AutoSuggest-With-Multiple-Parameters</link>
				<description>
				
				When loading datasets from the server for for the Spry AutoSugget widget, Spry passes the value of the search field as a parameter to the dataset URL. This works fine, but what if the suggestions you want to provide are based on a combination of form fields?

To give you a better of idea of what I mean, here is an example of a search form I was working on earlier today: &lt;i&gt;(Note: this form doesn&apos;t work.)&lt;/i&gt;

&lt;div style=&quot;border:1px solid #999999;margin:2px; padding:5px;background-color:#eeeeee&quot;&gt;

&lt;form&gt;
&lt;p&gt;
Find users within  
&lt;select name=&quot;radius&quot;&gt;
	&lt;option value=&quot;0&quot; selected&gt;Any Distance&lt;/option&gt;
	&lt;option value=&quot;1&quot;&gt;1 mile&lt;/option&gt;
	&lt;option value=&quot;2&quot;&gt;2 miles&lt;/option&gt;
	&lt;option value=&quot;3&quot;&gt;3 miles&lt;/option&gt;
	&lt;option value=&quot;4&quot;&gt;4 miles&lt;/option&gt;
	&lt;option value=&quot;5&quot;&gt;5 miles&lt;/option&gt;
	&lt;option value=&quot;10&quot;&gt;10 miles&lt;/option&gt;
&lt;/select&gt;
of the following 
&lt;select name=&quot;radiusCountry&quot; onChange=&quot;updateDsURL(this.form);&quot;&gt;
	&lt;option value=&quot;U&quot; selected&gt;U.S.&lt;/option&gt;
	&lt;option value=&quot;C&quot;&gt;Canadian&lt;/option&gt;
	&lt;option value=&quot;B&quot;&gt;U.S. or Canadian&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;radiusType&quot; onChange=&quot;updateDsURL(this.form);&quot;&gt;
	&lt;option value=&quot;City&quot;selected&gt;City&lt;/option&gt;
	&lt;option value=&quot;Zip&quot;&gt;Postal Code&lt;/option&gt;
&lt;/select&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;input type=&quot;text&quot; name=&quot;radiusValue&quot;/&gt;
&lt;/p&gt;
&lt;/form&gt;

&lt;/div&gt;

I wanted to provide suggestions for the search term, but I wanted to limit those suggestions to only the country (U.S. or Canada) and type (City or Postal Code) selected. So if a user selects U.S. Cities they should only get U.S cities as suggestions, no Canadian cities or postal codes of any kind. This turned out to be fairly easy but I though I&apos;d share my approach in case others are interested.

Basically what I needed to do is append multiple parameters to the dataset URL. To accomplish this I added a new function to the Spry AugoSuggest widget named addReplaceParams:

&lt;pre&gt;
&lt;code&gt;
  Spry.Widget.AutoSuggest.prototype.addReplaceParams = function(params)
  {
	
	var ds = this.dataset;
	ds.cancelLoadData();
	ds.useCache = false;
	
	this.setValue(&apos;&apos;);
	this.showSuggestions(false);
		
	var url = ds.url;
	
	for(var i=0; i &lt; params.length; i++) {	
		url = Spry.Widget.Utils.addReplaceParam(url, params[i].param, params[i].value);	
	}		
	
	ds.setURL(url);
	ds.loadData();
	
  };
&lt;/code&gt;
&lt;/pre&gt;
In a nutshell this function clears the search term, hides suggestions and updates the AutoSuggest&apos;s dataset URL using an array of parameters passed to the function.  The parameters are defined as simple JavaScript objects with two keys: param which is the name of the URL parameter, and value which is the value of the URL parameter. It does the actual parameter additon/replacement using the Spry.Widget.Utils.addReplaceParam() function. To update the URL using this function I wrote another function which I use as the onChange event handler for the country and type select elements.
   
&lt;pre&gt;
&lt;code&gt;
  function updateDsURL(f){
	
	var country = f.radiusCountry[f.radiusCountry.selectedIndex].value;
	var type = f.radiusType[f.radiusType.selectedIndex].value;
	
	var params = [];
	params[0] = {param:&apos;radiusCountry&apos;,value:country};
	params[1] = {param:&apos;radiusType&apos;,value:type};
	
	theSuggest.addReplaceParams(params);	
  };
&lt;/code&gt;
&lt;/pre&gt;

Putting it all together this is what it looks like:

&lt;pre&gt;
&lt;code&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot; src=&quot;includes/xpath.js&quot;&gt;&lt;/script&gt;
&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot; src=&quot;includes/SpryData.js&quot;&gt;&lt;/script&gt;
&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot; src=&quot;includes/SpryAutoSuggest.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;includes/SpryAutoSuggest.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;&gt;
  var ds1 = new Spry.Data.XMLDataSet(&quot;/suggestions.cfm&quot;,&quot;radiusvalues/radiusvalue&quot;);
  
  function updateDsURL(f){
	
	var country = f.cboRadiusCountry[f.cboRadiusCountry.selectedIndex].value;
	var type = f.cboRadiusType[f.cboRadiusType.selectedIndex].value;
	
	var params = [];
	params[0] = {param:&apos;radiusCountry&apos;,value:country};
	params[1] = {param:&apos;radiusType&apos;,value:type};
	
	theSuggest.addReplaceParams(params);	
  };


  Spry.Widget.AutoSuggest.prototype.addReplaceParams = function(params)
  {
	
	var ds = this.dataset;
	ds.cancelLoadData();
	ds.useCache = false;
	
	this.setValue(&apos;&apos;);
	this.showSuggestions(false);
		
	var url = ds.url;
	
	for(var i=0; i &lt; params.length; i++) {	
		url = Spry.Widget.Utils.addReplaceParam(url, params[i].param, params[i].value);	
	}		
	
	ds.setURL(url);
	ds.loadData();
	
  };
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form&gt;
&lt;p&gt;
Find candidates within  
&lt;select name=&quot;radius&quot;&gt;
	&lt;option value=&quot;0&quot; selected&gt;Any Distance&lt;/option&gt;
	&lt;option value=&quot;1&quot;&gt;1 mile&lt;/option&gt;
	&lt;option value=&quot;2&quot;&gt;2 miles&lt;/option&gt;
	&lt;option value=&quot;3&quot;&gt;3 miles&lt;/option&gt;
	&lt;option value=&quot;4&quot;&gt;4 miles&lt;/option&gt;
	&lt;option value=&quot;5&quot;&gt;5 miles&lt;/option&gt;
	&lt;option value=&quot;10&quot;&gt;10 miles&lt;/option&gt;
&lt;/select&gt;
of the following 
&lt;select name=&quot;radiusCountry&quot; onChange=&quot;updateDsURL(this.form);&quot;&gt;
	&lt;option value=&quot;U&quot; selected&gt;U.S.&lt;/option&gt;
	&lt;option value=&quot;C&quot;&gt;Canadian&lt;/option&gt;
	&lt;option value=&quot;B&quot;&gt;U.S. or Canadian&lt;/option&gt;
&lt;/select&gt;
&lt;select name=&quot;radiusType&quot; onChange=&quot;updateDsURL(this.form);&quot;&gt;
	&lt;option value=&quot;City&quot;selected&gt;City&lt;/option&gt;
	&lt;option value=&quot;Zip&quot;&gt;Postal Code&lt;/option&gt;
&lt;/select&gt;:
&lt;/p&gt;

&lt;div id=&quot;mySuggest&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;radiusValue&quot;/&gt;
  &lt;div id=&quot;resultsDIV&quot; spry:region=&quot;ds1&quot;&gt;
    &lt;ul&gt;
      &lt;li spry:repeat=&quot;ds1&quot; spry:suggest=&quot;{radiusvalue}&quot;&gt;{radiusvalue}&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	var theSuggest = new Spry.Widget.AutoSuggest(&quot;mySuggest&quot;,&quot;resultsDIV&quot;, &quot;ds1&quot;,&quot;name&quot;,{minCharsType:3, loadFromServer:true, urlParam:&apos;filter&apos;});
&lt;/script&gt;

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;/code&gt;
&lt;/pre&gt;

You may ask why two functions? Well, I thought this is something I may want to use again so I made the Spry.Widget.AutoSuggest.addReplaceParams() function fairly generic. For this example I included the function in the inline script block, but if I use it enough I&apos;ll probably end up putting it in my AutoSuggest.js file.

OK, that&apos;s &lt;a href=&quot;http://www.infoaccelerator.net/index.cfm?event=showEntry&amp;entryId=829F2B3C-FF30-C9A3-ADA302B9D82C64CD&quot; target=&quot;_blank&quot;&gt;one down&lt;/a&gt;... 
				</description>
				
				<category>Spry</category>				
				
				<category>JavaScript</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Thu, 17 Jan 2008 16:57:00-0400</pubDate>
				<guid>http://www.mischefamily.com/nathan/index.cfm/2008/1/17/Spry-AutoSuggest-With-Multiple-Parameters</guid>
				
			</item>
			
			<item>
				<title>Getting Spry&apos;s Auto Suggest Widget to Hover in IE</title>
				<link>http://www.mischefamily.com/nathan/index.cfm/2007/12/13/Getting-Spry-Auto-Suggest-to-Hover-in-IE</link>
				<description>
				
				Yesterday &lt;a href=&quot;http://www.danvega.org/blog/index.cfm/2007/12/12/Spry-Auto-Suggest-IE6-Bug&quot; target=&quot;_blank&quot;&gt;Dan Vega&lt;/a&gt; posted an entry about an issue he was seeing with Spry Auto Suggest widget in IE 6. Well it turns out I was investigating the Spry Auto Suggest widget just last week because I needed the cause of this bug. Let me explain.  [More]
				</description>
				
				<category>Spry</category>				
				
				<category>ColdFusion</category>				
				
				<pubDate>Thu, 13 Dec 2007 17:03:00-0400</pubDate>
				<guid>http://www.mischefamily.com/nathan/index.cfm/2007/12/13/Getting-Spry-Auto-Suggest-to-Hover-in-IE</guid>
				
			</item>
			</channel></rss>