如何使用Yii CGridView显示2行中的单个记录?

时间:2012-03-22 06:45:15

标签: yii frameworks

我在Yii中使用CGridView,如何在2行中显示单个记录?

基本上我想在第一行表格中显示记录详细信息,在其他行上显示我想要显示其摘要,我尝试使用div和css但是无法获得正确的结果,是否有人可以帮助我这个案子?

我这样使用:

<?php 

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bidding-grid',
'itemsCssClass' => 'data-default',
'dataProvider'=>$model,
'summaryText' => '',
'columns'=>array(
    'people_detail_for_bid.Person' => array(
                        'type'=>'raw',
                        'name'=>'people_detail_for_bid.Person',
                        'value'=>'Yii::app()->Controller->createUserNameLink($data->people_detail_for_bid->PeopleFirstName." ".$data->people_detail_for_bid->PeopleLastName, $data->people_detail_for_bid->PeopleId).
                        "<br><span class=decriptionText>".$data->people_detail_for_bid->PeopleDesignation."</span>".
                        "<br><span class=decriptionText>".$data->people_detail_for_bid->PeopleEmail."</span>"',
                        'htmlOptions'=>array('width'=>200),
    ),
    'timeAgo' => array(
                        'type'=>'raw',
                        'name'=>'timeAgo',
                        'value'=>'"<span class=decriptionText>".Yii::app()->Controller->_ago($data->PBPostedOn)."</sapn>"',
                        'htmlOptions'=>array('width'=>150),
    ),

),

)); 

?>

4 个答案:

答案 0 :(得分:5)

我认为最好和最干净的方法是创建一个新的扩展并将其扩展到CGridView,覆盖renderTableRow函数,如下所示:

/**
 * Renders a table body row.
 * @param integer $row the row number (zero-based).
 */
public function renderTableRow($row)
{
    if($this->rowCssClassExpression!==null)
    {
        $data=$this->dataProvider->data[$row];
        echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'">';
    }
    else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
        echo '<tr class="'.$this->rowCssClass[$row%$n].'">';
    else
        echo '<tr>';

    $colCtr = 0;
    foreach($this->columns as $column){
        $column->renderDataCell($row);
        $colCtr++;
    }

    echo "</tr>\n";
    echo "<tr><td colspan=\"$colCtr\">This is the summary row. </td></tr>";
}

答案 1 :(得分:1)

您可以自定义要在列中呈现的内容,因此,如果要在同一行中显示表的两个不同字段,则必须在模型中创建一个函数:

public function customColumn()
{
    return $this->PeopleDesignation.'<br/>'.$this->PeopleEmail;
}

然后将方法分配给列的值:

array(
    'type'=>'html',
    'value'=>'$data->customColumn()'
),

干杯,巴勃罗。

答案 2 :(得分:1)

经过太多的搜索......我尝试了不同的方式,现在终于得到了一个解决方案..这个解决方案基本上是一种第二种方式做任何事情而不是实际的方式..但它对我有用..

.....
.....

    'timeAgo' => array(
                        'type'=>'raw',
                        'name'=>'timeAgo',
                        'value'=>'"<span class=decriptionText>".Yii::app()->Controller->_ago($data->PBPostedOn)."</sapn></td></tr><tr><td colspan=\"6\"><span class=decriptionText>".$data->PBSummary."</span>"',
                        'htmlOptions'=>array('width'=>150),
    ),

.....
.....

在最后一列添加了一些表格标签以关闭行并添加另一个。

希望它适用于所有人..

谢谢..

答案 3 :(得分:0)

听起来像2个交错的CGridViews。所以你可以尝试一下,但我无法想象它会很好用:

将一个CGridView覆盖在另一个上,每行有足够的透明空间,另一行显示它的行,并且......

a. one `CGridView` table offset vertically by half a row height, *OR*
b. one table's row's text vertically top-aligned & the other bottom-aligned

但不知怎的,我怀疑这是CGridView能力;)并且保持两者同步所以它们的页面化和滚动在一起几乎是不可能的。听起来你需要增强CGridView或从CGridView派生一个新的小部件。我想知道JQuery是否有办法做到这一点?