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']],
],
Related
i checked 1000 times my code
all things okey ! but after i change my theme ! its not send ajax request for filtering grid view
when i checked this code i thought is okey and i should say that i searched and check wiki yii but i couldnt find my problem
my view Code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'product-grid',
'itemsCssClass' => 'table table-bordered table-striped dataTable',
'dataProvider' => $model->notDeleted()->search(),
'filter' => $model,
'columns' => array(
'appId',
array(
'name' => 'categoryId',
'value' => '$data->category->name',
'filter' => CHtml::activeDropDownList($model, 'categoryId', CHtml::listData(Category::model()->notDeleted()->findAll(), 'id', 'name'),
array('empty' => '')
)
),
'name',
'packageName',
'price',
'discount',
'version',
array(
'name' => 'isActive',
'value' => '$data->isActive ? Yii::t("app", "Yes") : Yii::t("app", "No")',
'filter' => CHtml::activeDropDownList($model, 'isActive', array(
1 => Yii::t('app', 'Yes'),
0 => Yii::t('app', 'No'),
))
),
array(
'name' => 'inAppPurchase',
'value' => '$data->inAppPurchase ? Yii::t("app", "Yes") : Yii::t("app", "No")',
'filter' => CHtml::activeDropDownList($model, 'inAppPurchase', array(
1 => Yii::t('app', 'Yes'),
0 => Yii::t('app', 'No'),
))
),
/*
'createUserId',
'createTime',
'isDeleted',
*/
array(
'class' => 'CButtonColumn',
),
),
)); ?>
and my controller:
$model = new Product('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Product']))
$model->attributes = $_GET['Product'];
$this->render('admin', array(
'model' => $model,
));
please help me !
For the next one, I had the same problem. I proceeded like this and it worked. for my case I delete
<script src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery-1.11.1.min.js">
</script>
Check if you included jquery.js more than once in your page. I had the same error and the reason was that yii had already added the jquery.js script, so no need to include it yourself.
Cr.Andrey Zausaylov
https://stackoverflow.com/a/41304449/3032180
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 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
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),