Hi everyone,
I am learning Yii and doing some practices, now i have a question about searching…
Thanks for help in advance.
I have already now how to achieve search in a normal page.
But now i meet a problem.
In my view/index page. I do have a conditional data provider, like this:
public function actionView()
{
$id = $_GET['id'];
$coursesDataProvider = new CActiveDataProvider('Courses', array(
'criteria' => array(
'condition' => 'university_id=:universityId',
'params' => array(
':universityId' => $this->loadModel($id)->id),
),
'pagination' => array('pageSize' => 10),
));
$this->render('view', array(
'model' => $this->loadModel($id),
'coursesDataProvider' => $coursesDataProvider,
));
}
For example, I have this code in my “University controller”, I have a widget to render the list of courses in the university’s view page.
But i only want to display this university’s course, so I use this type of data provider to give me ‘condition’ => ‘university_id=:universityId’,
so i can achieve this function(only display relative courses in this university, not courses in other universities)
However, now I want to do search in this page. I want to , still, search relative courses only.
How can I achieve that? I know there is a way like this to render a search, but I will lose my ‘condition’ => ‘university_id=:universityId’, and fail to display any data.
$model=new University('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['University']))
$model->attributes=$_GET['University'];
//send model object for search
$this->render('index',array(
'dataProvider'=>$model->search(),
'model'=>$model
));
Any better ideas to render this?
And if you have samples…would you please also help me about the views part? My old codes doesn’t work either.
Here are part of my old code I guess can be reused…(java part)
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiListView.update('coursesylistview', {
//this entire js section is taken from admin.php. w/only this line diff
data: $(this).serialize()
});
return false;
});
");
?>