Home > jQuery Google Suggest > JQuery Google Suggest (jquery.gsuggest.js)

JQuery Google Suggest (jquery.gsuggest.js)

December 9th, 2009

I was unable to find an jQuery autocomplete / jQuery autosuggest plugin available that met my exacting standards. While there are a good number of plugins available in the plugin repository, they are either too large, poorly written, or simply do not work as expected. In addition to this, the ones I reviewed lacked extensibility and a good default configuration.

I decided to write my own jQuery autosuggest plugin (GSUGGSET), and give back to the community.

Project Requirements

  • memic the behavior of Google Suggest
  • make implementation as easy as adding suggest=”yes” to an input:text
  • allow for simple integration of ajax or inline datasets
  • intelligent caching
  • light weight

Default Configuration

  • ‘debug’: false
  • ‘url’: ‘/suggest.cfc’
  • ’size’: 10
  • ‘cacheenabled’: true
  • ‘cacheaggressive’: false //false - when user press ESC cache set is deallocated, true - cache set exist for duration of session
  • ‘cacheforward’:false, //true - complete cache dataset, false - incomplete cache datasets
  • ‘casesensitive’: false
  • ’sort’: true
  • ‘css0′: {’position’:’absolute’, ‘z-index’:’1′, ‘display’:’none’}
  • ‘css’: {’background-color’:’#ffffff’, ‘border’:’solid 1px #000000′, ‘margin’:’0px’, ‘cursor’:’pointer’, ‘color’:’#000000′, ‘padding’:’0px’}
  • ‘highlight’: ‘#bcd5ff’

Usage Information

While I wrote this plugin to be as simple and intuitive to use, there are some things you should be aware of before using it.

  1. The dataset used by the plugin must be a JSON String Array.
  2. There are two ways to assign a dataset to the input:text:, Inline & AJAX
  3. The nosubmit attribute you see in the examples above prevents the form from submitting when enter/return is pressed. This overrides the default browser behavior, but it is up to you to decide if you want to use this.

————————————

A. Using an Inline Dataset

An inline dataset is one that does not make a round trip to any server to get a collection of data used for the suggest-box. To use this, simply add the parameter suggestvalue=”" to your input:text. Note, the suggest_value must be a JSON Array.

html: <input type="text" name="attorney_name" value="" size="100" suggest="yes" suggestvalue='["Micah","Shawn","Charles"]' nosubmit>

*The sugget_value does not need to be sorted, thats handled automatically.

B. Using an Ajax Dataset

An AJAX dataset is one that communicates with a server in the background to get a collection of data (when necessary thanks to caching) used for the suggest-box. To do so, you will need to pass the address of your ajax call (wether that be a cfm page, asp .net, php, whatever) to the URL Config Option.

jQuery(document).ready(function(){jQuery.gsuggest({'debug':true,'url':'myAjaxPage.aspx'})});

That page will receive two url parameters in its get request: value & method. value is the value of the input:text after keyup and should be used to execute a query and return a JSON String Array. method is an identifier to allow multiple queries to be executed inside a single ajax page. I prefer to have all of my suggest queries in one file, suggest.cfc, and you will see that reflected in the defaults.

html: <input type="text" name="attorney_name" value="" size="100" suggest="yes" nosubmit>

MIT License/GNU License

http://www.opensource.org/licenses/mit-license.php

http://www.gnu.org/licenses/gpl.html

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.

No related posts.

jQuery Google Suggest

  1. | #1

    Hi
    Thank you for sharing this code.
    I have one question… Can this be used in cases with 2 (or more) fields, each with its own ajax dataset?
    Rds.

  2. | #2

    Yes, absolutely.

  3. | #3

    @Micah Lacombe

    Just to be more clear: you can either embed multiple queries inside a single file and use the method parameter to determine which needs to be executed (as I do), or you can specify a different config url for each input:text and have your ajax code in separate files… how you decide to use it is up to you.

  4. | #4

    I just clicked your “SEE IT IN ACTION” link, and all that came up was binary data.

  5. | #5

    Hi Keith,

    Thanks for bringing this to my attention… I had a catastrophic hard drive failure in December and some files were lost. I rebuilt the sample “SEE IT IN ACTION” page this morning so you can check it out. Let me know if you have any questions!

    I would like to also add that if anyone has trouble getting the script to work, or understanding it, just make a comment here and I will follow up with specific details.

  6. Yugi
    | #6

    Hi,

    Can you write your gsuggest to support json format?

  7. | #7

    Hello - Can you give more details on your suggest.cfc file? I’m not familiar with ColdFusion, but I would like to add this functionality to my site using PHP. What exactly should the server side (CF, PHP, .NET, etc.) look like?

    Thanks for this work, it seems like it will be perfect for my application.

  8. | #8

    @Yugi

    Hi Yugi,

    I am not entirely sure what you mean… in fact it requires the dataset to be in JSON already. Can you explain more about what you are looking to do?

  9. | #9

    @Brian

    Brian,

    Don’t be scared off by the CFC. It is essentially the same thing as an object in any other language. In this particular case however it is a remote object, aka a Web Service. You can create your own Web Service in cf, php, .net, etc. That having been said you could also implement it without object remoting by doing an evaluate on the appropriate url parameters… the only requirement here is that the data is returned to gsuggest in the correct format.

    Here is the code for the CFC:

    http://www.micah-lacombe.com/utils/suggest_cfc_code.cfm

    Notice the comment at the bottom:


    <!---
    var method = obj.form.name + '_' + obj.name;
    var surl = $.gsuggest.config.url + "?method="+method+"&value="+objval+"&callback=?";
    --->

    This is where gsuggest sets up the url to use during the ajax call. As mentioned in the main article, [method] is the name of the method which gets executed, and [value] is the value currently in the text box. CF uses [method] as part of its remote call, however other languages may be different…. so you can update gsugggest.js to reflect whatever type of url you need and even add multiple language options by enhancing the configuration. Also, [callback] is something jquery uses (so that is also required).

    The data you return needs to be formatted in JSON:


    "#callback#(#serializejson(listtoarray(valuelist(qcustomers.contactname)))#)">

    This is outputing the value of [callback] as a function, hence the parenthesis. And the parameter value of that function is simply a JSON Simple Array. For further CFML Details, hit up google or ask me.

    Good luck and let me know how it goes!

  10. | #10

    @Brian

    I was thinking about this some more and realized I forgot to mention that you don’t really need to use a web service if you don’t want to. You can actually specify a separate file for each input by using a different config on each. If you take a look at the script you’ll see how to do this but if you get stuck or need a sample let me know.

    I prefer to consolidate everything in one place (aka one file) because it keeps it easier to manage… but that all depends on how your software is engineered.

  11. Jacek
    | #11

    Hi,
    I’ve a big trouble with your script. I cannot force it to work on my localhost (W7, Apache, PHP). I’ve done the following:
    1. I’ve copied the example (“SEE IT IN ACTION”) to may local machine and named it index.html
    2. I’ve also copied jquery-1.3.2.min.js and jquery.gsuggest.js and run the script with the ‘url’=http://www.micah-lacombe.com/utils/suggest.cfc and it’s OK in Firefox but not in IE8 and IE6 (XPMode)!
    3. I’ve place a call to suggest.cfc in FF:
    http://www.micah-lacombe.com/utils/suggest.cfc?method=gsuggest_customer&value=a&callback=?
    and it has returned:
    ?(["Alejandra Camino","Alexander Feuer","Ana Trujillo","Anabela Domingues","André Fonseca","Ann Devon","Annette Roulet","Antonio Moreno","Aria Cruz","Art Braunschweiger"])
    4. I’ve prepared local file ’suggest.php’ that contained the above string:

    5. It is not working! The string from suggest.php is always the same regardless of input:text value but even for ‘a’ it’s not working.

    Any suggestions?

    Regards,
    Jacek

  12. Jacek
    | #12

    My suggest.php file:
    echo ‘?(["Alejandra Camino","Alexander Feuer","Ana Trujillo","Anabela Domingues","André Fonseca","Ann Devon","Annette Roulet","Antonio Moreno","Aria Cruz","Art Braunschweiger"])’;

  13. | #13

    @Jacek

    Jacek,

    Your file will always contain the same content because you defined a static JSON set i.e. ‘?([”Alejandra Camino…

    Using my file as a remote call to test won’t work either. You need to have your class that returns the JSON on the same domain b/c gsuggest uses $.getJSON and not JSONP.

  14. Jacek
    | #14

    Hello,

    Your are right: I’ve defined static script in PHP that always returns the same content for testing purposes only - IMHO as long as I enter a single ‘a’ letter in input field it should work but it doesn’t. So that’s why I asked you about the output format of your suggest.cfc script. Properly formated JSON array is like: ['A...','A...','...'] but it’s not working in this case, too.
    So pls, tell me what the output format of AJAX data should be like?

    BTW calling your script from other domain works in FF 3.6 but not in IE8 - I understand it it’s security issue.

    Jacek

  15. | #15

    Your JSON Array is structured just fine, but you need to have the “?” replaced with the value in the URL parameter named “callback” aka. $_GET["callback"].

    As I said before, if you need cross domain support for your callback you can modify the code to use JSONP.

    Happy coding 8-)

  1. | #1