Change the Row Color Based on the Column value in CGridView - yii

In Yii, CGridView has it's own background color in the row. But what I want to do is highlight particular row based on the value of one of the column.
For Instance, I have three column, id, name, status. Now, If the Value of status is Inactive or 0, I should highlight the row with some color.
I read the class reference briefly and searched this site as well. But could not find the relevant solution. If some example or some direction toward the right solution, that would be much appreciated.
Thanks,
Ujjwal

CGridView 'rowCssClassExpression' is the way to get what you want.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'rowCssClassExpression'=>'($data->myFlag==0)?"normal":"especial"',
'columns'=>array(
...
),
));
You can also call a custom php function, and pass the $data variable to it. That function should return the class name for the given row :)

Use rowCssClass and rowCssClassExpression for your functionality. I did not tested this code but the trick you can use to get your solution.
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'rowCssClass'=>array('odd','even'),
'rowCssClassExpression'=>($data->status==0)?even:odd,
'columns'=>array(
),
));

Related

How to set items per page dropdown in grid view for yii

I want to set items per page dropdown in yii gridview.
What changes i need to do this? Has anyone had implemented this type of functionality before?
You have to add summaryText entry to the gridview as below.
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider_countries,
'summaryText'=>'Displaying {start}-{end} of {count} result(s). ' .
CHtml::dropDownList(
'pageSize',
$pageSize,
Yii::app()->params['pageSizeOptions'],
array('class'=>'change-pageSize')) .' rows per page',
Result is similar to following.

yii cgridview tooltip php variable

This is my one of the Cgridview Column
array( 'name'=>'furnished',
'type'=>'raw',
'value'=>'$data->furnished=="FF" ? CHtml::link(CHtml::encode($data->furnished),
"tooltip",["title"=>"**Here I Want to show <?php echo $data->anystring ?>**", "data-toggle"=>"tooltip",
"style"=>"text-decoration: none; cursor:pointer;"]) : $data->furnished',
),
How can I achieve this . Please help
normally typed data is displayed here. but how to show PHP variable
Thanks In advance
Everything in value will be executed via PHP's eval() function. So you must change your code to this:
'value'=>'$data->furnished=="FF" ? CHtml::link(CHtml::encode($data->furnished),
"tooltip",["title"=>"**Here I Want to show ".$data->anystring."**", "data-toggle"=>"tooltip",
"style"=>"text-decoration: none; cursor:pointer;"]) : $data->furnished'
Actually, you don't need <?php and all you need is just ..

How i dont display empty and null rows in yii

How I do not display empty and null rows in yiiframework with listview.
Many rows in database are null.When I want to show data in view.php I see many null rows.I see a data on the other page.
can I see just rows with data.
The CListView widget uses a template to render each row of data.
In the skeleton generated by Gii, the index.php file has a CListView widget that uses the _view.php file to display row data.
You only have to check if a field is null and don't show it.
If you are aking for the autogenerated view.php file, the default is CDetailView
At this place, you can use the "visible" element of the attribute item:
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
array(
'name'=>'field1',
'visible'=>$model->field1!=null,
),
..............
See attributes property of CDetailView and CListView
in your model, put a condition to filter null/empty values before rendering the result to your view.
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('table_column', $this->tableColumn, false );
$criteria->condition .= "another_table_column IS NOT NULL"; //this will filter null values
Use the afterFind() method in your Model to change the value displayed in the view so that a null value is shown as "NA" or some other value like that.

Yii ClistView Pagination set number of page shown in Pager

Hi think this is a quite easy problem, but I can't find the solution in the docs. I'm using Yii ClistView to paginate my results and everything works great, simply (as I could have a lot of results) I would like to limit non the result but the page shown in the navigation (that are ten by default) to let's say 4. But I won't limit the number of my results, just when I reach the 4th page the navigation will slide (as its default behavior) to show page 5, 6, ecc...
Is this possible? Does anyone have advice?
Thanks in advance!
How about setting pager's maxButtonCount like this:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
// ...
'pager' => array('maxButtonCount' => 4),
));

Default select one item in the dropDownList Yii

I have a dynamic dropdown, data coming from the database.
<?php $sel_id = $selected_id_array[0]->UPR_RelationType;?>
My dropdowm looks like this
<?php echo CHtml::dropDownList('RelationType_'.$pat_id[0]->PAT_ID,'U2U_RelationType',CHtml::listData(MasterTypeItems::model()->findAllByAttributes(array('MSTT_MST_ID'=>$relationship_type_array[0]->MST_ID),array('order' => 'MSTT_Name')), 'MSTT_ID', 'MSTT_Name'),array('id'=>'select','class'=>'relation_type','style'=>'width:50px'));
In this dropdown i have to select defaultly $sel_id;
for example am getting $sel_id=5; In the drop down i have to select 5th option as selected in yii. please give me any suggestion what i have to write in dropDown to select $sel_id;
If I understand your question correctly, what you want to achieve is a dropdownlist which already has a pre-selected option. If so, then this piece of code should work.
echo dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
where $select would be your default selected item.
More information can be found here: http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail