Bug Tracker

Opened 14 years ago

Closed 12 years ago

#3097 closed enhancement (invalid)

[autocomplete] pass function as data source

Reported by: khoker Owned by:
Priority: major Milestone: 1.3
Component: plugin Version: 1.2.6
Keywords: autocomplete Cc:
Blocked by: Blocking:


Jorn (and all), great work on the autocomplete plugin. If you are accepting enhancements, I would love to see the ability to pass a function as a datasource. Right now autocomplete accepts data as an array of objects [{obj1},{obj2},{obj3},...] or a URL. (urlOrData). I think it would add useful functionality to be able to pass a function as well. That would make the autocomplete plugin a lot more dynamic with respect to the dataset to be searched. In addition, there's often the need to manage your own ajax calls outside of the jQuery universe, depending on how many active requests an application is trying to manage, so the ajax call might need to be passed to a function instead of manually done at the plugin.

Attachments (1)

autocomplete_fn.zip (23.5 KB) - added by mwinckler 14 years ago.
Modified jquery.autocomplete.js to accept function as datasource

Download all attachments as: .zip

Change History (3)

Changed 14 years ago by mwinckler

Attachment: autocomplete_fn.zip added

Modified jquery.autocomplete.js to accept function as datasource

comment:1 Changed 14 years ago by mwinckler

I agree. I needed this same capability, so I wrote some modifications to the plugin to do the job. I haven't tested it thoroughly yet, but it seems to be working with the default settings. I've attached a zip archive with my modifications and example usage. I made the changes to Revision: $Id: jquery.autocomplete.js 5747 2008-06-25 18:30:55Z joern.zaefferer $. Lines changed include: 17-21, 333-352 (inserted block), and 424 (after the insert).

You can pass the autocomplete function a function reference instead of a URL or array. The function reference should accept two parameters: the query term, and a callback to execute (this will be the "success" function in the autocompleter). The function then calls the callback, passing it the result set corresponding to the query. The result should by an array of objects, each of which has a 'value' property with the term to be displayed in the autocompleter, e.g.:


The objects could also have other properties for use in formatting, etc.

I just started playing around with the autocomplete plugin, so my code is probably not the best solution, but hopefully it's a starting point.

comment:2 Changed 12 years ago by dmethvin

Resolution: invalid
Status: newclosed

This is not a jQuery core bug. Please report plugin bugs to the plugin's author, or ask on the jQuery forums. jQuery UI bugs should be reported on the UI bug tracker, http://dev.jqueryui.com .

Note: See TracTickets for help on using tickets.