使用#ahah制作ajax联动菜单效果

-- Submitted by Admin on Thu, 05/13/2010 - 20:00

基本的ahah使用方法,可见上一篇文章 http://www.incollege.cn/content/biao-dan-shi-yong-ahahzeng-qiang-ajaxti-yan

一、表单中定义#ahah属性

$form['father'] = array(
  '#title' => 'father',
  '#type' => 'select',
  '#options' => $brand,//$father变量在前面定义好
  '#ahah' => array(
    'event' => 'click',//定义触发事件类型
    'method' => 'replace',//替换wrapper指向的容器里的内容,可以改成其它方式
    'path' => 'amenu',//ahah调用的菜单路径
    'wrapper' =>'child-wrapper',//返回的内容将以method定义的方式来改变指定容器内容
    'effect' => 'fade',//以渐现形式来显示返回的内容
    'progress'=>array(
      'type' => 'throbber',//会显示drupal默认的小时钟图标以表明是正在异步获取数据
    ),
  ),
);

二、定义一个#ahah要调用的路径

$items['amenu'] = array(
  'page callback' => 'get_child',
  'access callback' => TRUE,
  'type' => MENU_CALLBACK,
);

三、定义调用路径的回调函数

<?php
function get_child() {
 
$child_list_name = $_POST['family'];
 
//根据father的选项-$child_list_name,找到child,并将其赋给$child变量
 
*************************
 
$form['child'] = array(
   
'#type' => 'select',
   
'#options' => $child,
  );
 
$output = ahah_render($form,'child');
 
drupal_json(array('status' => TRUE, 'data' => $output));
}
//这个函数用来清理表单的缓存,替换原来的child表单
function ahah_render($fields, $name) {
 
$form_state = array('submitted' => FALSE);
 
$form_build_id = $_POST['form_build_id'];
 
$form = form_get_cache($form_build_id, $form_state);
 
$form[$name] = $fields;
 
form_set_cache($form_build_id, $form, $form_state);
 
$form += array(
   
'#post' => $_POST,
   
'#programmed' => FALSE,
  );
 
// Rebuild the form.
 
$form = form_builder($_POST['form_id'], $form, $form_state);

 
// Render the new output.
 
$new_form = $form[$name];
  return
drupal_render($new_form);
}
?>

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <img> <ebmed>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
3 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.