1

I have a standard form with a table. I can add lines to the table via AJAX.
I also like to delete the lines using AJAX. But I don't know exactly where and how to add the buttons.

Here is my code to build the table inside the standard form building function:

  ...
  $aPersonsTable = array();
  foreach ($aPersons as $key => $value) {
    $row = array($value['dname'],'HERE IS THE PLACE WHERE THE BUTTON SHOULD SHOW UP');
    $aPersonsTable[]=$row;
  }

  $form['persons']['ptable'] = array(
    '#theme' => 'table',
    '#header' => $pheader,
    '#rows' => isset($aPersonsTable)?$aPersonsTable:null,
    '#disabled' => TRUE,
    '#title' => 'Users currently assigned',
    '#empty' => t('No users assigned yet!'),
    '#prefix' => '<div id="ptable_wrapper">',
    '#suffix' => '</div>',
  );
  ...

I'd like to have the button at 'HERE IS THE PLACE WHERE THE BUTTON SHOULD SHOW UP' :-)

I tried something like:

$row = array($value['dname'],array(
  '#type' => 'submit',
  '#value'=> 'Delete person',
  '#attributes' => array('id'=>$value['id']),
  '#ajax'=> array(
    'callback' => 'delete_person_ajax_callback',
  )
));

Any ideas? I googled and stackexchanged a lot, but did not find the right solution. Thx.

7
  • "I can add lines to the table via AJAX." - Cool, show us how you do it, and we'll be able to show you how to change the code to make remove lines instead.
    – Mołot
    Commented Jan 8, 2014 at 13:24
  • Hi! The problem is not the ajax callback code but where to put the button for it. The problem is, when adding lines to the table I only have one button with one defined callback function. But how to have multiple buttons in a table with a depending callback?
    – schulle877
    Commented Jan 8, 2014 at 13:27
  • where to put the button for it - anywhere you want, it does not matter as long as it stays outside the wrapper of AJAX-altered part. I only have one button with one defined callback function - no one forbids you to have two. You set #ajax property per button. I have a form with dozens AJAX buttons, 4 different callbacks, and it works.
    – Mołot
    Commented Jan 8, 2014 at 13:29
  • I think I found the solution by myself. I use the drupal_render() function to render the buttons. Here is the code: $form_button['button'] = array( '#id' => 'del_button', '#type' => 'submit', '#title' => 'DELETE', ); $deleteButton = drupal_render($form_button['button']); $row = array($value['dname'],$deleteButton);
    – schulle877
    Commented Jan 8, 2014 at 13:36
  • You can post solutions as self-answers :) Or is the timeout for this still in place?
    – Mołot
    Commented Jan 8, 2014 at 13:38

3 Answers 3

1

I found the answer using drupal_render(). The buttons are now generated in each row, havin their own ids, so that I can read the triggering element in the callback function. Now I only have the problem whe I click one of the buttons, the complete form is submitted. There seems to be no ajax callback. I will search further an will the edit the answer...

  foreach (aPersons as $key => $value) {
    $form_button['button'] = array(
      '#id' => 'del_'.$key,
      '#type' => 'button',
      '#value' => 'Delete',
      '#ajax' => array(
        'callback' => 'delete_person_ajax_callback',
      )
    );
    $deleteButton = drupal_render($form_button['button']);
    $row = array($value['dname'],$deleteButton);
    $aPersonsTable[]=$row;
  }
1
  • Tried it right now, but still the button does reload the whole page and not via ajaxing. Does your example still work? Drupal 7 btw.
    – kwoxer
    Commented Mar 22, 2016 at 15:08
1

I had the same issue with drupal 8.

The issue occured when several buttons/submits shared the same "#value". Drupal doesn't seem to handle the callbacks correctly in this case.

For me, the solution was to make each button's "#value" unique.

1
  • Your #value uniqueness sounded crazy, but I tried it and well... it IS crazy, but it's also true. However, it turns out you don't need to make the #value unique, you can assign a unique #name instead. See drupal.org/project/drupal/issues/1342066
    – jeff-h
    Commented Jul 26, 2018 at 2:42
0

I have fixed the above issues. Button element rendering through theme row data array not using a function like drupal_render(). Each row's theme table passes by reference. The respective row triggers the ajax callback function individually.

foreach ($Persons as $key => $value) {
    $form_button['button_'. $key] = array(
      '#id' => 'del_'.$key,
      '#type' => 'button',
      '#submit' => array('delete_person_button_submit'),
      '#value' => 'Delete',
      '#ajax' => array(
        'callback' => 'delete_person_ajax_callback',
      )
    );
    $row = array('data' => &$form_button['button_'. $key]);
    $aPersonsTable[] = $row;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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