jquery.suggest 1.2

A few people have contacted in the past about what I did to get IDs to work on Peter’s Script over at vulgarisoips.com (see the original post here).

Peter’s script works great; but for my needs I had to add an ID to the suggestion as a way to relate the selected value back to a dB entry.

First, I needed a way to tell the script where to add the results to in the DOM instead of having the script add the object automatically.  I created a new option called attachObject which allowed me to control the object in the DOM.  This allowed me to do this:

$('#name').suggest('/suggest/client',{dataContainer:'#cid', attachObject:'#sresults', onSelect: myFunction );

Note how above I use #sresults as my object to attach the suggestion results to.  In my edits if you leave out the attachObject it will still build the <ul> object for you without any issues.

Next, I needed a way to have the ID go along with the representation values (ex. a hash-like key, value relationship).  To do this I added two other options to the script: one called dataContainer and one called dataDelimiter which I set to a default value of tab (\t).  The dataContainer is an ID of an input box which holds the database ID of the selected object in the suggest box.  The dataDelimiter is the delimiter for the payload which contains the key values pairs separated by the dataDelimiter.  (Note: the “row” delimiter is still a new line character).

In my XHTML I have this:

<input type="hidden" name="cid" id="cid" value="-1"/>
<input type="text" id="name" size="30" maxlength="128"/>
<div id="suggestResults"></div>

(and I use the script stub above…)

On the server I have /suggest/client do something similar to this:

.
.
.
$stmt->execute();
foreach( $stmt->fetchAll(PDO::FETCH_OBJ) as $row ) {
echo "{$row->displayName}\t{$row->tmpid}\n";
}

NOTE: I do make an assumption your keys will always be unique; as I append a s_ to the key to construct the necessary <li> items (need to keep them unique for DOM reasons).

Here is the original version of Peter’s script: Original Version 1.1 (in text)

Here are my updates (let us call it v1.2):Updated Version 1.2 (in text)

In hindsight there are definitely better ways to do this but this solution is working great still (this idea and solution go back to August of 2007); and I will always suggest “working effectively in production” is always better than how it “should be.”

4 Comments so far »

  1. Pilipo said

    December 12 2008 @ 7:34 pm

    Is it possible to have this function populate multiple input fields at once instead of just the id and name? If so, when you have time could you illustrate with an example?

    Thanks

  2. Chris Schuld said

    December 22 2008 @ 12:04 pm

    @Pilipo:

    What type of fields do you want to populate? Are you inquiring about a drop down which then would populate an entire record (maybe name, address, email address, phone)?

  3. Neto said

    January 29 2009 @ 12:04 pm

    Hi Cris, is possible you send an example for me?
    Tanks
    From Brazil,
    Att
    Neto

  4. Murray said

    March 10 2009 @ 3:36 am

    Hi Chris,

    great article, but I have a lot of doubts

    could you write or send me a little example?

    thanks in advance

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

Email: (Required)

Website:

Comment: