Prokop
Prokop

Reputation: 35

drupal ajax form change

I'm working on a drupal 7 module, where I wish to print out infos on a page (MENU_LOCAL_TASK node/%node/something), with ajax filters. I created a form and added 2 checkboxes, 1 is on default other is not. I want to show to the user the information according to wich checkbox is checked. 1 is on table row 1 is displayed, 2 is on table row 2 is displayed. If some of it is off, than that table row is off. Did I mentioned, that I want to solve it without submit and reload, only ajax. I added to the two 'checkbox'es the following 'ajax' => array('callback' => 'my_module_callback') . Here is the rest of the code, simplefied.

function my_module_callback($form, $form_state) {
    $data = array();
    $nid = 1;
    if ($form_state['values']['checkbox1']) { 
        $data += load_data($nid, "checkbox1");
    }
    if ($form_state['values']['checkbox1']) { 
        $data += load_data($nid, "checkbox2");
    }
    $commands[] = ajax_command_html("#here", my_module_table($data));
    return array('#type' => 'ajax', '#commands' => $commands);
}


function my_module_table($data){
    //do some stuff with the data in a foreach
    return theme("my_module_fancy_table",array("data" => $data));
}

function theme_my_module_fancy_table($data){ //registered with my_module_theme()
    // putting html into $output in a foreach
    return $output;
}

function my_module_page_callback_from_menu_function($nid){
    $output = drupal_render(drupal_get_form('my_module_custom_ajax_form'));
    $output .= "adding other stuffs including div#here";
    return $output;
}

First of all is this the 'good way' to do this, cause I kind of lost confident:) Second question, how to show the data on page load, rigth now one checkbox needs to be changed to see some infos.

Thanks and sorry for the short description :)

Upvotes: 0

Views: 1904

Answers (1)

2pha
2pha

Reputation: 10155

You should not really be doing the processing in the callback, it should be done in the form building function. The callback usually only returns the part of the form that has changed. Also, I don't think there is not need for setting commands[] in this case as returning part of the form will automatically replace the content set by 'wrapper'.

function my_module_form($form, $form_state){
  $data = array();
  $nid = 1;
  if ($form_state['values']['checkbox1']) { 
    $data += load_data($nid, "checkbox1");
  }
  if ($form_state['values']['checkbox2']) { 
    $data += load_data($nid, "checkbox2");
  }

  $form = array();
  $form['checkbox1'] = array(
    '#type' => 'checkbox',
    '#ajax' => array(
      'callback' => 'my_module_callback'
      'wrapper' => 'mydata',
      'event' => 'change',
    ),
  );
  $form['checkbox2'] = array(
    '#type' => 'checkbox',
    '#ajax' => array(
      'callback' => 'my_module_callback'
      'wrapper' => 'mydata',
      'event' => 'change',
    ),
  );
  $form['mydata'] = array(
    '#prefix' => '<div id="mydata">',
    '#suffix' => '</div>',
    '#markup' => my_module_table($data),
  );
  return $form;
}

function my_module_callback($form, $form_state){
  // $form_state['rebuild'] = true; may have to be set because the form has not been submitted and wont be rebuilt...I think, I cant remember for sure.
  return $form['mydata'];
}

To show the data on page load, you just have to change the logic of setting data in the form build function. Also, fyi, there is a stack site specifically for drupal at: http://drupal.stackexchange.com

Upvotes: 1

Related Questions