查询数据自动完成(比如 free tags)或搜索内容自动完成(比如drupal api的自动搜索),这样的效果省时又省力!所以让我们来学会它并在项目中多用吧! 一、表单中定义 #autocomplete_path 属性 1 | $form [ 'user_select' ] = array ( |
2 | '#type' => 'textfield' , |
3 | '#autocomplete_path' => 'user/autocomplete' , |
二、定义一个#autocomplete_path 1 | $items [ 'user/autocomplete' ] = array ( |
2 | 'page callback' => 'user_autocomplete' , |
3 | 'type' => MENU_CALLBACK |
三、定义调用路径的回调函数 01 | function user_autocomplete( $string ){ |
04 | $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); |
05 | while ( $row = db_fetch_object( $result )) { |
06 | $matches [ $row ->title . ' [nid:' . $row ->nid . ']' ] = check_plain( $row ->title); |
09 | print drupal_to_js( $matches ); |
型动视觉摘自似水流云的博客,谢谢!
|