Yii grid

$this->widget(‘zii.widgets.grid.CGridView’, array(

 'dataProvider'=>$dataProvider,


 'columns'=>array(


     array(           


         'dataField' => 'announcment',


         //expression to output HTML as html (otherwise simple text with tags is shown)


         'dataExpression' => '$data->announcment',


     ),


 ),

));

sorry if this is already being discussed, but I cant seem dataField and dataExpression properties, has it being changed to name and value??

also if i wish to display a column which have HAS_MANY relation, how can i do that?

i’m looking for the same

I am not sure I understand your question. Properties "dataField" and "dataExpression" are CDataColumn class properties. This class represents one grid column.

Do you want to display many related records inside some grid cell, like comments for post? If this is what you want then, I think, you can write CGridColumn subclass. Or maybe it is better to use CListView where you can specify view file for model item.

ahh… I kinda don’t understand my own word after reading it again. But Im trying to state that dataField and dataExpression is not define in CDataColumn. And after drilling into the code, Im only able to find ‘name’ and 'value properties. So is it being standardlize to be the same as cdetailview?

The solution I use is by defining a new method in the model. eg. $data->newMethod($data->HasManyRelation());




function newMethod($datas) {

      foreach($datas as $data) {

          $returnVal .= $data->field;

      }

      return $returnVal

}



I thought it would be something as magnificient as is jqgrid. But having tried it out, it`s just a table renderer. Or am I missing osmething?

If you’re looking for the CRudColumn class, it’s been renamed to CButtonColumn (took me some time to figure that out, especially since the CRudColumn class is still mentioned in the api)

See: http://code.google.com/p/zii/source/detail?r=95

And as someone else already mentioned, dataField and dataExpression got renamed to name and value (also outdated in api)

See: http://code.google.com/p/zii/source/detail?r=86

Hi, I’m looking for some help with pagination using the Yii grid

I am using yii1.1rc1585

I want to limit the number of buttons but using ‘maxButtonCount’ gives me an error.

in controller (this works)


		$dataProvider=new CActiveDataProvider('Participant', array(

    'pagination'=>array(  

      // every property of CPagination can be configured here

        'pageSize'=>self::PAGE_SIZE),

                ));

    $this->render('list',array('dataProvider'=>$dataProvider));

	}

this doesn’t work


		$dataProvider=new CActiveDataProvider('Participant', array(

    'pagination'=>array(  

      // every property of CPagination can be configured here

        'pageSize'=>self::PAGE_SIZE,

        'maxButtonCount'=>6),

                ));



it gives this error Property "CPagination.maxButtonCount" is not defined

in the view


$this->widget('zii.widgets.grid.CGridView', array(

     'dataProvider'=>$dataProvider,

     'columns'=>array(

     array(            // display a column with "view", "update" and "delete" buttons

              'class'=>'CRudColumn',

          ),

     array( 'class'=>'CLinkColumn',

                        'label'=>'View',

                        'urlExpression'=>'Yii::app()->createUrl("participant/show",array("id"=>$data->primaryKey))'),

            'firstname',

            'lastname',

            'phone1'

     ),

 ));

How would I limit the number of page buttons like this example


$this->widget('CLinkPager',array('pages'=>20,'maxButtonCount'=>6));

If I change line 32 in CLinkPager.php to this


	public $maxButtonCount=6;

It works as desired, but of course this is not where I want to change it.

Thanks,

doodle

You need to configure the ‘pager’ property of CGridView.

perfect!

updated code for the benefit of others


$this->widget('zii.widgets.grid.CGridView', array(

     'dataProvider'=>$dataProvider,

     'pager'=>array('maxButtonCount'=>4), <----- added this line

     'columns'=>array(

     array(            // display a column with "view", "update" and "delete" buttons

              'class'=>'CRudColumn',

          ),

     array( 'class'=>'CLinkColumn',

                        'label'=>'View',

                        'urlExpression'=>'Yii::app()->createUrl("participant/show",array("id"=>$data->primaryKey))'),

            'firstname',

            'lastname',

            'phone1'

     ),

 ));

Thanks,

doodle

mmmm, but why this is not working??

normal generate this view (Admin manage)




Usuario |Usuario| Senha|  Grupo   | Nome|   

    97  | test  | *****|    1     |teste|  



controller:





         $dataProvider=new CActiveDataProvider('usuario', array(

	'pagination'=>array('pageSize'=>self::PAGE_SIZE,),

		      ));

		$this->render('admin',array(

			'dataProvider'=>$dataProvider,

  		));



view:





<?php $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$dataProvider,

    'pager'=>array('maxButtonCount'=>4),

	'columns'=>array(

		'usuario_id',

		'usuario',

		'senha',

		'grupo_id.grupo',

		'nome',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



relations:




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

				'grupo'=>array(self::BELONGS_TO, 'grupo','grupo_id'),);

	}



result:




Usuario |Usuario| Senha|  Grupo   | Nome|   

    97  | test  | *****|          |teste|  



expected:




Usuario |Usuario| Senha|  Grupo   | Nome|   

    97  | test  | *****|  Admin  |teste|  



For my app I have a table with Genders (should only be two I hope) but I have three because I am porting data from a large database and many entries don’t have the gender specified. So I have


CREATE TABLE IF NOT EXISTS `Gender` (

  `id` int(1) NOT NULL auto_increment,

  `name` char(25) NOT NULL,

  PRIMARY KEY  (id)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin  ;

-- entries

1=>'male',

2=>'female',

3=>'unspecified'



This is linked to my participants table with participant.gender_id --> gender.id

Participant model


	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'donations' => array(self::HAS_MANY, 'Donation', 'participant_id'),

			'eventParticipants' => array(self::HAS_MANY, 'EventParticipant', 'participant_id'),

			'events' => array(self::HAS_MANY, 'Events', 'contact_id'),

		 ------>'gender' => array(self::BELONGS_TO, 'Gender', 'gender_id'),

			'lodgingtype' => array(self::BELONGS_TO, 'LodgingType', 'lodgingtype_id'),

			'participantCreditCards' => array(self::HAS_MANY, 'ParticipantCreditCard', 'participant_id'),

			'participantFoodHealths' => array(self::HAS_MANY, 'ParticipantFoodHealth', 'participant_id'),

			'participantParticipantLevels' => array(self::HAS_MANY, 'ParticipantParticipantLevel', 'participant_id'),

			'participantPrograms' => array(self::HAS_MANY, 'ParticipantProgram', 'participant_id'),

		);

	}



Now in controller (no change from previous example)


		$dataProvider=new CActiveDataProvider('Participant', array(

    'pagination'=>array(  

      // every property of CPagination can be configured here

        'pageSize'=>self::PAGE_SIZE),

         ));

    $this->render('list',array('dataProvider'=>$dataProvider));

	}



Now in view


$this->widget('zii.widgets.grid.CGridView', array(

     'dataProvider'=>$dataProvider,

     'pager'=>array('maxButtonCount'=>5),

     'columns'=>array(

     array(            // display a column with "view", "update" and "delete" buttons

              'class'=>'CRudColumn',

          ),

     array( 'class'=>'CLinkColumn',

                        'label'=>'View',

                        'urlExpression'=>'Yii::app()->createUrl("participant/show",array("id"=>$data->primaryKey))'),

            'firstname',

            'lastname',

            'gender_id', <------- this shows the gender id#

            'phone1'

     ),

 ));



This now shows the gender id (1, 2, or 3 in my case)

To see the name of the gender (more useful) eg, Male, Female, unspecified.


$this->widget('zii.widgets.grid.CGridView', array(

     'dataProvider'=>$dataProvider,

     'pager'=>array('maxButtonCount'=>5),

     'columns'=>array(

     array(            // display a column with "view", "update" and "delete" buttons

              'class'=>'CRudColumn',

          ),

     array( 'class'=>'CLinkColumn',

                        'label'=>'View',

                        'urlExpression'=>'Yii::app()->createUrl("participant/show",array("id"=>$data->primaryKey))'),

            'firstname',

            'lastname',

            'gender.name', <------ now it shows the name of the gender

            'phone1'

     ),

 ));

This works!

doodle

now is working fine I have corrected the BELONG_TO!

but, how to show the name of the gender pointing to link gender/show/1

this is not working… :(




array( 

'class'=>'CLinkColumn',

'label'=>$data->$gender, 

'urlExpression'=>'Yii::app()->createUrl("gender/show/",array("id"=>$data->gender_id))'

),



thanks

I am trying this on ‘zii.widgets.grid.CGridView’




 <td><?php echo CHtml::encode($post->statusText); ?></td>

    <td><?php echo CHtml::link(CHtml::encode($post->title),

        array('show','id'=>$post->id)); ?></td>


********************

CHtml::encode($post->title), array('show','id'=>$post->id))


array( 

'class'=>'CLinkColumn',

'label'=>$data->$gender,

'urlExpression'=>'Yii::app()->createUrl("gender/show/",array("id"=>$data->gender_id))'

),




Hello,

how can i set the visibility of CGridView columns such as CRudColumn?

If a user is logged in as admin, he should be able to se the crudcolumn, guest users shouldn’t.

Something with


visible=EXPRESSION

would be fine.

@megabr - it doesn’t seem to accept a variable for the link lable, perhaps you can request this feature.

@kenci What about defining two widgets for two different user groups

example

if( an admin user ) {

define a widget with crud column;

} else {

define a widget for everyone else;

};

I think that this would work and be quite flexible, after all the amazing thing about the widgets is how few lines it takes to create something amaZinG!!

doodle

thanks for reply,

in this case I try use this:




<?php foreach($modelList as $n=>$model): ?>

<?php echo CHtml::encode($model->usuario_id); ?>

<?php endforeach;?>



regards.

hi all,

I am looking for a better implementation of the original sorting function. My concern is that the user has to scroll down to see my table and when he sorts one of the columns the page is refreshed and he has to scroll down again to see the new sorted table. My question is which of the new Grid extensions would best replace the original table sorting function. Probably I am trying to achieve sorting with ajax so the user can be kept at the current location of the screen no matter how many times he sorts the table. This is not for the admin section but for the storefront section of my website.

all suggestions are welcome.

thanks,

b

p.s. since you are discussing the Grid implementation in general I decided to use this thread for my question.

FYI if you are using the grid widget and a crud column

as of Yii1.1.0r1700

CRudColumn.php is now CButtonColumn.php

doodle

Hi,

I have a CButtonColumn with a button that has a ‘click’ attribute to run some javascript code. Does anyone know how to pass the current row data to the click function?




<?php 

   array(

      'class'=>'CButtonColumn',

      'buttons'=> array(

        'copy'=>array(

          'label'=>'Copy',

          'imageUrl'=>Functions::getImage("find.png"),

          'click'=>'

            function(){

              $("#searchCust").dialog("close");

              $("#Job_company_id").val("'.<-- I want the $row["company_id"] value here -->.'");

            }

            '

        )

      ),

      'template'=>'{copy}'

    ),

?>