// don't show the current month in the list of values (for whatever reason)
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
3.
自动填充本地数据,格式化显示和结果
配置项复杂的autocomplete,看文档例子的第三段:
<p>
<label>E-Mail (local):</label>
<input type="text" id="email" />
<input type="button" value="Get Value" />
</p>
对应的js:
$("#email").autocomplete(emails, {
minChars: 0,
width: 310,
matchContains: "word",
autoFill: false,
formatItem: function(row, i, max) {
return i + "/" + max + ": \"" + row.name + "\" [" + row.to + "]";
},
formatMatch: function(row, i, max) {
return row.name + " " + row.to;
},
formatResult: function(row) {
return row.to;
}
});
首先来看emails的定义:
var emails = [
{ name: "Peter Pan", to: "peter@pan.de" },
{ name: "Molly", to: "molly@yahoo.com" },
{ name: "Forneria Marconi", to: "live@japan.jp" },
{ name: "Master <em>Sync</em>", to: "205bw@samsung.com" },
{ name: "Dr. <strong>Tech</strong> de Log", to: "g15@logitech.com" },
{ name: "Don Corleone", to: "don@vegas.com" },
{ name: "Mc Chick", to: "info@donalds.org" },
{ name: "Donnie Darko", to: "dd@timeshift.info" },
{ name: "Quake The Net", to: "webmaster@quakenet.org" },
{ name: "Dr. Write", to: "write@writable.com" }
];
emails的定义稍微复杂一些,可以看做数组的数组。
然后看配置选项:
1) 前2个选项和前面基本相同, matchContains的配置和前面不同,这里的配置为“word”,即为true,表示可以是包含关系。autoFill为false表示不自动把鼠标所在的行填充到输入框。
2) formatItem对返回结果的每一行做了处理。每一行都被处理成了这样的一种形式:i/max:”name”[to],例如第一个被表示成:1/10: "Peter Pan"[peter@pan.de]
3) formatMatch对每一行数据使用此函数格式化需要查询的数据格式. 返回值是给内部搜索算法使用的. 参数值和formatItem的参数一样。
4) formatResult和formatItem类似,但可以将将要输入到input文本框内的值进行格式化.同样有三个参数,和formatItem一样.Default: none,表示要么是只有数据,要么是使用formatItem提供的值.
4.
自动填充本地数据,多项选择,用分隔符分隔
<p>
<label>Tags (local):</label>
<input type="text" id='tags' />
</p>
对应js:
$("#tags").autocomplete(["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"], {
width: 320,
max: 4,
highlight: false,
multiple: true,
multipleSeparator: " ",
scroll: true,
scrollHeight: 300
});
配置选项很简单,就是多了一个多项选择和分隔符。
5.
自动填充本地数据,弹出层(thickbox
)
<p>
<a href="http://xianfengmc.blog.163.com/blog/#TB_inline?height=155&width=400&inlineId=modalWindow" class="thickbox">Click here for an autocomplete inside a thickbox window.</a> (this should work even if it is beyond the fold)
</p>
<div id="modalWindow" style="display: none;">
<p>
<label>E-Mail (local):</label>
<input type="text" id="thickboxEmail" />
<input type="button" value="Get Value" />
</p>
</div>
对应js:
$("#thickboxEmail").autocomplete(emails, {
minChars: 0,
width: 310,
matchContains: true,
highlightItem: false,
formatItem: function(row, i, max, term) {
return row.name.replace(new RegExp("(" + term + ")", "gi"), "<strong>$1</strong>") + "<br><span style='font-size: 80%;'>Email: <" + row.to + "></span>";
},
formatResult: function(row) {
return row.to;
}
});
当点击链接的时候弹出层,可以在弹出层中调用autocomplete。
(二)
远程数据6.
自动填充远程数据,配置简单
从第四段开始就是取远程数据了,看第四段的html:
<p>
<label>Single Bird (remote):</label>
<input type="text" id="singleBirdRemote" />
</p>
对应的js:
$("#singleBirdRemote").autocomplete("search.php", {
width: 260,
selectFirst: false
});
由于远程取的是php文件,所以我首先搭建了一个php环境,用的是Apache的http服务器,不会搭建的话随便百度一下。我是用的百度文库里的一片文章,如果你懒的话就用这个地址吧http://wenku.baidu.com/view/ddc083c24028915f804dc2ff.html。
首先让我们来看看search.php,里面都做了什么。
search.php文件的一开始:
<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
用$_GET()方法获取get参数q,如果q为空则直接返回。然后定义了一个键值对的数据:
$items = array(
"Great <em>Bittern</em>"=>"Botaurus stellaris",
"Little <em>Grebe</em>"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",...);
文件的最后:
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
echo "$key|$value\n";
}
}
?>
foreach循环数组里所有的元素,如果“键”里边包含所输入的值(查询的时候都把字母转化为了小写),则输出到页面。
其次,让我们分析一下js:
autocomplete函数第一个参数是一个url:search.php,刚才已经分析过search.php文件,search.php把删选出来的数据返回给autocomplete用作自动填充的选项。第二个参数是配置选项,这里只给出了两个选项:第一个width不必多说,第二个selectFirst: selectFirst (Boolean):如果设置成true,在用户键入tab或return键时autoComplete下拉列表的第一个值将被自动选择,尽管它没被手工选中(用键盘或鼠标).当然如果用户选中某个项目,那么就用用户选中的值. Default: true
7.
自动填充远程数据,关键字包含,多项选择
之所以放在一块儿看,是因为这两段比较象,只是看不同的配置:
<p>
<label>Single City (contains):</label>
<input type="text" id="suggest14" />
</p>
<p>
<label>Multiple Cities (local):</label>
<textarea id='suggest3' cols='40' rows='3'></textarea>
</p>
对应的js:
$("#suggest14").autocomplete(cities, {
matchContains: true,
minChars: 0
});
$("#suggest3").autocomplete(cities, {
multiple: true,
mustMatch: true,
autoFill: true
});
第六段的配置其实上边已经讲述,第七段多了一个配置选项multiple: multiple (Boolean): 是否允许输入多个值即多次使用autoComplete以输入多个值. Default: false.
8.
自动填充远程数据,用函数格式化显示和结果
<p>
<label>Multiple Birds (remote):</label>
<textarea id='suggest4'></textarea>
</p>
对应的js:
$("#suggest4").autocomplete('search.php', {
width: 300,
multiple: true,
matchContains: true,
formatItem: formatItem,
formatResult: formatResult
});
取远程数据search.php,不过把formatItem和formatResult分别指定了一个函数,这次我们需要看的只是这两个函数:
function formatItem(row) {
return row[0] + " (<strong>id: " + row[1] + "</strong>)";
}
function formatResult(row) {
return row[0].replace(/(<.+?>)/gi, '');
}
formatItem函数对每一行进行了格式化,formatResult函数去掉了其中的一些字符,如此而已。
9.
自动填充远程数据,列表中显示图片
<p>
<label>Image search (remote):</label>
<input type="text" id='imageSearch' />
</p>
对应的js:
$("#imageSearch").autocomplete("images.php", {
width: 320,
max: 4,
highlight: false,
scroll: true,
scrollHeight: 300,
formatItem: function(data, i, n, value) {
return "<img src='http://xianfengmc.blog.163.com/blog/images/" + value + "'/> " + value.split(".")[0];
},
formatResult: function(data, value) {
return value.split(".")[0];
}
});
这次autocomplete函数请求的时候一个远程数据images.php,来看看images.php做了什么:
<?php
$term = $_REQUEST['q'];
$images = array_slice(scandir("images"), 2);
foreach($images as $value) {
if( strpos(strtolower($value), $term) === 0 ) {
echo $value . "\n";
}
}
?>
首先利用$_REQUEST得到参数q,赋值给$item,然后扫描images目录,返回文件名数组,并分隔数组,然后去搜索文件名,如果包含输入的值则返回文件名。
formatItem函数首先根据查询到的文件名显示图片,然后输入文件名。
formatResult只是显示文件名。