0

This question already has an answer here:

Have a simple form including few of the input elements that should show up the data (based on the input) on tableselect (part of the same form) through the ajax callback functionality.

//submit button
    $form['action'] = [
    '#type' => 'button',
    '#value' => $this->t('Submit'),
    '#ajax' => [
    'callback' => '::setMessage',
    'prevent' => 'click',
    'wrapper' => 'scrollable',
    'method' => 'replace',
    'effect' => 'fade', 
    ],
    '#suffix' => '</div></div>',
    ];

and within my ajax callback function:

public function setMessage(array $form, FormStateInterface $form_state) {
     $options = array();

     $header = [
       'subject_id' => t('Subject ID'),
       'ct_availability' => t('CT Availability'),
       'sex' => t('Sex'),
       'race' => t('Race'),
       'cardiovascular_disease' => t('Cardiovascular Disease'),
       'registration_age' => t('Registration_Age'), 
       ];

    $options = $this->getOptions(); //getting the associative array     
    dpm($options);  //printing to make sure that the array is filled

//assigning to the table
    $form['table']['#header'] = $header;
    $form['table']['#options'] = $options;
    return $form['table'];
}

The header gets assigned fine and the table is displayed only with it. The data doesn't get filled and it seems the #options does not get assigned to the tableselect thus showing no data.

enter image description here

There are no ajax errors from the console

Wondering if it has something to do with 'processTableselect' function or rebuilding the form before returning the table element?

Or if it is a wrong approach to assign form element attributes within the callback function?

marked as duplicate by 4k4 8 Jun 15 '18 at 19:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $form['table'] = \Drupal\Core\Render\Element\Tableselect::processTableselect($form['table'], $form_state, $form); Adding this before the return solved this. Is this an appropriate solution or just a work around? Looking into what might be possible reasons behind – Abhi Jun 15 '18 at 18:58
  • You shouldn't build form elements in an ajax callback, see drupal.stackexchange.com/questions/225770/… – 4k4 Jun 15 '18 at 19:42
  • Sure. I was under impression that a form element can be defined outside the callback and the attributes can be set within the function. Here in my case I'm trying to filter a part of the form and #options for my table varies according to the filters from the form. Even though processTableselect option works fine, Now I wonder if its absolutely a wrong approach to use callback function for my use case of dynamically assigning #options to the table. I'll comprehensively take a look at the suggested answers from the link shared above in the comments. Thanks @4k4 – Abhi Jun 19 '18 at 16:52
  • Your workaround is great to show what is missing if form elements are modified in an ajax callback, the processing. But this involves normally more than one method/callback (which can be added to the form build dynamically too btw), so the workaround manually calling these methods would not be a practical solution and unnecessary, because there are better places to modify $form as shown in the linked topic. – 4k4 Jun 19 '18 at 18:24

Browse other questions tagged or ask your own question.