<?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),
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']],
],
there are 2 columns check box in cgridViewtable and two bootstrap widgets TbButton in view page.
I can not get value of my checkbox very good. My value in checkboxs transfer into controller but changed id of checkboxs After a period of timeand , and controller don't knew checkbox,
View:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'profile-information-form',
'enableAjaxValidation' => false,
));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'First validation'),));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'End validation'),));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'profile-information-grid',
'dataProvider' => $model->children(),
'filter' => $model,
'columns' => array(
array(
'header' => '',
'value' => '$this->grid->dataProvider->pagination->offset + $row+1', // row is zero based
),
array(
'name' => 'ProfileInformation.user.scope',
'value' => 'CHtml::encode($data->user->scope->name)',
'filter' => Scope::model()->options,
),
array(
'name' => 'id',
'value' => 'CHtml::encode($data->id)',
),
array(
'name' => 'center',
'value' => 'CHtml::encode($data->center)',
),
array(
'name' => 'sendCount',
'value' => 'CHtml::encode($data->sendCount)',
'filter' => '',
),
// Use CCheckbox column with selectableRows = 2 for Select All
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser2(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser1(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
),
));
// action button
$this->endWidget();
Controller:
if (isset($_POST['profile-information-grid_c10']) & isset($_POST['yt0'])) {
////Do action1
}
if (isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])) {
////Do action2
}
}
my problem is in $_POST['profile-information-grid_c10'] and _POST['profile-information-grid_c12'] , before id were $_POST['profile-information-grid_c8'] and $_POST['profile-information-grid_c10'] , and now is $_POST['profile-information-grid_c12'] and $_POST['profile-information-grid_c14'].
my problem is very involved. I cannot explain very good.
I want to have a fix id.
I donto why change id of check box?
Can I assign ids for checkboxs?
Can I assign ids for checkboxs? Sure, you better explicitly set checkbox column id:
'class' => 'CCheckBoxColumn', 'id'=>'column1', ... see these docs.
This way you firmly set it and you'll have no things like these yt0, yt2...
The same you can do for the buttons. See TButton docs.
Also you need to use double && in logical condition:
isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])
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.
),
)
),
Im trying change my GridView column width. This is my code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'prefixs-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'id',
'header' => 'No.',
'htmlOptions' => array('class' => 'center'),
),
array(
'name' => 'pfx',
'htmlOptions' => array('width' => 30),
),
array(
'class' => 'CButtonColumn',
),
),
)); ?>
When i run 'pfx' columns width must be changed to 30, but width is same. How i can change width of column? Any ideas?
If you do not want to use a separated CSS definition you should do:
'htmlOptions' => array('style' => 'width: 30px;'),
'filterHtmlOptions' => array('style' => 'width: 30px;'),
'columns' => [
[
'headerOptions' => ['width' => '150'],
'attribute' => 'name',
'format' => 'raw',
'value' => 'name'
],
]
There are multiple ways to approach this issue.
You can widen the cell's contents (cells of individual rows):
$columns[] = array(
'header'=>'Name',
'value'=>'$data["name"]',
'htmlOptions'=>array('style'=>'width: 150px')
);
Sometimes you may want to widen the column header instead of the individual rows' cells:
$columns[] = array(
'header'=>'Name',
'value'=>'$data["name"]',
'headerHtmlOptions'=>array('style'=>'width: 150px')
);
Widening the header is the preferred option, but even if you do that, sometimes it doesn't work as expected.
If this is the case, you can go my personal preferred way and widen the contents of the header cell. The widening will be inherited by the rows below it as well.
$columns[] = array(
'header'=>'<div style="width:150px;">Name</div>',
'value'=>'$data["name"]',
);
i noticed with cgridview in the tags there is an id attribute id="table-name_c0" id="table-name_c1" etc. you could hook into this and the .filters class for the filter width. e.g.
#table-name table-name_c0 {
width: 30px;
}
#table-name .filters:nth(0) {
width:30px;
}
where c0 and :nth(0) are the same number.
it's slightly hacky but what you are trying to achieve is also slightly hacky imho
Simply add
'contentOptions' =>['style' => 'width:30px'],
-- For example:
'columns' => [
[
'attribute' => 'product.name',
'contentOptions' =>['style' => 'width:30px'],
],
That should do the trick!