Drupal D3模块添加Ajax

时间:2015-06-09 13:58:14

标签: php ajax drupal d3.js

我正在尝试在D3 Drupal 7模块中实现ajax,这是我如何做到的,但没有得到所需的结果,即

添加了一个菜单

function d3_form_menu() {    
  $items['d3_form'] = array(
    'title' => 'D3 form examples',
    'description' => 'Different visualization examples using d3.',
    'page callback' => 'xxxxxxx',
    'access callback' => TRUE,
  );

  $items['d3_form/simple_form'] = array(
    'title' => 'Simple Form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('d3_form_frm'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

**将要求饼图**

的用户输入值的表格
function d3_form_frm($form,&$form_submit) {

  $form['firstname'] = array(
    '#title' => t('Firstname'),
    '#type' => 'textfield',
    //'#required' => TRUE,
  );
  $form['lastname'] = array(
    '#title' => t('Lastname'),
    '#type' => 'textfield',
  );
  $form['submit'] = array(
    '#value' => t('Submit'),
    '#type' => 'submit',
    '#ajax'=>array(
        'callback' => 'd3_form_frm_ajax_submit',
        'wrapper' => 'd3-form-frm',
        'method' => 'replace',
        'effect' => 'fade',
        ),
    );

  return $form;
}


 Ajax callback function.

function d3_form_frm_ajax_submit($form, $form_state) {
  $pieArray=array();
  // Dummy/dumb validation for demo purpose.
  if (!empty($form_state['input']['firstname'])) {
      $pieArray[0]=$form_state['input']['firstname']*1;
      $pieArray[1]=$form_state['input']['lastname']*1;

    return d3_form_custom_pie($pieArray);

  }
  else {
    return 'Not input Received';
  }
}

这应该产生一个饼图,但它没有。它还会在单击后禁用提交按钮。但是,当我启用 Devel时,我注意到了 开发人员的各种块,页面和功能。图表出现。

function d3_form_custom_pie($pieArray) {
  $chart = array(
    'id' => 'visualization',
    'type' => 'PieChart',
    'rows' => array(
      array('Development', $pieArray[0]),
      array('Strategy', $pieArray[1]),
    ),
  );
  return d3_draw($chart);
}

0 个答案:

没有答案