Yii Framework Forum: Problem With CGridView - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem With CGridView Rate Topic: -----

#1 User is offline   vatsalpande 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-November 10

Posted 22 December 2010 - 11:51 AM

Hi All,
I am trying to use Zii widgets and I must say that using them saves a lot of effort .But I am stuck in a strange problem this time.
I have a custom function in my model that returns a string from my database on behalf of id.
I have successfully used it in CDetailView

<?php
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'strDivisionName',
array(
'label'=>$model->getAttributeLabel('intZone_Id'),
'value' =>$model->ZoneName,
)
),
'htmlOptions'=>array('class'=>'question-answer'),
)); ?>

and it is working fine
It shows the Zone Name in place of Zone id(This is the functionality which I need)


I am trying to do the same with CgridView

<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'division-grid',
'dataProvider'=>$model->search(),

'columns'=>array(
'strDivisionName',
array(
'name'=>$model->getAttributeLabel('intZone_Id'),
'type'=>'raw',
'value'=>$model->ZoneName
),
array(
'class'=>'CButtonColumn',
'template'=>'{view}{update}'
),
),
)); ?>

Here the label is coming up fine but value is not coming.It is coming as blank. I must be missing a very obvious point here.
Any help is highly appreciated...

Regards
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,710
  • Joined: 10-October 10
  • Location:Denmark

Posted 22 December 2010 - 12:09 PM

You need to put it in your_model::search.
And in the rules function, make sure it's set in the search scenario rule.
"Less noise - more signal"
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,834
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 22 December 2010 - 12:19 PM

This should work better
<?php
$this->widget('zii.widgets.grid.CGridView', array(
  'id'=>'division-grid',
  'dataProvider'=>$model->search(),

  'columns'=>array(
    'strDivisionName',
    array(
      'name'=>$model->getAttributeLabel('intZone_Id'),
      'type'=>'raw',
      'value'=>'$data->ZoneName',
    ),
  ),
));
?>

/Tommy
Don't forget to read The Definitive Guide to Yii (1.1/2.0 en) | The class reference (1.1/2.0) has the details
0

#4 User is offline   vatsalpande 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-November 10

Posted 22 December 2010 - 12:36 PM

Hi

Thanks a lot for paying attention to my problem.
If I use $data->ZoneName then its saying $data is undefined and as per my understanding it is giving correct error message as I havent defined anything as data.

This is the function I made in my model

public function getZoneName(){
$zoneId=$this->intZone_Id;
$zoneName ='';
if(trim($zoneId)!="" && trim($zoneId)!=NULL){
$zoneNameObj = Zone::model()->findbypk($zoneId,'',array('select'=>'strZoneName'));
$zoneName = $zoneNameObj->strZoneName;
}
return $zoneName;
}


I have following search rules set in my model

public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('intDivision_Id',$this->intDivision_Id);
$criteria->compare('strDivisionName',$this->strDivisionName,true);
$criteria->compare('intZone_Id',$this->intZone_Id,TRUE);

return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}

and in my controller actionadmin I have

function actionAdmin(){
$model=new Division('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Division']))
$model->attributes=$_GET['Division'];

$this->render('admin',array(
'model'=>$model,
));


I hope I am a little more descriptive right now. Can u please guide me where am I going wrong

Regards
0

#5 User is offline   vatsalpande 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-November 10

Posted 22 December 2010 - 12:48 PM

Thanks a Lot
It worked
I was missing single quotes '' in my value field
Thnx all for paying attention
:rolleyes:
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,834
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 22 December 2010 - 12:50 PM

View Postvatsak, on 22 December 2010 - 12:36 PM, said:

Hi

Thanks a lot for paying attention to my problem.
If I use $data->ZoneName then its saying $data is undefined and as per my understanding it is giving correct error message as I havent defined anything as data.
...


The $data variable exists locally when the CGridView rows are rendered (note the quotes around the 'value' value). I think you should include the zoneName property in a 'select' criteria when creating the dataprovider (not tested). See this example

(Please use [ code ] tags when providing code examples, for better readability. There's a '<>' button in the editor.)

/Tommy

Edit: Glad to hear you fixed it.

This post has been edited by tri: 22 December 2010 - 12:54 PM

Don't forget to read The Definitive Guide to Yii (1.1/2.0 en) | The class reference (1.1/2.0) has the details
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users