I have the code below in view.php of a model:
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'cat_id',
'price',
array(
'name' => 'create_date',
'header' => 'Create Date',
'value' => 'Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->create_date)'
),
array(
'name' => 'update_date',
'header' => 'update Date',
'value' => 'Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->update_date)'
),
array(
'name' => 'last_visit_date',
'header' => 'Last visit Date',
'value' => 'Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->last_visit_date)'
),
'hit',
'update_count',
'status',
'sold',
'active',
),
)); ?>
but I see
Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $data->last_visit_date)
instead of converted date. I use this in admin.php and it is ok but in view.php it is not.
Delete quotes, use this:
'value' => Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $model->last_visit_date)
You need to delete the quotes, because the CDetailView can only show one record. I think you also need to change $data to $model like this:
'value' => Yii::app()->dateFormatter->format("yyyy-MM-dd HH:mm:ss", $model->create_date)
If you want to show multiple records, you can use CListView
Note:
In the CDetailView you're using a CModel as data and in the CListView you're using a dataProvider
Related
I am using yii1 and facing problem in adding class to image shown in detail view , tell me how to add css class or static property to image to make width and height 40px;
my detail view code is below
<?php
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
'id',
'title',
array(
'label' => 'image',
'type' => 'image',
'value' => Yii::app()->baseUrl . '/images/foodcategory/'
.$model->image ,
),
'status',
'created_at',
'created_by',
'modified_at',
'modified_by',
),
));
?>
Change the type to raw. You can then add it using CHtml::image():
array(
'label' => 'image',
'type' => 'raw',
'value' => CHtml::image(
Yii::app()->baseUrl . '/images/foodcategory/'.$model->image,
'Alt text',
array('class' => 'myclass') //and other html tag attributes
),
),
[
'attribute'=>'image_url',
'type' => 'image',
'value'=> \Yii::$app->request->BaseUrl.'/'.$model->image_url,
'format'=> ['image',['width'=>'100','height'=>'100']],
],
I print some data using foreign key, but I don't how it was converted to array in my view page.
echo ucfirst(User::model()->findByPk($model->postby_id)->username);
$this->widget('bootstrap.widgets.TbDetailView', array(
'data' => $model,
'attributes' => array(`enter code here`
'id',
'title',
'question',
'detail',
//'postby_id',
array(
'name' => 'Post By',
'value' => ucfirst(User::model()->findByPk($model->postby_id)->username),
),
'verified',
'verifiedby_id',
'post_date',
'hits',
'status',
),
));
Try this:
array(
'name' => 'Post By',
'value' => echo ucfirst(User::model()->findByPk($model->postby_id)->username),
),
Or:
array(
'name' => 'Post By',
'value' => function(){
echo ucfirst(User::model()->findByPk($model->postby_id)->username)
},
),
Update:
From documentation, "value" can also be an anonymous function whose return value will be used as a value. The signature of the function should be function($data) where data refers to the data property of the detail view widget.. I think below solution can help you:
First define a function in your model:
public function getUsername($postby_id)
{
return ucfirst(User::model()->findByPk($postby_id)->username);
}
Then you can have this in your view:
array(
'name' => 'Post By',
'value' =>$model->getUsername($model->postby_id)),
),
I cant list data in grid using yii framework.My controller is Sitecontroller.php,My view is list_jobseeker.php .I got the error "Fatal error: Call to a member function getData() on a non-object ".Anybody help me?
My controller code
public function actionlist_jobseeker()
{
$session_id=Yii::app()->session['user_id'];
if ($session_id == "")
{
$this->redirect( array('/employee/site/login'));
}
$user_id =$session_id;
$models = Yii::app()->db->createCommand()
->select('*')
->from('job_seeker_profile s')
->join('job_profile j','s.user_id = j.user_id')
->order('s.id')
->queryAll();
$this->render('list_jobseeker',array('models' =>$models));
}
View- list_jobseeker.php
<h1>View Jobseeker</h1>
<div class="flash-success">
</div>
<div class="form">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'rates-phase-grid',
'htmlOptions' => array('class' => 'table table-striped table-bordered table-hover'),
'dataProvider'=>new CArrayDataProvider($models),
'columns' => array(
array(
'name' => 'Name',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->name)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
),
array(
'name' => 'Email',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->email)',
'htmlOptions' => array('style'=>'width:250px;','class'=>'zzz')
),
array(
'name' => 'Password',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->password)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz')
),
array(
'name' => 'Contact No',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->contact_no)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'name' => 'Gender',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->gender)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'class' =>'CButtonColumn',
'deleteConfirmation'=>'Are you sure you want to delte this item?',
'template'=>'{update}{delete}',
'buttons' =>array('update'=>array(
'label'=>'edit',
'url'=>'Yii::app()->controller->createUrl("UpdateJob",array("id"=>$data["id"]))',
),
'delete'=>array('label'=>'delete',
'url'=>'Yii::app()->controller->createUrl("DeleteJob",array("id"=>$data["id"]))'),
)
)
),
));
?>
dataProvider should be a CDataProvider instance. You are passing in an array: $model. You could wrap this in a CArrayDataProvider:
'dataProvider' => new CArrayDataProvider($model),
A couple of other issues:
a) Your gridview expects $data to be an object and not an array. Either alter all $data->* instances to $data[*] or use CActiveDataProvider and the CActiveRecord instance for the job_seeker_profile table
b) By convention $model usually refers to a single CModel instance. Your array should therefore be named something else in plural e.g $items
I use this:
$data = ...::model()->findAll();
$dataProvider=new CArrayDataProvider('Class of your object');
$dataProvider->setData($data);
$dataProvider->keyField = "primary key of your model";
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
...
array( 'class'=>'CButtonColumn',
'template' => '{ver}',
'buttons' => array(
'ver' => array(
'url'=>'"?id=".$data["..."]',
'imageUrl'=> Yii::app()->baseUrl.'/images/view.png', //Image URL of the button.
),
)
),
<?php
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'htmlOptions' => array('style' => 'width:425px; float:left; margin-right:20px; word-break:break-all', 'class' => 'detail-view table table-striped table-condensed'),
'attributes' => array(
'id',
'name',
'street',
'housenumber',
'zipcode',
'city',
array(
'label' => Yii::t('api', 'Country'),
'name' => Yii::t('api', 'country.name'),
),
),
));
?>
I want to translate the values for the attributes label and name in the array for the columns.
But it only translate the label and not the name.
Can someone tell me, what I am doing wrong?
name is the column name in the database, I think you want to adjust value, try this:
'label' => Yii::t('api', 'Country'),
'name' => 'country.name', //in reality you don't need this since you are setting the value
'value' => Yii::t('api', $model->country->name),
I am stuck in a problem in CGridView yii, my refund field show 0/1 but I want to show "Yes" if 0 and "No" if 1, without using any second table.
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'transaction-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'member_id',
array(
'header' => 'MemberName',
'name' => 'member_id',
'value' => '$data->member->f_name'
),
'refund',
'band_id',
array(
'class'=>'CButtonColumn',
'template'=>'{view}',
),
),
));
Both of the other answers will work, but the cleanest way to do it is:
'columns'=>array(
'id',
'member_id',
...
'refund:boolean',
),
There are a bunch of CGridView column data types that are auto-used if you use colons like above. More info here: https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md
array(
'name' => 'refund',
'header' => "Refund",
'value' => '$data->refund?Yii::t(\'app\',\'Yes\'):Yii::t(\'app\', \'No\')',
'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes')),
'htmlOptions' => array('style' => "text-align:center;"),
),
Hope this will solve your problem.
Replace "refund" with this code.
array(
'header' => 'Refund',
'name' => 'refund',
'value' => '($data->refund == 0) ? "Yes" : "No"'
),
When displaying a boolean field in a CGridView use the name:type:header format when creating the columns to specify the type as boolean. E.g.
$this->widget('zii.widgets.grid.CGridView', array(
...
'columns'=>array(
'id',
'refund:boolean',
),
If you want to change the way the field is displayed in a CActiveForm change the render method to use either a checkbox or a dropdown list. My preference is dropdown list because it gives you the option of setting the value back to null.
$form->dropDownList($model,'refund', array(null=>"Not checked", 0=>"No", 1=>"Yes"));
Quick fix:
Replace 'refund', with:
array(
'name' => 'refund',
'type' => 'raw',
'value' => function($model){
return $model->refund == 1 ? 'No' : 'Yes';
}
),
IN VIEWS NAMES ADMIN.PHP
array(
'name'=>'status',
'header'=>'status',
'filter'=>array('1'=>'Inacive','2'=>'Active'),
'value'=>'($data->status=="1")?("Inacive"):("Active")'
),