Yii Framework Forum: Yii 1.1: Cgridview Update Grid After Click On Custom Button - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii 1.1: Cgridview Update Grid After Click On Custom Button Rate Topic: -----

#1 User is offline   sanderpost 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-July 14

Posted 01 July 2014 - 04:41 AM

The problem
You want a button in the buttoncolumn of the cgridview. The button should perform some action (async ofcoz) and then reload the Cgridview (without the page-reload ofcoz).

Solution
There's some posts that kinda describe the solution. Search for this post /create-custom-button-button-with-ajax-function-in-cgridview (not allowed to embed links yet :unsure:) Describes a possible solution. To me it seemed just a bit to much "write a lot javascript in a php-string".
Then there's /using-cbuttoncolumn-to-customize-buttons-in-cgridview/#c2788 on the wiki which gives a nice indication on how the options of the button can be used to attach ajax stuff.

Combining the 2 posts, do something like this in your view:

$gridId = "myGridId"; // use this for your widget's id!
...
'columns'=>array(
  ...
  array( 
   'class'=>'CButtonColumn',
   'template' => '{toggle}',
   'buttons'=>array(
     'toggle'=> array(
       'url'=> '{some expression to be evald for each button}',
       'options'=>array(  // this is the 'html' array but we specify the 'ajax' element
         'ajax'=>array(
           'type'=>'POST',
           'url'=>"js:$(this).attr('href')", // ajax post will use 'url' specified above
           'success'=>"function(){\$.fn.yiiGridView.update('$gridId');}",
         ),
       ),
     ),
   ),
  ),
),


Seems to work like a charm. I was a bit amazed I couldn't find a straight answer right away which might indicate I'm doing something stupid here...

further reference on how for instance the delete button js code is generated can be found on github (checkout) /yiisoft/yii/blob/master/framework/zii/widgets/grid/CButtonColumn.php, line 266 and down.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users