Jump to content
Larry Ullman's Book Forums

Updating Count Of Cgridview With Ajax


Jonathon
 Share

Recommended Posts

Hi Larry,

 

I have a hopefully not too hard a question. I am updating a Cgridview via Ajax successfully. However before I display this widget I have a small title and use

 

 

$model->itemCount 

 

just to display the number of records. When I update I update via Ajax, how do I then update this title with the new number of records (If a user deletes one, or moves a record (I have a cart/wishlist kinda thingy))

 

Thanks

 

Jonathon

Link to comment
Share on other sites

I found the best solution to this problem with CGridView

 

http://www.yiiframework.com/doc/api/1.1/CGridView#afterAjaxUpdate-detail

 

afterAjaxUpdate property
public string $afterAjaxUpdate;

a javascript function that will be invoked after a successful AJAX response is received. The function signature is function(id, data) where 'id' refers to the ID of the grid view, 'data' the received ajax response data.

 

If you don't like writing raw javascript just use jquery for now and then if you site starts getting busy and slow change it to raw js or work on raw queries rather than using active record.

 

I apoligize for all my posts i had been updating at times as of when i found new data, new to CGridView too. It is awesome but it does require quite a bit of customization. The good point with this reuseable code is once we have it figured out for now situation its easy to adapt css or customizations for another situation rather quickly. Now i am starting to understand why people love Yii.

Link to comment
Share on other sites

Well just write a Yii or standard ajax function in the afterAjaxUpdate method to run a query on the current wish list/ watch list database then once the value is returned update the value on the page. Its as simple as that. You can add the ajax method to which ever model is most relevant to your wish list or watch list.

 

This page may be useful it has some info on how to edit the afterAjaxUpdate method without directly writing into the framework files.

 

http://www.yiiframework.com/forum/index.php/topic/23116-hide-a-column-in-cgridview/

Link to comment
Share on other sites

  • 2 weeks later...

So it was the afterAjaxUpdate, that was the way i done it, and CJavaScriptExpression is the only way to get the javascript into the widget code. Have you dealt with the session time out problem that CGridView fails to handle where you can override the CWebUser class (That is if you are using session timeouts)? You need to have a bit of patience sometimes to workout some these problems it can take some reading around.

Link to comment
Share on other sites

For anyone interested. I did it like this:

'afterAjaxUpdate'=> new CJavaScriptExpression('updateCounter'), //ajax 
 

Then called the JS function that used CHtml::ajax() to pass the command to the controller and return a json response.

 

By the way this method is slightly slower i just tested it against putting the actual ajax request js code within the afterAjaxUpdate. You can test it for yourself and you will notice it updates at the same time without the time lag. I am starting to really love CGridView, it can be a bugger to get to know but somehow it seems to work.

 

I BEG YOUR PARDON

 

Sorry it is the same but for me personally i prefer to add in the js code directly so that its easier to find out quickly what going on with that particular part of cgridview. I do have separate js sheets but on this occasion it isn't really necessary to create more.

'afterAjaxUpdate'=>new CJavaScriptExpression("function(id, data){
    $.post(  
        '".Yii::app()->baseUrl."/item/countinventory',   
        function(data){  
	    $('#item-inventory-count').text(data);
            },  
            'html' 
		);
	    return false;
}"),
Link to comment
Share on other sites

 Share

×
×
  • Create New...