beoneofus
(Rdandaya)
December 15, 2012, 11:38am
1
How can I add a search in my _view file?
I have an account table, and the users are student and teacher. in the list or _view all record will be post right.
so I want to put a search field or dropdown on it. In dropdown there are two options teachers or students. If i choose the teacher only users/data of the teachers will be viewed.
thank you
konapaz
(Konapaz)
December 15, 2012, 6:28pm
2
Hi my friend
This code may be help you
$dataProvider=new CActiveDataProvider(‘Users’);
$this->widget(‘zii.widgets.grid.CGridView’, array(
'dataProvider'=>$dataProvider,
));
OR
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter'=>$model,
For more details
http://www.yiiframework.com/doc/api/1.1/CGridView
seenivasan
(Chellamnivas)
December 15, 2012, 7:38pm
3
Dear Friend
I assumed that you want to do filter in CListView rather than in CGridView.
I also assumed that teacher and students belong to a column something like ‘designation’.
index.php
<?php echo CHtml::dropDownList("designation",'',array('teacher'=>"teacher","student"=>"student",),array("prompt"=>"select",'id'=>"designation"));?>
<?php $this->widget('zii.widgets.CListView', array(
'id'=>'account',
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
)); ?>
<?php Yii::app()->clientScript->registerScript('account','
$("#designation").on("change",function(){
$.fn.yiiListView.update("account",{"data":{"designation":$(this).val()}});
});
'); ?>
So in index.php I have added a drop down list. Based on the selection it is going to update the list by AJAX.
In controller
public function actionIndex()
{ $criteria=new CDbCriteria;
if(isset($_GET['designation']) && $_GET['designation']!=="")
$criteria->condition='designation="'.$_GET['designation'].'"';
$dataProvider=new CActiveDataProvider('Person',array(
'criteria'=>$criteria
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
Regards
beoneofus
(Rdandaya)
December 15, 2012, 8:39pm
4
thanks you konapaz and seenivasan for your help.
I used code the same as in the search filter, I based it there and it work.
Thanks both of you for your help ^^