Complex data in an auto-suggest

This is a question I get about once a month. Is it possible to use an autosuggest that returns complex data? What I mean is - I want to be able to start typing into a text field, see a list of matching names come up, but when I pick one, I need to know both the name value and the ID related to the name. So for example, imagine this data:

IDName
1Paris Hilton
2Darth Vader
3Victor Newman

If I type "v" and see Victor Newman pop up, when I select him I need to know that his ID was 3.

As far as I know this isn't possible with both Spry and ColdFusion 8's autosuggest. Both demand you return simple values to populate the suggestions.

You could do a lookup based on the name. So once you get the name, do another Ajax request to translate "Victor Newman" to 3. You can probably guess the problem with this. If there are two Victor Newmans, than what ID would you return? You could return names along with a middle initial, but that simply reduces the problem. It doesn't fix it. Depending on how complex you want to get though - you could do your Ajax request and when you see more than one result returned, prompt the user to pick the one they meant.

Has anyone else solved this problem? I'm thinking there has to be a better solution.

Comments

I've done this by bind a cfselect to an input field. So the user types in the input field and the values appear in the select. You can set the size of the select to 5 or 10 if you want people to see it.
# Posted By Sam Farmer | 5/27/08 8:51 AM
So I'm confused. Is your comment specifically talking about the issue of linking one name to N results with the same name?
# Posted By Raymond Camden | 5/27/08 9:07 AM
I've never used it with ColdFusion 8 or Spry, but normally I in my json or whatever feeds the values to the type suggest I will create lists as my values of some sort, wither by number of characters or delimiters: "23|John Doe". Thats pretty easy to decode in any server side language.
# Posted By Trent Richardson | 5/27/08 9:18 AM
The multiple "Victor Newman" problem may be a moot point. If two "Victor Newman"s show up in your auto-suggest list, how is your user supposed to differentiate between them? Seems like there's a more critical user interface problem that underlies that hypothetical duplicate label problem... ie, you shouldn't be showing duplicates in the list in the first place (assuming the list is an identity selection and not a search term selection).

Granted, the secondary lookup of the selected label is clunky, but it does seem like a valid workaround.
# Posted By Doug Keen | 5/27/08 9:41 AM
This should be pretty simple to do if you can capture or wire your own event that gets triggered after you do the select. Something like:
function afterSelect() {
// alert the value of the selected element
alert(document.getElementById(this.id).value));
}

The above has not been tested but it should give you an idea on how to implement.
# Posted By Boyan Kostadinov | 5/27/08 9:42 AM
@Ray:

This is how it would work:

<input type="text" name="lookup" id="lookup"><br />

<cfselect name="userID" id="userID"
bind="url:/index.cfm?
event=user.lookup={lookup@keyup}"
display="userName"
value="userID"
bindOnLoad="false" />

As the user types in the lookup field, it fires off a request to the server which returns a json-ed query to the select which puts the IDs and names in the right place. User then picks the correct user name and when submitting the form returns the ID.
# Posted By Sam Farmer | 5/27/08 9:46 AM
Ah so technically you aren't doing a traditional autosuggest at all.
# Posted By Raymond Camden | 5/27/08 9:52 AM
Yeah.

Is autosuggest old enough to have a traditional version?! Haha.
# Posted By Sam Farmer | 5/27/08 10:00 AM
This is easy with jQuery and Jorn's excellent Autocomplete plugin: (http://bassistance.de/jquery-plugins/jquery-plugin...)

I routinely use this and then use jQuery to populate a hidden field with the user ID.
# Posted By Jim Priest | 5/27/08 10:25 AM
Thanks for sharing, Jim.

Is there anything jQuery can't do? ;)
# Posted By Raymond Camden | 5/27/08 10:27 AM
It currently cant bring Coffee to my desk, but I've taken to calling my assistant Jquery to bridge that gap.
# Posted By Richard Dillman | 5/27/08 10:55 AM
I ran into this when autosuggesting on multiple fields and displaying multiple fields...for instance: auto suggest a POC at specific location... I let the user type in the location or the first or last name of the poc and it returns the POC, Company, and City and lastly ID into the autosuggest. I append ID: at the end of it, then I parse out the ID to run another ajax query and load the complete POC data into display with other contact info and pass the ID as a hidden field.
# Posted By Jim Ruzicka | 5/27/08 12:11 PM
This is possible with Spry. Display the autosuggest in a table:

<div id="autosuggest">
   <span id="SupplierName"><input type="text" name="SupplierName" id="SupplierName" /></span>
   <div id="SupplierMenu" spry:region="dsSuppliers">
      <table>
         <tr>
            <th>Supplier ID</th>
            <th>Supplier Name</th>
         </tr>
         <tr spry:repeat="dsSuppliers" spry:suggest="{SUPPLIERID}|{SUPPLIERNAME}">
            <td>{SUPPLIERID}</td>
            <td>{SUPPLIERNAME}</td>
         </tr>
      </table>
   </div>
</div>

Tweak the Spry.Widget.AutoSuggest.prototype.setValue function in SpryAutoSuggest.js file (Spry 1.6.1 line 178)to break the string into an array of strings and populate the form:

Spry.Widget.AutoSuggest.prototype.setValue = function(str)
{
   if (!this.textElement)
      return;
   this.textElement.value = str;
   var myArray = str.split(/\|/);
   document.form.SupplierID.value=myArray[0];
   this.textElement.value=myArray[1];
   this.showSuggestions(false);
};
# Posted By stitch | 5/27/08 3:59 PM
To be fair Stich - I don't think modifying Spry to make it work is the same as 'possible' with Spry. ;)
# Posted By Raymond Camden | 5/27/08 4:04 PM