使用 jQuery.UI 自动完成文本框
我正在尝试 jQuery.UI 库。使用这个库开发 Web 界面非常容易。我指的是使用“远程 JSONP 数据源”自动完成的文档和演示 这里
这是从远程源获取数据到自动完成文本框的完整代码。
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
我想知道以下代码段的确切功能是什么。
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
特别是
$.map( data.geonames, function( item )
提前致谢
I'm trying out jQuery.UI library.Developing web interfaces is very easy with this library.I was referring to the documentation and demo of using auto complete with "Remote JSONP Datasource" Here
this is the complete code for getting data from a remote source in to a autocomplete text box.
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
I want to know what is the exact functionality of the following code segment.
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
Specially of
$.map( data.geonames, function( item )
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一段有趣的代码。
该小部件需要一个对象数组,其中每个对象都有属性
label
和/或value
。该代码试图创建一个看起来像这样的数组。因此,调用
$.map
(这是一个非常有用的实用程序函数)将从 JSONP 调用检索到的对象数组转换为上述格式的对象数组。阅读此内容的一种方法
是:“通过获取
data.geonames
数组的每个元素并使用给定函数转换该元素来创建一个新数组”在这种情况下,该函数只是构建一个具有
label
和value
属性的对象,以便它可以与自动完成小部件一起使用。然后,该新数组立即作为参数传递给
response
函数。That is an interesting piece of code.
The widget expects an array of objects where each object has properties
label
and/orvalue
. What that code is attempting to create is an array that looks like that.So the call to
$.map
(which is a pretty useful utility function) is transforming the array of objects retrieved from the JSONP call into an array of objects in the format described above.One way to read this:
would be: "Create a new array by taking each element of the
data.geonames
array and transforming the element using the given function"In this case, the function just builds up an object with a
label
andvalue
property so that it will work with the autocomplete widget.Then, that new array is immediately passed as an argument to the
response
function.