You are here

drupal #ahah属性(4)

 

在下面,还是这个模块,不过这次实现了进度条功能,而更新的频率为两秒一次(参看图10-19)。
 
警告 这个模块只是简单的说明了如何与进度条进行交互;在实际中,你应该报告的是实际任务完成的百分比。特别强调的一点是,你不要像示例中所给的那样,使用Drupal的持久化变量系统来存储和读取进度,这是因为多个用户同时运行表单时,这种方式就会出错。替代的方式是,你需要对数据库进行查询来获取已插入记录的百分比。
 
<?php
 
/**
 * Implementation of hook_menu().
 */
function poof_menu() {
    $items['poof'] = array(
        'title' => 'Ahah!',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('poof_form'),
        'access arguments' => array('access content'),
    );
    $items['poof/message_js'] = array(
        'page callback' => 'poof_message_js',
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
    );
    $items['poof/interval_js'] = array(
       'page callback' => 'poof_interval_js',
       'type' => MENU_CALLBACK,
       'access arguments' => array('access content'),
    );
    return $items;
}
 
/**
 * Form definition.
 */
function poof_form() {
    $form['target'] = array(
        '#type' => 'markup',
        '#prefix' => '<div id="target">',
        '#value' => t('Click the button below. I dare you.'),
        '#suffix' => '</div>',
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Click Me'),
        '#ahah' => array(
            'event' => 'click',
            'path' => 'poof/message_js',
            'wrapper' => 'target',
            'effect' => 'fade',
           'progress' => array(
              'type' => 'bar',
              'message' => t('One moment...'),
              'interval' => 2,
              'url' => 'poof/interval_js',
           ),
        )
    );
 
    return $form;
}
 
/**
 * Menu callback for AHAH additions.
 */
function poof_message_js() {
    $output = t('POOF!');
    for ($i = 0; $i < 100; $i = $i + 20) {
       // Record how far we are.
       variable_set('poof_percentage', $i);
       // Simulate performing a task by waiting 2 seconds.
       sleep(2);
    }
    drupal_json(array('status' => TRUE, 'data' => $output));
}
 
/**
 * Menu callback for AHAH progress bar intervals.
 */
function poof_interval_js() {
    // Read how far we are.
    $percentage = variable_get('poof_percentage', 0);
    // Return the value to the JavaScript progress bar.
    drupal_json(array('percentage' => $percentage));
}
 
10-19.进度条显示了完成的百分比。

老葛的Drupal培训班 Think in Drupal

Drupal版本:

评论