查询数据自动完成(比如 free tags)或搜索内容自动完成(比如drupal api的自动搜索),这样的效果省时又省力!所以让我们来学会它并在项目中多用吧!
一、表单中定义 #autocomplete_path 属性
$form['user_select'] = array(
'#type' => 'textfield',
'#autocomplete_path' => 'user/autocomplete',
);二、定义一个#autocomplete_path
$items['user/autocomplete'] = array(
'page callback' => 'user_autocomplete',
'type' => MENU_CALLBACK
);三、定义调用路径的回调函数
<?php
function user_autocomplete($string){
$matches = array();
if ($string) {
$result = db_query_range("SELECT n.nid,n.title FROM {node} n WHERE n.title LIKE('%s%%') AND n.type='%s'", $string,'user', 0, 20);
while ($row = db_fetch_object($result)) {
$matches[$row->title .' [nid:'. $row->nid .']'] = check_plain($row->title);
}
}
print drupal_to_js($matches);
}
?>- Admin's blog
- 451 reads
Comments
Post new comment