Showing posts with label custom. Show all posts
Showing posts with label custom. Show all posts

Friday, September 17, 2010

Yii - data grid view with some customization of cell content format

Lets start by creating a simple table

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(128) NOT NULL,
  `activationKey` varchar(128) NOT NULL DEFAULT '',
  `createtime` int(10) NOT NULL DEFAULT '0',
  `lastvisit` int(10) NOT NULL DEFAULT '0',
  `superuser` int(1) NOT NULL DEFAULT '0',
  `status` int(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  KEY `status` (`status`),
  KEY `superuser` (`superuser`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Configure the database connection as shown in one of the earlier posts.

Generate the corresponding model for the table in your database by executing the following commands
>protected\yiic shell

>> model *

Now, I want to create a data grid view to show the details of the table.

To show a link to the new "users" page(with a data grid) on navigation bar, open the file ..\mytestapp\protected\views\layouts\main.php

and add the following entry, in the "mainmenu" div.


widget('zii.widgets.CMenu',array(
            'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'Users', 'url'=>array('/site/users')),  //####### new entry
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>



The site controller needs information about where to render the new "users" page.
This can be done by adding the following method in the ..\mytestapp\protected\controllers\SiteController.php file

public function actionUsers()
    {
    $dataProvider=new CActiveDataProvider('Users');
    $this->render('users',array('dataProvider'=>$dataProvider,
                ));
    }

As you would notice that these methods should begin with "action" prefix.

Create a new file "users.php" at this location
..\mytestapp\protected\views\site\users.php

The contents of this file would look something like this-


$this->pageTitle=Yii::app()->name . ' - Users';
$this->breadcrumbs=array(
    'Users',
);

?><h1>Users

beginWidget('CActiveForm', array(
    'id'=>'members-form',
    'enableAjaxValidation'=>true,
)); ?>

widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider,
    'columns'=>array(
   array(
    'name'=>'id',
    'value'=>'CHtml::encode($data->id)'
   ),
   array(
    'name'=>'username',
    'value'=>'CHtml::encode($data->username)'
   ),
   array(
    'name'=>'createtime',
    'value'=>'CHtml::encode(date(\'Y-m-d\', $data->createtime))'  //this will format the unix timestamp to a custom date format
   ),
   array(
     'name'=>'lastvisit',              
     'value'=>'CHtml::encode(date(\'Y-m-d\', $data->lastvisit))'      // format the cell with date format
   ),
   array(
     'name'=>'status',
     'value'=>'CHtml::encode($data->status==1 ? \'Active\': \'Inactive\' )' 
// to render the gridview cell with a particular value
    ),
    ),
)); ?>
endWidget(); ?>


The page should be hosted at
http://localhost/mytestapp/index.php/site/users
or
http://localhost/mytestapp/index.php/?r=site/users

depending on the url manager setting in the config file.