How i dont display empty and null rows in yii - 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.

Related

Yii, set radio button value fetch from database

In Yii, I listed my table which is fetch from database in grid view.
'value'=> 'CHtml::radioButton("set_default",false,array(
"value"=>"$data->id",
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
))',
the above code I entered in normal radio button view, how do I fetch from database? Anybody help me?
CGridView lets you write any value inside row's column instead of normal database values. For modify column values, you need to add a function for value attribute. Inside of this function, you can access each data's attributes. In your case, You need to echo a radio button like this:
//$data refres to each data row in the CGridView
'value' => function ($data, $row) {
echo
CHtml::radioButton("set_default",false,array(
"value"=>$data->id,
"set"=>"1",
"disable"=>"disable",
"uncheckValue"=>null
)),
},

Div id conflict in ClistView yii 1

I am working on a Yii project,on the index page i have multiple div id's loaded by the yii the first id is named yw0 and second is yw1 and third is yw2,but after the yw1 id i have a ajax page which also generates two id's yw0 and yw1. so the new id's are conflicting with the old one what will will be the possible solution to get rid of this.
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dp,
'itemView'=>'_view',
'id'=>'id-of-list-view-div',
'pager'=>array(
'id'=>'id-of-pager-ul'
)
));
You can set your own html ID on both widgets (CListView and CPager)
This way when they are reloaded they will keep the IDs and there will be no conflict
Hope it helps

Yii populate a dropdown on select of another

I have a dropdown that I want to populate when an item in another dropdown is selected. Both the dropdown are tied to data/model passed on from controller. and the first dropdown is populated from DB by calling a function in the model. Heres' the form,
echo $form->dropDownListRow($modelunit,
'superunit',
$model->getSunits(),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('user/getunits'),
'update'=>'#unit_id',
)));
echo CHtml::dropDownList('unit_id','', array());
Here's the action user/getunits called by Ajax.
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['superunit']));
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
I keep getting an error "Undefined index: superunit" when first dropdown is selected. Also, you may notice I am using form->dropDownListRow for the first dropdown while using CHtml::dropDownList for the second. That's cause I am clueless on the syntax of how exactly to make sure the dropdown is populated correctly with ajax and at also properly bind to the model.
You use $form->dropDownListRow that's why you will get $_POST['MyModelName']['superunit'] on your server side
Change you code like
$data=Unit::model()->findAll('sid=:sid',
array(':sid'=>(int) $_POST['MyModelName']['superunit']));
Where MyModelName is a model that you use)
Or like
echo CHtml::dropDownList('superunit'.....
For others - this wiki may help.

Hidden Pagination summary text

I use CGridView in Yii to create tables.
I would like to show my table with pagination but hide the summary text at the top, which indicates the number of page restance (Displaying 1-4 of 4 results.)
Is that possible?
thank you
Sorry for my English
There is a template option. By default it equals {summary}\n{items}\n{pager}
If you override it in your gridview config, you'l be able to remove summary section:
$this->widget(
'zii.widgets.CGridView',
array(
Your options here ...
'template' => '{items}\n{pager}',
)
);
Another option is to set the CGridView summaryText value to false
Because YII uses CListView, it also add a certain summary class CSS in the asset folder. So to combat this, simply override the css.
Add this to your CSS.
#yw0>.summary{
display:none;
}

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