基本的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);
}
?>- Admin's blog
- 538 reads
Comments
Post new comment