BadHorsie
BadHorsie

Reputation: 14544

CakePHP - Add attribute to table rows using Html helper

When building a table using HtmlHelper, is there any way to add an attribute such as an ID to all <tr> rows?

e.g. This is a simplified version of my current <tbody> code:

foreach ($subjects as $subject) {               
    echo $this->Html->tableCells(
        array(
            $subject['Subject']['id'],
            $subject['Subject']['name']
        ),
        array('class' => 'odd'), null, true
    );
}

I want to have the table come out something like:

<tr id="subj-34"><td ...
<tr id="subj-263"><td ...
<tr id="subj-11"><td ...

Upvotes: 1

Views: 3178

Answers (1)

JJJ
JJJ

Reputation: 33163

Took me while to realize this was a simpler case than I first thought. You can just add the id attribute to the second and third parameters (so that it applies to both even and odd rows).

foreach ($subjects as $subject) {               
    echo $this->Html->tableCells(
        array(
            $subject['Subject']['id'],
            $subject['Subject']['name']
        ),
        array('class' => 'odd', 'id' = > 'subj-'.$subject['Subject']['id']), 
        array('id' = > 'subj-'.$subject['Subject']['id']), 
        true
    );
}

Upvotes: 3

Related Questions