Score:0

How can I select multiple rows from a table and let the submission handler know which rows have been selected?

gb flag

In a custom module, I have a form showing a table with multiple rows, as in the screenshot.

screesnhot

Once users select one or more rows, and they click on the Delete button, rows in the database should be deleted.

I've built a tableselect table with type. I cannot understand how to get the selected table rows in the submission handler.

How can I achieve this?

The following is the code I am using to build the form.

foreach ($result as $row) {
  $data[] = [
    [
      'data' => [
        '#theme'      => 'image',
        '#alt'        => 'catImg',
        '#uri'        => File::load($row->Image)->getFileUri(),
        '#width'      => 100,
      ],
    ],
    $row->title,
    $row->mail,
    $row->created,
    t("<a href='editKitty/$row->id' class='db-table-button 
    db-table-button-edit use-ajax' data-dialog-type='modal'>Edit</a>"),
    t("<a href='delete-cat/$row->id' class='db-table-button
    db-table-button-edit use-ajax' data-dialog-type='modal'>Delete</a>"),
  ];
}

$header = [
  t('image'), t('Name'), t('Email'), t('Created'),
  t('Edit'), t('Delete'),
];

$build['table'] = [
  '#type' => 'tableselect',
  '#header' => $header,
  '#options' => $data,
];

$build['submit'] = [
  '#type' => 'submit',
  '#name' => 'submit',
  '#value' => $this->t('Delete'),
];
Roma Roma avatar
gb flag
i`ve found an interesting solution here. https://drupal.stackexchange.com/questions/163527/tableselect-checkboxes/164550
sonfd avatar
in flag
Does this answer your question? [tableselect checkboxes](https://drupal.stackexchange.com/questions/163527/tableselect-checkboxes)
Kevin avatar
in flag
Any reason why Views Buik Operations or a normal View would not be used here vs custom code?
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.