Jonathon Posted August 7, 2013 Share Posted August 7, 2013 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 More sharing options...
Edward Posted August 9, 2013 Share Posted August 9, 2013 Sorry Jonathon, if i had worked with cGridView I would of given you a hand but ive only work with cListView so far. You seem to be further ahead on your project than I am. Link to comment Share on other sites More sharing options...
Larry Posted August 9, 2013 Share Posted August 9, 2013 Jonathon, no answer off the top of my head, but I can look into this for you if it's still an issue. Link to comment Share on other sites More sharing options...
Edward Posted August 10, 2013 Share Posted August 10, 2013 Im just about to start reading the cGridView and implementing it today, i have a similar situation to yours coming up Jonathon. Link to comment Share on other sites More sharing options...
Edward Posted August 10, 2013 Share Posted August 10, 2013 Okay i found a solution for what you want to do but you will need to write your own ajax request. Here is a link to a page on the YiiFramework forum for the same problem. http://www.yiiframework.com/forum/index.php/topic/12564-cgridview-shopping-cart/ Link to comment Share on other sites More sharing options...
Edward Posted August 10, 2013 Share Posted August 10, 2013 Also i found a Yii Booster extension: TbTotalSumColumn http://yiibooster.clevertech.biz/extended-grid.html#extendedfilter Link to comment Share on other sites More sharing options...
Edward Posted August 10, 2013 Share Posted August 10, 2013 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 More sharing options...
Jonathon Posted August 12, 2013 Author Share Posted August 12, 2013 Hi Edward + Larry, Yes I'd already looked at that, I just wasn't sure how get the value exactly via JQuery Link to comment Share on other sites More sharing options...
Edward Posted August 12, 2013 Share Posted August 12, 2013 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 More sharing options...
Jonathon Posted August 18, 2013 Author Share Posted August 18, 2013 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. 1 Link to comment Share on other sites More sharing options...
Larry Posted August 19, 2013 Share Posted August 19, 2013 Thanks, Jonathon, for sharing that! Link to comment Share on other sites More sharing options...
Edward Posted September 3, 2013 Share Posted September 3, 2013 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 More sharing options...
Edward Posted September 3, 2013 Share Posted September 3, 2013 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 More sharing options...
Jonathon Posted September 3, 2013 Author Share Posted September 3, 2013 Thanks Edward. Link to comment Share on other sites More sharing options...
Edward Posted September 3, 2013 Share Posted September 3, 2013 Thank you too, Jonathon. I find your posts some of the very most useful of this forum, especially now being more related to Yii. I hope you will hang around on here for a good while. Link to comment Share on other sites More sharing options...
Recommended Posts