1

In Drupal 7, I could achieve this using the following code.

$form['table'] = array(
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $options,
  '#empty' => t('No data')
);

This doesn't work anymore in Drupal 8. I just want a page similar to the one at /admin/people. What's the correct way in Drupal 8?

2
  • You can look at his they rendered the /admin/people page – Eyal Jan 10 '16 at 18:46
  • I think admin/people is a view, in Drupal 8. The OP is asking what is the code that they should use in Drupal 8, and that is equivalent for the Drupal 7 code shown in the question. – kiamlaluno Jan 10 '16 at 18:51
2

The form element type "tableselect" is available in Drupal 8

Description: A table created with a far left column of radios or checkboxes. Build the table headings and columns with the #headers property, and the rows with the #options property.

https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/8#tableselect

More info on "Adding checkboxes to a table" can be found in the handbook documentation here: https://www.drupal.org/node/945102

3
  • 1
    it doesn't work! i tried this:'#type' => 'tableselect', '#header' => $header, '#options' => $rows, '#multiple' => TRUE, '#empty' => $this->t('No data .'), ); but the result is empty – jiaxin Jan 11 '16 at 2:06
  • @benson, I suspect it will be an error in your code and not in the core API. Please check the above handbook link and verify your implementation is correct. – David Thomas Jan 11 '16 at 8:14
  • 1
    i did read the documentary carefully and tried it many times. Please note it worked perfectly in the Drupal 7. i spent many hours trying to find an example in the Drupal 8 source code and failed to find any module using the tableselect render. – jiaxin Jan 12 '16 at 9:20
0

You may be missing that the keys of header and respective keys of option should be same. Either both indexed or both associative. I face same issue having index key in header and associative key in option which gives empty table. https://www.drupal.org/node/945102 code will work

0

I spent many hours to figure this out and hope my code will help someone to save time. I am using Drupal 8 version I was working on some same sort of code where I wanted to output a listing of nodes after form submit and the listing should have checkbox in each row. Below is the sample code. Please modify as per your requirements.

$form['mytable'] = array(
    '#type' => 'table',
    '#title' => 'List of Nodes',
    '#header' => ["Checkbox","Title","Author"],
);


$nids = \Drupal::entityQuery('node')
        ->condition('status', 1)
        ->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$nids = array_keys($nodes);

// Add input fields in table cells.
$i=0;
foreach($nids as $nid) 
{

  $node = \Drupal\node\Entity\Node::load($nid);
  $contenttitle=$node->title->value;
  $receivername = $node->getOwner()->getDisplayName();
  \Drupal::entityManager()->getStorage('node')->resetCache(array($nid));

    $form['mytable'][$i][''] = array(
                                        '#type' => 'checkbox',
                                        '#return_value' => $nid,
                                    );

    $form['mytable'][$i]['Title'] = array(
                                        '#type' => 'label',
                                        '#title' => t($contenttitle),
                                    );
    $form['mytable'][$i]['Author'] = array(
                                        '#type' => 'label',
                                        '#title' => t($receivername),
                                    );
    $i++;
}
// Add a submit button that handles the submission of the form.
$form['actions']['submit'] = [
                                '#type' => 'submit',
                                '#value' => $this->t('Submit'),
                            ];
return $form;

below is the output example enter image description here

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.