- jQuery UI Cookbook
- Adam Boduch
- 665字
- 2021-08-13 16:40:50
Remote autocomplete filtering
The autocomplete filtering capabilities aren't just limited to the default implementation, which searches for objects in array sources. We can specify a custom source()
function that will retrieve only data the user is looking for. This is the ideal approach if you're looking to use autocomplete on a data source with thousands of items. Otherwise, filtering gets too demanding on the browser—the large data set to download, followed by a large array search with each keystroke.
How to do it...
We'll use the GitHub API as the data source for our autocomplete widget. This is a good example since it is much too large to use in the browser memory.
$( function() { $( "#autocomplete" ).autocomplete({ minLength: 3, source: function( request, response ) { $.ajax({ url: "https://api.github.com/legacy/repos/search/:" + request.term, dataType: "jsonp", success: function( resp ) { var repositories = resp.data.repositories.splice( 0, 10 ); var items = $.map( repositories, function ( item ) { return { label: item.name + " (" + item.language + ")", value: item.name }; }); response( items ); } }); } }); });
Now if you look at the resulting widget in the browser and start typing, you'll see Github repository data in the drop-down menu.

How it works...
Since we're using a large data source, we're telling this particular autocomplete widget that the search for items should only be performed if there are at least three characters. This is done using the minLength
option. Otherwise, we would be asking the server to query based on one or two characters which isn't what we're after.
The source
option in our example specifies the data source that we're going to use – the Github API. The function we've passed to the source performs an $.ajax()
call against the Github API. We're using jsonp
as the format, which simply means that a callback function from the API will be sent back. We're also passing some query data to the API.
Our success callback function is executed once the API responds with data. We then pass this data through the $.map()
utility function in order to produce an array the autocomplete widget understands. Our success function does a simple $.map()
on the data to transform it into an array of objects that the autocomplete can use.
There's more...
We can further cut back on the cost of network communication overheads by introducing a term cache to the widget. A term cache, as the name suggests, would store locally the results of performing a remote filter operation. This way, when the user inevitably does the exact same thing with their keystrokes, we're not performing the exact same task with the remote API call since we've already cached the result in the widget.
( function( $, undefined ) { $.widget( "ab.autocomplete", $.ui.autocomplete, { _cache: {}, _search: function( value ) { var response = this._response(), cache = this._cache; this.pending++; this.element.addClass( "ui-autocomplete-loading" ); this.cancelSearch = false; if ( value in cache ) { response( cache[value] ); } else { this.source( { term: value }, response ); } } }); })( jQuery ); $( function() { $( "#autocomplete" ).autocomplete({ minLength: 3, source: function( request, response ) { var self = this; $.ajax({ url: "https://api.github.com/legacy/repos/search/:" + request.term, dataType: "jsonp", success: function( resp ) { var repositories = resp.data.repositories.splice( 0, 10 ); var items = $.map( repositories, function ( item ) { return { label: item.name + " (" + item.language + ")", value: item.name }; }); self._cache[request.term] = items; response( items ); } }); } }); });
You can see where we've made changes in the preceding code to support caching the items returned from the HTTP request. Now we're extending the widget to add the new _cache
attribute. We're also extending the _search()
function, which is in charge of checking for a cached value. If it finds one, the rendering response is called using the cached version of the data. The source()
function is responsible for storing cached results, but this is a simple one-liner.
- pcDuino開發(fā)實(shí)戰(zhàn)
- Linux Mint Essentials
- Puppet實(shí)戰(zhàn)
- Linux自動(dòng)化運(yùn)維:Shell與Ansible(微課版)
- 嵌入式系統(tǒng)原理及開發(fā)
- 計(jì)算機(jī)系統(tǒng):基于x86+Linux平臺(tái)
- Linux命令行大全(第2版)
- Kali Linux高級(jí)滲透測(cè)試
- Distributed Computing with Go
- 新編電腦辦公(Windows 10+ Office 2013版)從入門到精通
- Linux系統(tǒng)最佳實(shí)踐工具:命令行技術(shù)
- Windows 7使用詳解(修訂版)
- Windows Server 2008組網(wǎng)技術(shù)與實(shí)訓(xùn)(第3版)
- Docker容器技術(shù)與應(yīng)用
- Windows網(wǎng)絡(luò)編程(第2版)