Progress of Ajax

Is there any way to have feedback from an ajax submit which inserts records into a database?

I have a CGridView with checkboxes and each of the records need to be imported using a database driver that takes ~2 mins per record. I think it would be beneficial to display which records have been inserted.

an ajax request is a http request after all, so you have no way to return data back while the request is running (you can only do it when it ends).

In order to accomplish what you need, you can so something like:




// javascript code.

var ids=[1,2,3,4,5];//you'd take these from your selected rows in the grid, this is just an example.

var i=0;

callThisRecursiveFunction();


function callThisRecursiveFunction(){

   if(ids.length == 0){

     // the recursive function done all the items in the queue, so we need to stop and tell the user we're done.

     return;

   }

   ++i;

   var requestId=ids.splice(0,1);

   $.get('your/yii/url/handling/this/request',{id:requestId},function(data){

      if(data.result=='success'){

         $("#some-id").html("Action #"+i+" completed");

         callThisRecursiveFunction();

      }

   },"json");


}



Your Yii controller action should be smth like:




public function actionProcess($id){

   $model=SomeModel::model()->findByPk((int)$id);

   if(empty($model)){

     // this is an invalid request

     echo json_encode(array('result'=>'error'));

     Yii::app()->end();

   }

   // do processing here

   echo json_encode(array('result'=>'success'));

   Yii::app()->end();

}



These are big lines, but you get the point…i hope :)

Fantastic! It seems so obvious now.

Keep up the good work!