1

How can I hide the product using ajax callback . I am trying to implement that using ajax_commands_invoke but it does not work using below code

  $form['field_product_type_select']['und']['#ajax']= array('callback' => '_change_table_product_type','wrapper' => 'display-table-ajaxify');
 $header = array(
   'field_name' => t('Field Name'),
   'field_type' => t('Field Type'),
   'field_no_of_values' => t('Single/Multi Value'),
 );
 $options = array();

 foreach(commerce_product_types() as $product_type => $product_type_array){
   $field_info_instances = field_info_instances('commerce_product',$product_type);

   foreach($field_info_instances as $key => $value){
   $field_info_types = field_info_field_types($value['type']);
   $options[$key] = array(
     'field_name' => $value['label'],
     'field_type' => $field_info_types['label'],
     'field_no_of_values' => $value['cardinality'],
   );
  }

 $form['display_table'][$product_type] = array(
   '#type' => 'tableselect',
   '#options' => $options,
   '#header' => $header,
   '#multiple' => TRUE,
   '#prefix' => '<div class="display-'.$product_type.'" id="display-table-ajaxify" style="display:none;"><b>'.\
$product_type_array['name'].'</b>',
   '#suffix' => '</div>',
 );
 }

function _change_table_product_type(&$form,&$form_state){
  $commands = array();
  $product_type = $form_state['values']['field_product_type_select']['und'][0]['value'];
  drupal_set_message($product_type);
  $commands[] = ajax_command_invoke('display-'.$product_type,'show');


  /* $product_type = $form_state['values']['field_product_type_select']['und'][0]['value']; */
  /* foreach ($form['display_table'] as $key => $value){ */
  /*   if ($key != $product_type) { */
  /*     unset($form['display_table'][$key]); */
  /*   } */

  /* } */
  //  return $form['display_table'][$product_type];
  //  return array('#type' => 'ajax', '#commands' => $commands);
  print ajax_render($commands);
}
0

AJAX Commands are supposed to be returned in the callback function, not printed. Also, you have to give a CSS valid selector.

Try this:

function _change_table_product_type(&$form,&$form_state){
  $commands = array();
  $product_type = $form_state['values']['field_product_type_select']['und'][0]['value'];
  drupal_set_message($product_type);
  $commands[] = ajax_command_invoke('.display-' . $product_type, 'show');

  return array('#type' => 'ajax', '#commands' => $commands);

}

You can show the actual selector that code generates and test it in the JavaScript console. Just type:

 jQuery('CSS_SELECTOR').show();

If element is shown your CSS selector code is right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.