0

I'm trying to add row ID's for a custom table I implemented via a custom module. But when I append an ID to the '#attributes' array it becomes the table ID, not the row ID. How can I define IDs for rows?

This is what I have so far:

$form['table'] = [
  '#type' => 'table',
  '#header' => $header_table,
  '#rows' => $rows,
  '#attributes'=> ['id' => 'IdforRow'],
  '#empty' => t('No Data found'),
  '#prefix' => '<b>Total Tenders : '.@$count.'</b><br>',
  '#suffix' => '',
];
4
  • If you want add ID for row, you have to add attributes at $rows not at table.
    – Jonh
    Jan 22, 2019 at 7:51
  • Can you try this code? $row = array('data' => 'some data'); $rows[] = array('data' => $row, 'class' => 'some class'); ); Or maybe the following one. $row = array('data' => 'some data'); $rows[] = array('data' => $row, 'class' => array('some class')); See also drupal.org/forum/support/post-installation/2016-04-21/….
    – vinhdv
    Jan 22, 2019 at 8:03
  • no its not working
    – Devya
    Jan 22, 2019 at 11:40
  • even i try this too foreach ($vars['rows'] as $key => $row) { $vars['rows'][$key] = array('data' => $row, 'class' => array('myclass')); }
    – Devya
    Jan 22, 2019 at 11:41

2 Answers 2

0

The row should be prepared as given below, to have id and class for each rows.

$rows[] = [
  'id' => $row_id,
  'class' => 'row-class',
  'data' => [
    $cell1_data,
    $cell2_data,
  ]
]
-2

Try to add the ID in an array format like below:

$form['table'] = [
    '#type' => 'table',
    '#header' =>  $header_table,
    '#rows' => $rows,
    '#attributes'=>[
      'id' => 'IdforRow'
    ],
    '#empty' => t('No Data found'),
    '#prefix' => '<b>Total Tenders : '.@$count.'</b><br>',
    '#suffix' => '',
];
2
  • ID never comes as array. OP has no problem with their table ID. OP wants row IDs.
    – leymannx
    Jan 22, 2019 at 7:50
  • its showing same as my question
    – Devya
    Jan 22, 2019 at 11:43

Your Answer

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

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