Shadman
Shadman

Reputation: 773

How can I display a single record in 2 rows by using Yii CGridView?

I am using CGridView in Yii, how can I display a single record in 2 lines?

Basically I want to show a record details in 1st row of table and on other row I want to display its summary, I tried it with div and css but can't get proper results, is anyone there who can help me in this case?

I am using like this:

<?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),
    ),

),

)); 

?>

Upvotes: 2

Views: 6063

Answers (4)

macinville
macinville

Reputation: 236

I think the best and cleanest way to accomplish this is to create a new extension and extend it to CGridView, override the renderTableRow function like this:

/**
 * 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>";
}

Upvotes: 5

Shadman
Shadman

Reputation: 773

After too much search .. and I tried with different ways now finally got a solution for this .. that solution is basically a kind of 2nd way to do anything not actual way .. but it works to me ..

.....
.....

    '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),
    ),

.....
.....

added some table tags on last column to close row and added another one.

hope it works for all ..

thanks ..

Upvotes: 1

hobs
hobs

Reputation: 19259

Sounds like 2 interleaved CGridViews. So you might try that, but I can't imagine it will work out well:

Overlay one CGridView over the other, with enough transparent space in each row for the other to display it's row, and ...

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

But somehow I doubt that's a CGridView capability ;) And keeping the two in sync so they pagenate and scroll together would be darn near impossible. Sounds like you need to enhance CGridView or derive a new widget from CGridView. I wonder if JQuery has something to do this?

Upvotes: 0

Puigcerber
Puigcerber

Reputation: 10104

You can customize what you are rendering in the columns, so if you want to show two different fields of your table in the same row, you have to create a function in your model:

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

And then assign the method to the value of your column:

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

Cheers, Pablo.

Upvotes: 1

Related Questions