Yes, this can be done using several ajax calls. Every Ajax request should start an controller action which returns single value, or complex HTML form. So normally you put your logic into controller, and rendering in view, but this time, you will return view content as ajax response rather than printing it.
Here are few examples:
[size="4"]Example with HTML response [/size]
public function actionAjaxGetUser($id){
if(Yii::app()->request->isAjaxRequest()){
$user = Users::model()->findByPk($id);//Get user
$this->renderPartial('displayUser',array('user'=>$user));//Render this user using displayUser view file
Yii::app()->end();//End application
}
echo 'Ups you should do ajax request to access this action';
}
[size="4"]Example of controllers action with JSON response [/size]
[size=“2”]Note: If you want to return JSON as AJAX response, then set dataType=‘json’ in ajax call (look below)[/size]
public function actionAjaxGetUserJson($id){
if(Yii::app()->request->isAjaxRequest()){
$user = Users::model()->findByPk($id);//Get user
echo json_encode($user);
Yii::app()->end();//End application
}
echo 'Ups you should do ajax request to access this action';
}
[size="4"]How to make ajax call on front-end[/size]
[size="2"]Note: This ajax call can be encapsulated in any other javascript code[/size]
jQuery.ajax({
type: 'POST',//You can set GET or POST
dataType:'json',//Here you define return format. Set html if you will return HTML content, and set json if you will return json format
url: '<?php echo $this->createAbsoloteUrl('controller/AjaxGetUser'); ?>',//The url of action you want to call
data:{id:<?php echo $idUser; ?>,otherParameter:'hallo'},//These values can be accessed in controller by typing Yii::app()->request->getParam('id'), Yii::app()->request->getparam('otherParameter')
success:function(data){//Function that be called upon successful ajax call
alert(data);//Print received data
},
error:function(a, b, c){//Function that be called upon unsuccessful ajax call
//console.log(a,b,c);//Use this to print errors or somethign
alert('Ajax call was not successful');
}
});