CakePHP - 使用Html帮助器向表行添加属性

时间:2011-09-06 14:20:52

标签: cakephp

使用HtmlHelper构建表格时,有没有办法在所有<tr>行中添加ID等属性?

e.g。这是我当前<tbody>代码的简化版本:

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

我希望表格能像:

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

1 个答案:

答案 0 :(得分:2)

我意识到这是一个比我想象的更简单的案例。您只需将id属性添加到第二个和第三个参数(以便它适用于偶数行和奇数行)。

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
    );
}