0

In my drupal 7 module which is separate from the node portion of drupal (discrete tables) I'm trying to use the array of 'attributes' to one element of table using theme().

Here's a simple example:

$query = "SELECT projectID, title, housestaff_needed FROM Projects";
$result = db_query($query)->fetchAll();
$header = array('name' => t('Name'), 'title' => t('Title'), 'housestaff_needed' => t('Housestaff Needed'));

    foreach ($result as $item) {
      $rows[] = array(
          $item->title, 
          $item->housestaff_needed, 
          array('attributes' => array('id' => array('staff-' . $item->projectID))),
          );
    }
    $theme = theme('table', array('header' => $header, 'rows' => $rows));
    return $theme;

As you know, this only adds another element after the housestaff_needed data. I've used this syntax before to enclose an entire table with an attribute, but I've read and read to see how this can be done on just an element but all I've read is how you can do it in the form api using '#attribute' which doesn't seem to be the same thing and a different setup outside the forms.

I'm trying to get this so I can identify this element for a jQuery scripted and can't get to first base. Please point out what I'm not doing correctly, thx, sam

2

I believe you need to add the data as an array to an associative array with the 'data' key, and the attributes with the attribute name as the key. For instance, your code would look like:

$rows = array();
foreach ($result as $item) {
  $rows[] = array(
    'data' => array($item->title, $item->housestaff_needed),
    'id' => 'staff-' . $item->projectID,
  );
}
$theme = theme('table', array('header' => $header, 'rows' => $rows));
return $theme;

I think this will add an id to each <tr>. If you want to add attributes to each individual <td>, you would use a similar structure for $item->title and $item->housestaff_needed. That is, it would become:

$rows = array();
foreach ($result as $item) {
  $rows[] = array(
    'data' => array(
      array(
        'data' => $item->title,
        'id' => 'SOME_ID',
      ),
      array(
        'data' => $item->housestaff_needed,
        'id' => 'SOME_OTHER_ID'
      ),
    ),
    'id' => 'staff-' . $item->projectID,
  );
}
$theme = theme('table', array('header' => $header, 'rows' => $rows));
return $theme;

At least, that's how it works in d6, I beleive, so here's hoping it still works in d7.

0

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.