- 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.
- Citrix XenApp Performance Essentials
- Red Hat Enterprise Linux 8系統管理實戰
- 嵌入式Linux系統開發:基于Yocto Project
- Installing and Configuring Windows 10:70-698 Exam Guide
- Moodle 3.x Teaching Techniques(Third Edition)
- Kubernetes從入門到實踐
- Docker+Kubernetes應用開發與快速上云
- Python基礎教程(第3版)
- Windows Server 2019 Administration Fundamentals
- STM32庫開發實戰指南:基于STM32F4
- 蘋果OS X Mavericks 10.9應用大全
- Introduction to R for Quantitative Finance
- Linux操作系統案例教程(第2版)
- Instant Responsive Web Design
- Apache ShardingSphere權威指南