Display TbDetailView attributes in a single table row - yii

<?php
$this->widget('bootstrap.widgets.TbDetailView',array(
'type'=>'table condensed bordered',
'data'=>$model,
'attributes'=>array(
'regid',
'fname',
'lname',
'mname',
'registered_date',
)
));
?>
For the above TbDetailView attributes, I want to display fname and lname in a single row.

Try to use this approach:
$this->widget('bootstrap.widgets.TbDetailView',array(
'type'=>'table condensed bordered',
'data'=>$model,
'attributes'=>array(
'regid',
array(
'label'=>'User name',
'type'=>'raw',
'value'=>$model->fname.' '.$model->lname
)
'mname',
'registered_date',
)
));

Related

How to disable sort for a column in CGridView?

There is the enableSorting flag, that, if equals false, disable sorting for all grid columns. But how can I do this for just one specific column?
try this
'sortable'=>false,
Here is example ,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tasks-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'id',
array(
'header'=>'Surname',
'value'=> '$data->surname',
'name'=> 'surname',
'sortable'=>false,
),
'due_date',
'status',
),

extendedSummary Yiiboster not show the sum total of gridview

I have gridview using TbExtendedGridView yii booster, in footer gridview i want show the sum of field using extendedSummary, i have follow the tutorial in yiibooster web but i can't show the extendedSummary to show the sum of column. Pls help me what's wrong in my code below ?. in the footer just show the box with blank text.
//this isi my gridview code
$no_loan= $_GET[no_loan];
$sql2="SELECT * from tbangsuran where nomor_pinjaman = '$no_loan' and status_bayar=1 order by no ASC";
$sqlProvider = new CSqlDataProvider($sql2);
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
//'filter'=>$model,
'id'=>'tbangsuran-grid',
'type'=>'striped bordered',
'template' => "{items}\n{extendedSummary}",
'dataProvider' =>$sqlProvider,
'columns'=>array(
array(
'name'=>'Angsuran Ke',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:90px;'),
'htmlOptions'=>array('style'=>'text-align:center'
),
'value'=> '$data[\'no\']',
),
array(
'name'=>'Tanggal Tagihan',
'headerHtmlOptions'=>array('style'=>'text-align:center;width:90px;'),
'htmlOptions'=>array('style'=>'text-align:center'),
'value'=> 'date("d-m-Y",strtotime($data[\'tanggal_bayar\']))',
),
//'nomor_pinjaman',
array(
'name'=>'Tunggakan Pokok',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_pokok\'],0,"",".")',
),
array(
'name'=>'Tunggakan Bunga',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_bunga\'],0,"",".")',
),
array(
'name'=>'Total Tunggakan',
'headerHtmlOptions'=>array('style'=>'text-align:center;width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_pokok\']+$data[\'pastdue_bunga\'],0,"",".")',
),
),
'extendedSummary' => array(
'title' => 'Total Tunggakan',
'columns' => array(
'pastdue_pokok' => array('label'=>'Total Tunggakan','class'=>'TbSumOperation')
)
),
'extendedSummaryOptions' => array(
'class' => 'well pull-right',
'style' => 'width:300px'
),
));
You must use the same column name in extendedSummary field.
In your case would be:
'extendedSummary' => array(
'title' => 'Total Tunggakan',
'columns' => array(
'Total Tunggakan' => array('label'=>'Total Tunggakan','class'=>'TbSumOperation')
)
),
I recommend you to use short column name to this tasks.
Hope this helps!

Accessing field in related table through Foreign Key in Yii

I have three tables
1. tbl_employee: id(PK), name, position_id(FK), type_id(FK)
2. tbl_position: id, position
3. tbl_type : id, type
I want to display records in field position like what sql does below.
SELECT tbl_employee.name, tbl_position.position
FROM tbl_employee, tbl_position
WHERE tbl_employee.position_id = tbl_position.id AND tbl_position.position LIKE '%Designer';
In my EmployeeController
public function actionIndex(){
$dataProvider=new CActiveDataProvider('Employee', array(
'criteria'=>array(
'select'=>'t.name, tbl_position',
'with'=>array(
'position'=>array('select'=>'position'),
'type'=>array('select'=>'type'),
),
'condition'=>"tbl_position.position LIKE '%Designer'",
),
));
$this->render('index',array('dataProvider'=>$dataProvider,));
}
and in my models
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(
'position' => array(self::BELONGS_TO, 'Position', 'position_id'),
'type' => array(self::BELONGS_TO, 'Type', 'type_id'),
);
}
and this is my view
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'name'=>'position_id',
'value'=>CHtml::encode($model->position->position),
),
array(
'name'=>'type_id',
'value'=>CHtml::encode($model->type->type),
)
),
)); ?>
The error I've got is:
Active record "Employee" is trying to select an invalid column "tbl_position".
Note, the column must exist in the table or be an expression with alias.
How can I access position field in tbl_position by using join?What is the correct syntax for achieving that purpose.
Many thanks
'select'=>'t.name, position.position',
'condition'=>"position.position LIKE '%Designer'",
or
$model = TblEmployee::model()->with('position','type')->findall(array("condition"=>"position.position LIKE '%Designer'"));
Here I find the solution, but actually your answer was very close
public function actionIndex(){
$dataProvider=new CActiveDataProvider('Employee', array(
'criteria'=>array(
'select'=>'t.name',
'with'=>array(
'position'=>array('select'=>'position'),
'type'=>array('select'=>'type'),
),
'condition'=>"position.position LIKE '%Designer'",
),
));
$this->render('index',array('dataProvider'=>$dataProvider,));
}

yii cgridview dropdownlist value

I am unable to set the value of the column day in the cgrid view :
I am able to see the dropdownlist but all have the same day Monday set irrespective of the value of the day as represented by $data->day
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'type'=>'raw',
'name'=>'day',
'value'=> ' CHtml::dropDownList(\'someName\'.$row,\'$data->day\',array(
\'Mo\'=>\'Monday\',
\'Tu\'=>\'Tuesday\',
\'We\'=>\'Wednesday\',
\'Th\'=>\'Thursday\',
\'Fr\'=>\'Friday\',
\'Sa\'=>\'Saturday\',
\'Su\'=>\'Sunday\',))',
),
'ts_id'
)
));
If you can accept a minor break in MVC then things will get much easier to read if you put a little getter method into your model:
public function getDayDropDown()
{
$days = array('Mo'=>'Monday', ...);
return CHtml::dropDownList('someName', $this->day, $days);
}
Now in your gridview you can use it as a column like
array(
'name' => 'Day',
'type' => 'raw',
'value' => '$data->dayDropDown',
),
Just a minor issue resolved the whole episode.
I had put quotes around $data->day in the original post. which was not needed.
array( 'type'=>'raw',
'name'=>'day',
'value'=> ' CHtml::dropDownList(\'someName\'.$row,$data->day,
array(\'Mo\'=>\'Monday\',
\'Tu\'=>\'Tuesday\',
\'We\'=>\'Wednesday\',
\'Th\'=>\'Thursday\',
\'Fr\'=>\'Friday\',
\'Sa\'=>\'Saturday\',
\'Su\'=>\'Sunday\',)
)',
),
This is an example of dropdown value and update onchange:
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'session-grid',
'type'=>'striped bordered condensed',
'dataProvider'=>$PSDataProvider,
'filter'=>$PSModel,
'emptyText'=>'No hay sesiones abiertas.',
'columns'=>array(
array('name'=>'username', 'header'=>'Nombre de Usuario', 'filter'=>CHtml::activeTextField($PSModel, 'username',
array('placeholder'=>'Buscar por usuario...'))),
array('name'=>'product', 'header'=>'Producto', 'filter'=>CHtml::activeTextField($PSModel, 'product',
array('placeholder'=>'Buscar por producto...'))),
array('name'=>'expire', 'value'=>'date("d-m-y H:i:s", $data->expire)','header'=>'Hora de Expiracion', 'filter'=>CHtml::activeTextField($PSModel, 'expire',
array('placeholder'=>'Buscar por expiracion...'))),
array(
'header'=>CHtml::dropDownList('pageSize',$pageSize,array(5=>5,10=>10,20=>20,50=>50),array(
'onchange'=>"$.fn.yiiGridView.update('session-grid',{ data:{pageSize: $(this).val() }})",
'class'=>'span1 custom-tb-dropdown-inline',
)),
'class'=>'bootstrap.widgets.TbButtonColumn',
'htmlOptions'=>array('style'=>'width: 50px'),
'template'=>'{delete}',
'buttons'=>array(
'delete' => array(
'label'=>'Terminar sesión',
),
),
'deleteConfirmation'=>'Está seguro que desea terminar la sesión seleccionada?',
'deleteButtonUrl'=>'$this->grid->owner->createUrl("productSession/delete", $data->primaryKey)'
),
),
));

order data by desc order in default in YII

I am tyring to display somedata in desending order in YII view.HOw Do I order the data by studentID desc by default ?
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'student-grid',
'criteria'=>array(
'order'=>'StudentID DESC',
),
'dataProvider' => Agent::getStudents($model->agent_id),
'columns' => array(
'StudentID',
'first_name',
'last_name',
'dob',
'gender',
array(
'header' => 'Options',
'class' => 'CButtonColumn',
'template'=>'{View}',
'buttons'=>array(
'View' => array(
'url'=> 'Yii::app()->createUrl("/students/view/" . $data->StudentID)',
),
),
),
),
)); ?>
Thanks Ab
In your dataProvider (i assume that's what getStudents() function returns) add another array to your config array :) Like:
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$some_criteria,
/* Your array */
'sort'=>array(
'defaultOrder'=>array(
'StudentID'=>true,
),
),
/***/
));
Value 'false' in 'defaultOrder' array refers to ascending order, 'true' refers to descending order.
I hope that's what you're looking for :).
Also check out this forum thread: http://www.yiiframework.com/forum/index.php/topic/8428-cgridview-default-sort/
and this doc: http://www.yiiframework.com/doc/api/1.1/CSort#defaultOrder-detail
Regards.