I have following code in my controller:
public function actionAdmin()
{
$model=new MForm('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['ChManageForm']))
$model->attributes=$_GET['ChManageForm'];
$this->render('admin',array(
'model'=>$model,
));
}
and
const member=1;
const district=2;
My view(called admin)
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ch-manage-form-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'form_name',
'region',
'phone_number',
'email',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
1 and 2 appears in the column called name(in my view file) and values of this column are saved in integer format. I need to show(in my view(admin)) member and district instead of numbers(e.g member instead of 1). How can I do it?
You can also specify the grid view column like this (doesn't require a helper in your model):
array(
'name'=>'form_name',
'value'=>'$data->form_name ? \'Member\':\'District\'',
'type'=>'text',
),
this works great if you only have 2 value in the variable.
Visit this URL to get more details.
http://www.yiiframework.com/forum/index.php/topic/14845-if-condition-inside-cgridview/
EDIT:
IF you have more than 2 values,
Model which you use to CGridView:
public function getValueText() {
return $this->getValueTextOptions[$this->form_name];
}
public function getValueTextOptions() {
return array(
1 => 'Member',
2 => 'District',
);
}
View with CGridView:
array(
'name'=>'form_name',
'value'=>'$data->getValueText()',
)
Related
I am trying to use filters in cGridview with cActiveDataProvider but it gives following exception
CActiveDataProvider and its behaviors do not have a method or closure named "getValidators"
Please check following code
Controller
public function actionAdmin()
{
$admin_type=Yii::app()->user->isAdmin;
if($admin_type==1)
{
$admin_batches=WebHelper::getAllAdminBatchesInArray(Yii::app()->user->getId());
if(!empty($admin_batches))
{
$batch_list=implode(",",$admin_batches);
}
$batch_criteria="group_id IN ( ".$batch_list." ) and status!=-1";
}
else
$batch_criteria="1 and status!=-1";
$model=new CActiveDataProvider('Users',array('criteria'=>array(
'condition'=>$batch_criteria,
)));
// print_r($model);die;
//$model->unsetAttributes(); // clear any default values
if(isset($_GET['Users']))
$model->attributes=$_GET['Users'];
$this->render('admin',array(
'model'=>$model,
));
}
View
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model,
'filter'=>$model,
'columns'=>array(
array(
'class'=>'CButtonColumn',
'htmlOptions'=>array('width'=>'70px'),
'buttons' => array(
'view' => array(
'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/view-image.png'
),
'update' => array(
'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/Edit-icon.png'
),
'delete' => array(
'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/Delete-icon.png'
)
)
),
first_name',
'last_name',
'user_name',
'group.group_title',
//'password',
'email_id',
array(
'type'=>'raw',
'name'=>'Status',
'value'=>'($data->status==0)?"<a href=\'\' id=\'$data->user_id\' class=\'user_status Active\'>Active</a>":"<a href=\'\' id=\'$data->user_id\' class=\'user_status Inactive\'>Inactive</a>"'
),
),));?>
I generated a new yii project and made a company table in my db, Then I generated a CRUD in gii for testing. This is the default structure for actionAdmin in every generated yii controller:
public function actionAdmin() {
$model = new Company('search');
$model->unsetAttributes(); // clear any default values
if (isset($_POST['Company']))
$model->attributes = $_POST['Company'];
$this->render('admin', array(
'model' => $model,
));
}
And in the default admin view for every generated CRUD, yii passes $model to the CGridView filter attribute. This means that, CGridView's filter accepts only Model's Object, but you pass instance of CActiveDataProvider to it. Suppose $model is a model object(For example, Users in your case), You should pass $model to filter of CGridView and pass $model->search() to dataProvider of CGridView. Then you can build your Criteria inside of $model->search() method. I recommend you to generate a sample CRUD in yii and see generated code.
Certain class has a property called status (that can be 0 or 1). In the corresponding model I have defined two variables STATUS_CLOSED = 1 and STATUS_OPEN = 2.
I am using a CDetailView to display model info inside a "View" view like:
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'account_number',
'account_type',
array(
'label'=>'Banco',
'type'=>'raw',
'value'=>CHtml::encode($model->bank->bank_name),
),
),
));
I have defined these two functions in my model:
public function statusLabels()
{
return array(
self::STATUS_CLOSED => 'Inactiva',
self::STATUS_OPEN => 'Activa',
);
}
public function getStatusLabel($status)
{
$labels = self::statusLabels();
if (isset($labels[$status])) {
return $labels[$status];
}
return $status;
}
I need to customize the CDetailView (possibly using these two functions) to display the corresponding label, depending on the status value.
I thought this would work:
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'account_number',
'account_type',
array(
'label'=>'Estado',
'type'=>'raw',
'value'=>$model->statusLabel($model->status),
),
),
));
But I get: Missing argument 1 for BankAccount::getStatusLabel()
What I am doing wrong?
Ok so first off you don't need to send in the status for a model since the model already knows its own status so I would change your function to this:
public function getStatusLabel() {
$labels = self::statusLabels();
if (isset($labels[$this->status])) {
return $labels[$this->status];
}
return $this->status;
}
So then your widget would just be like this:
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'account_number',
'account_type',
array(
'label'=>'Estado',
'type'=>'raw',
'value'=>$model->statusLabel
),
),
));
Also it doesn't cause an error but in reality you should make the function statusLabels() a static function.
public static function statusLabels() {
...
}
I have 2 tables/models:
Tmp1
Header
QuestionText
Tmp2
Header
Part
OutOf
I'm trying to display the columns: Header, QuestionText, Part, OutOf
in a single CGRIDVIEW.
In Tmp1 model:
public function relations()
{
return array(
'tmp2s' => array(self::HAS_MANY, 'Tmp2', 'Header'),
);
}
In Tmp2 Model:
public function relations()
{
return array(
'header' => array(self::BELONGS_TO, 'Tmp1', 'Header'),
);
}
Controller:
public function actionReviewAll()
{
$tmp1 = new Tmp1('search');
$tmp1->unsetAttributes();
if(isset($_GET['Tmp1'])){
$model->attributes=$_GET['Tmp1'];
}
$this->render('ReviewAll',array(
'tmp1'=>$tmp1,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
array(
'name' => 'tmp2.OutOf',
'value' => '$data->tmp2[0].OutOf',
'type' => 'raw'
),
),
)); ?>
Error:
Property "Tmp1.tmp2" is not defined.
Any help is greatly appreciated
You have a sintax error:
Tmp1.tmp2 = Tmp1.tmp2s
The relation alias is with 's'. "tmp2s".
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
'tmp2s.Part',
'tmp2s.OutOf',
),
)); ?>
But you are trying to show a 'self::HAS_MANY' relationship, you can't show that on a normal CGridView widget...
Your code probably works (I haven't tried it), but not in cases where your tmp1 model has no tmp2's. You must make sure there is a tmp2 before accessing it:
'value' => 'isset($data->tmp2) ? $data->tmp2[0].OutOf : Null',
You can also pass a function($data,$row) to value to make it look less messy. (Yes, that's important to me.)
I got 2 models: Project and User. In Project:
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(
"users"=>array(self::MANY_MANY, 'User',
'projects_users(project_id, user_id)'),
);
}
I want to list all users who are in actual project, $model contain actual project:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->users,
'columns'=>array(
'ID',
'username',
'displayname',
'firstname',
'lastname',
'email',
/*
'password',
'isAdmin',
*/
array(
'class'=>'CButtonColumn',
'template'=>'{delete}',
),
),
)); ?>
Unfortuletlly I get an error:
Fatal error: Call to a member function getData() on a non-object in /var/www/vhosts/aevers.com/editor/framework/zii/widgets/CBaseListView.php on line 107
That's because $model->users is not a CActiveDataProvider : it's an array of CActiveRecords.
Try to use a CArrayDataProvider instead :
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=> new CArrayDataProvider($model->users),
[...]
),
)); ?>
U can Group And display the results according to Projects and users in cgridView
Try This Thread Grouping members
i am trying to display two database tables source in one CGridView..
2 tables were reg.students and login.user..
my students model relation is,
public function relations()
{
Yii::app()->getModule('user');
return array(
'royaltyOutstandings' => array(self::HAS_MANY, 'RoyaltyOutstanding', 'studentID'),
'srkMedicalInfos' => array(self::HAS_MANY, 'SrkMedicalInfo', 'studentID'),
'parents' => array(self::HAS_ONE, 'SrkParents', 'studentID'),
'srkStudentWorksheets' => array(self::HAS_MANY, 'SrkStudentWorksheet', 'studentID'),
'user' => array(self::BELONGS_TO, 'User', 'centre_id'),
);
}
In user module,
public function tableName()
{ return 'login.user'; }
in cgridview columns array,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'students-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header' => 'No.',
'value' => '$row+1',
),
array('name' => 'user.centre_id',
'value'=>'$data->user->centre_id',
),
'... // & so on
Controller action is,
public function actionAdmin()
{
$this->layout = 'column3';
$form = new Reports ;
$model=new Students('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Students']))
$model->attributes=$_GET['Students'];
$this->render('admin',array(
'model'=>$model,
'form'=>$form,
));
}
If I understand you correctly, you want to show one or more relations in your gridview.
If this is what you want, have a look at this tutorial: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
This tutorial doesn't only show you how to show a relation in your gridview but it also shows you how to sort and filter the data. good luck!