Yii Framework Forum: View List - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

View List Rate Topic: -----

#1 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 03 March 2013 - 11:03 PM

In Yii how can add Filter drop down list in CList view? In a have 3 fields- Id, Name, Sex. I want to add a filter drop down in list view contain sex attribute (male/female) and update the list on selection of drop down. How it possible ?
Thanks
Aneesh.
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 04 March 2013 - 12:17 AM

Dear FRIEND

Let us assume that we have a Model:Medico(id,name,age,sex,native).

In index.php,you have to insert a dropdown at the top and also registers a script to update the list view.

views/medico/index.php
<?php
echo CHtml::dropDownList('sex','',array("male"=>"male","female"=>"female"),array(
'id'=>"sex","prompt"=>"choose sex"
));
?>
<?php $this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
	'id'=>"medico-list"
	
)); ?>

<?php

Yii::app()->clientScript->registerScript('sex','
$("#sex").change(function(){
	$.fn.yiiListView.update("medico-list",{data:{sex:$(this).val()}});
	
	});

');



MedicoController.php

public function actionIndex()
{  
     $criteria=new CDbCriteria;
     if(isset($_GET['sex']))
	{
		$criteria->addCondition("sex=:sex");
		$criteria->params=array(":sex"=>$_GET["sex"]);
	}

     $dataProvider=new CActiveDataProvider('Medico',array(
			'criteria'=>$criteria,
			));
     $this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
}


Regards.
1

#3 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 04 March 2013 - 01:07 AM

Thanks 4 ur rply.. The code works fine. But In the controller action have a condition. After adding this condition the code doesn't works. How it solve?
Controller
public function actionIndex()
	{       
                $type='2';
                 $criteria=new CDbCriteria;
                if(isset($_GET['sex']))
        {
                $criteria->addCondition("sex=:sex");
                $criteria->params=array(":sex"=>$_GET["sex"]);
        }
                $criteria->select = "`name`,`address`";
                $criteria->condition="type = $type";               
                $dataProvider=new CActiveDataProvider('Address',array('criteria'=>$criteria,'pagination'=>array('pageSize'=>3)));
		
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}


Index
<?php
echo CHtml::dropDownList('sex','',array("1"=>"male","2"=>"female"),array(
'id'=>"sex","prompt"=>"choose sex"
));
?>
<?php $this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
        'id'=>"address-list"
)); ?>
<?php

Yii::app()->clientScript->registerScript('sex','
$("#sex").change(function(){
        $.fn.yiiListView.update("address-list",{data:{sex:$(this).val()}});
        
        });

');

Thanks
Aneesh.
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 04 March 2013 - 01:40 AM

Dear Friend

Change the following

$criteria->condition="type = $type";      


to

$criteria->addCondition("type=$type");   

0

#5 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 240
  • Joined: 26-September 12

Posted 04 March 2013 - 01:43 AM

Thanks...:) It works fine.
Thanks
Aneesh.
0

#6 User is offline   gin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 14-October 12

Posted 16 March 2013 - 07:38 PM

View Postseenivasan, on 04 March 2013 - 12:17 AM, said:

Dear FRIEND

Let us assume that we have a Model:Medico(id,name,age,sex,native).

In index.php,you have to insert a dropdown at the top and also registers a script to update the list view.

views/medico/index.php
<?php
echo CHtml::dropDownList('sex','',array("male"=>"male","female"=>"female"),array(
'id'=>"sex","prompt"=>"choose sex"
));
?>
<?php $this->widget('zii.widgets.CListView', array(
	'dataProvider'=>$dataProvider,
	'itemView'=>'_view',
	'id'=>"medico-list"
	
)); ?>

<?php

Yii::app()->clientScript->registerScript('sex','
$("#sex").change(function(){
	$.fn.yiiListView.update("medico-list",{data:{sex:$(this).val()}});
	
	});

');



MedicoController.php

public function actionIndex()
{  
     $criteria=new CDbCriteria;
     if(isset($_GET['sex']))
	{
		$criteria->addCondition("sex=:sex");
		$criteria->params=array(":sex"=>$_GET["sex"]);
	}

     $dataProvider=new CActiveDataProvider('Medico',array(
			'criteria'=>$criteria,
			));
     $this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
}


Regards.




nice work,but how is multiple ajax calls ?
0

#7 User is offline   yiiplayboy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 03-May 12

Posted 18 March 2013 - 05:16 AM

View PostRvr101, on 03 March 2013 - 11:03 PM, said:

In Yii how can add Filter drop down list in CList view? In a have 3 fields- Id, Name, Sex. I want to add a filter drop down in list view contain sex attribute (male/female) and update the list on selection of drop down. How it possible ?



if you just want to add a filter try this,
in your CListview columns

array(
       'name'=>'status',
       'header'=>Yii::t('zii','Status'),
       'filter'=>array('1'=>Yii::t('zii','Active'),'0'=>Yii::t('zii','Inactive')),
       'value'=>'($data->status=="1")?("Active"):("Inactive")',
      ),

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users