How to achieve search function when you have data provider(conditional)

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;

});

");

?>






    $model=new Courses('search');

    $model->unsetAttributes();  // clear any default values


    $model->university_id = /* something */


    if(isset($_GET['University']))

        $model->attributes=$_GET['University'];

 

    //send model object for search

    $this->render('index',array(

        'dataProvider'=>$model->search(),

        'model'=>$model

    )); 



When $model->university_id is set, then CDbCriteria::compare() function will make the appropriate CActiveDataProvider in your Courses::search() method.




    $criteria->compare('university_id', $this->university_id);



Gii-generated ‘admin’ page is the best sample code for CGridView and CActiveDataProvider. :)

I strongly recommend you to read these and learn how CActiveDataProvider is used for CGridView.

  • actionAdmin() method of the controller

  • admin.php view file

  • _search.php partial view file

  • search() method in the model

Thanks…But My code is not working… I don’t know why…


	public function actionIndex()

	{


 $model=new Courses('search');

    $model->unsetAttributes();  // clear any default values




    if(isset($_GET['Courses']))

        $model->attributes=$_GET['Courses'];

     $model->university_id = $this->_university->id;

 

    //send model object for search

    $this->render('index',array(

        'dataProvider'=>$model->search(),

        'model'=>$model

    )); 


	}

I guess everything should be right. But each time I search anything. The view will not be updated and the page remain the same…

(search is not working)

[html]//search

<?php

Yii::app()->clientScript->registerScript(‘search’, "

$(’.search-button’).click(function(){

&#036;('.search-form').toggle();


return false;

});

$(’.search-form form’).submit(function(){

&#036;.fn.yiiListView.update('courseslistview', { 


    //this entire js section is taken from admin.php. w/only this line diff


    data: &#036;(this).serialize()


});


return false;

});

");

?>

<h1>Courses</h1>

<div><br>

<?php echo CHtml::link(‘Search Courses’,’#’,array(‘class’=>‘search-button btn btn-primary’,‘data-title’=>‘Search’, ‘data-content’=>‘hello world!’, ‘rel’=>‘popover’)); ?>

<div class="search-form" style="display:none">

<?php $this->renderPartial(’_search’,array(

'model'=&gt;&#036;model,

)); ?>

</div>

<?php $this->widget(‘zii.widgets.CListView’, array(

'dataProvider'=&gt;&#036;dataProvider,


'itemView'=&gt;'_view',


    'id'=&gt;'courseslistview',


    'sortableAttributes'=&gt;array(


                          'name',


                           ),

)); ?>

[/html]

Thanks so much…

Um, your code looks fine to me … I don’t know what’s wrong.

Isn’t there any javascript error reported in your browser’s tool?

Hi , I solved this problem…However,

I actually i don’t want implement the search in the courses index page…

I want to implement search in the university’s view page( I have a widget for courses lists in that page) , is that possible?

Or it’s not…?