AutoSuggest, Get Data Object from an external file

John Kim's Avatar

John Kim

04 Nov, 2010 07:17 PM

On the official AutuSuggest page, Drew says:

 Below is an example using a URL to gather the Data Object and other various options:
 $("input[type=text]").autoSuggest("http://mysite.com/path/to/script", {minChars: 2, matchCase: true});

I want to get the data (list of value-name for the autosuggest) from some other file, but I'm not sure how to do this.

For example, on the script below, if I wanted to move the var data list to another file, what should I do?

My intuition tells me that making a list.php and copy-pasting the whole list would not help.

  1. 1 Posted by John Kim on 04 Nov, 2010 07:20 PM

    John Kim's Avatar

    Ooops. The example script got deleted. Here it is.

    var data = {items: [ {value: "21", name: "Mick Jagger"}, {value: "43", name: "Johnny Storm"}, {value: "46", name: "Richard Hatch"}, {value: "54", name: "Kelly Slater"}, {value: "55", name: "Rudy Hamilton"}, {value: "79", name: "Michael Jordan"} ]}; $("input[type=text]").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name"});

  2. 2 Posted by Cameron Tinker on 20 Dec, 2010 04:52 AM

    Cameron Tinker's Avatar

    Hi John,

    What you need to do is simply put your JSON object in another file and reference it like this:
    $("input[type=text]").autoSuggest("data.js", {selectedItemProp: "name", searchObjProps: "name"});

    The path you reference is relative to where your file is to be stored on your server. You can name the file anything you like. I just suggested data.js as an example.

    Your JSON object would look similar to this:
    [ {value: "21", name: "Mick Jagger"}, {value: "43", name: "Johnny Storm"}, {value: "46", name: "Richard Hatch"}, {value: "54", name: "Kelly Slater"}, {value: "55", name: "Rudy Hamilton"}, {value: "79", name: "Michael Jordan"} ]

    Also, if you want, you can actually reference a script that returns a JSON object. The way to reference the object is the same as before. The AutoComplete plugin passes a query string of 'q' to the script but you can specify more parameters if needed.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac