Score:-2

Unable to create a custom form tableselect

ni flag
    /**
     * An example controller.
     */
    class GenerateCertificateTable extends ControllerBase {
    
      /**
       * Returns a render-able array for a test page.
       */
      public function content($nid) {
            $query = \Drupal::database()->select('users_field_data', 'u');
            $query->fields('u', ['uid','name','mail']);
            //For the pagination we need to extend the pagerselectextender and
            //limit in the query
            $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);
            $results = $pager->execute()->fetchAll();
            // dump($results);
            // Initialize an empty array
            $header = [
                'uid' => t('UID'),
                'mail' => t('mail'),
                'name' => t('Username'),
            ];
            $output = array();
            // Next, loop through the $results array
            foreach ($results as $result) {
             if ($result->uid != 0 && $result->uid != 1) {
                $options[$result->uid] = [
                    'uid' => $result->uid,
                    'mail' => $result->mail,
                    'name' => $result->name,
                ];
             }
           }
        
           $form['table'] = [
            '#type' => 'table',
            '#header' => $header,
            '#options' => $output,
               '#js_select' => TRUE,
              '#multiple' => TRUE,
              '#required' => TRUE,
            ];
            // Finally add the pager.
            $form['pager'] = array(
              '#type' => 'pager'
            );
       $build[] = $form;
return $build;
 }
}

I am trying to create table form using tableselect element but even after I get the data it shows empty data value.

Score:1
fr flag

You have used wrong array index key for rows output. It should be '#rows' => $output not '#options' => $output, .

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.