Dataprovider update/load with ajax call?

Hello everyone! I’ve been using Yii for some time and have to say that every day I discover something exciting and new! Very nice framework!

However I have come to simple problem, and wanted to ask Yii people to help me.

In my controller, I have code that loads dataprovider from database.

In my view I use that dataprovider to render zii.widgets.CListView.

But then, I get from javascript a variable (site visitor location), and want to get that variable back to controller (doesn’t have to be the same controller mentioned above), so it can update the dataprovider (eg. search items in database with same or similar location string) and get updated/new dataprovider back to view page so it can render another CListView.

I’m stuck, how can I do this?

Here is my code for siteController.php



public function actionIndex()


	{


                $dataProvider=new CActiveDataProvider('Project', array(


                'criteria'=>array(


                    'condition'=>'project_status = 0',


                    ),


			'pagination'=>array(


			'pageSize' => 8,


			),


		));  


		$this->render('index',array('dataProvider'=>$dataProvider));


	}

So in my view, I use that dataprovider, and also get the location variable from google:

 CityName = google.loader.ClientLocation.address.city; 

Now I want to write something like this:

CHtml::ajax(CODE HERE)

So I can return without refreshing to another siteControler function and do this:



public function actionIndexLocation($locationName)


	{


		$dataProvider=new CActiveDataProvider('Project', array(


            'criteria'=>array(


                'condition'=>'project_status = 0 AND location.location_name LIKE ' . '%' . $locationName . '%',


                'limit' => 4,


                ),


	        ));


		 $this->renderPartial('indexLocation', array('dataProvider'=>$dataProvider));


	}

and get new dataprovider to render another CListView from the same view.

Can you please give me some help?

can request the same action:





public function actionIndex($locationName='')

        {

                if($locationName == ''){

                   $dataProvider=new CActiveDataProvider('Project', array(

                'criteria'=>array(

                    'condition'=>'project_status = 0',

                    ),

                        'pagination'=>array(

                        'pageSize' => 8,

                        ),

                ));  

 $this->render('index',array('dataProvider'=>$dataProvider));


                }else{

                     // ajax request and passed the cityName from client 

                       $dataProvider=new CActiveDataProvider('Project', array(

            'criteria'=>array(

                'condition'=>'project_status = 0 AND location.location_name LIKE ' . '%' . $locationName . '%',

                'limit' => 4,

                ),

                ));

                 $this->renderPartial('indexLocation', array('dataProvider'=>$dataProvider));

                  }

                  

                       }




js code try use :





CityName = google.loader.ClientLocation.address.city; 


$.fn.yiiListView.update('listViewIdHere', {

 //type:'GET',

data:{"locationName":CityName}

});




just a tip , you may need do some modify . : $this->renderPartial(‘indexLocation’ //may render the same view file :D

[color="#008000"]NOTE: merged here the same topic that you made in "tips, snippets and tutorials"[/color]

Please do not make duplicate posts, it’s not needed.

Hello,

Thank you, so solution is to send ajax request without doing the CHtml::ajax() part.

So the right way is to call it from javascript directly. Thanks for the tip! Great help, I know to get stuck in one mindset.

@mdomba - Sorry I made first topic and realised i posted it in wrong section. It wont happen again.

I found a solution:

In page view - jQuery, CityName is location name found by google maps geocoding service:




function getLocationData(){

    var locationList = $('#LocationAjaxResult');


    locationList.html("Loading..."); //TODO: FORMAT THIS STRING

        $.ajax({

           url: "<?php echo Yii::app()->createUrl('site/data'); ?>",

           dataType: 'json',

             data: {"locationData" : CityName},

             cache: false,

             success: function(data){

               showLocation = true;

               if(data != null)

               {

                    if(data != "")

                    {

                        document.getElementById('LocationAjaxResult').innerHTML=data;         

                    }

                    else

                    {

                        document.getElementById('locationPhase').innerHTML='';            

                    }

               }

               else

               {

                    document.getElementById('locationPhase').innerHTML='';            

               }

               

           }

        });

    }

In page controller:




public function actionData($locationData)

	{

		$dataProvider=new CActiveDataProvider('Project', array(

               'criteria'=>array(

                   'condition' => ' project_status = 0 AND location_name LIKE "%' . $locationData .'%"',

					'limit' => 4,

					'with' => array('location'),

                  ),

			'pagination'=>array(

			'pageSize' => 4,

			),

		)); 

		$projectListing[] = $this->renderPartial("indexLocation", array('dataProvider' => $dataProvider), true);

		echo CJSON::encode($projectListing);

	}



and in page view file:




<div id="locationPhase">

something something

<a id="locationField"></a>

<div id="LocationAjaxResult"></div>

</div>