CGridView CbutonColumn自定义按钮单击不起作用


CGridView CbutonColumn Custom Button Click not working

我使用的是CGridView,其中有一个CButtonColumn,我为其定义了一个按钮。没有调用"click"js。URL会被调用并完成。我想向用户显示一个确认,但它永远不会出现。根据文档,"点击"是一个JS函数,可以在点击时调用,但它对我不起作用。

$this->widget('zii.widgets.grid.CGridView', array(
            'dataProvider'=>$model->search(),
            'filter'=>$model,
            'columns'=>array(
                    'id',
                    'name',
                    'question',
                    'created',                      
                    array(
                            'class'=>'CButtonColumn',
                            'header'=>'Reset',
                            'template'=>'{reset}',
                            'buttons'=>array(
                                    'reset'=>array(
                                            'label'=>'Reset',
                                            'click'=>'function(){alert("Are you sure?");}',
                                            'imageUrl'=>Yii::app()->request->baseUrl.'/images/reset.png',
                                            'url'=>'Yii::app()->createUrl("favs/reset", array("id"=>$data->id))',                                           
                                            'options'=>array(
                                                    'ajax'=>array(
                                                            'type'=>'POST',
                                                            'url'=>"js:$(this).attr('href')",                                                               
                                                    ),
                                            ),
                                    ),
                            ),                      
                    ),                      
            ),
    ));

这与Yii为您的代码生成的jQuery有关:

jQuery('#your-grid-id a.reset').live('click',function(){alert("Are you sure?");});
/* more jquery ... 
... 
...
*/
jQuery('body').undelegate('#yt1','click').delegate('#yt1','click',function(){jQuery.ajax({'type':'GET','url':$(this).attr('href'),'cache':false,'data':jQuery(this).parents("form").serialize()});return false;});
// similar jquery follows for the other rows of the grid view 

如您所见,<a>标记的单击事件处理程序首先是您使用click选项提供的警报函数
然后,稍后使用undelegate删除此单击事件处理程序,并使用delegate添加另一个处理程序,这是因为您添加了ajax选项
如果您删除ajax选项,您将看到对话框,并且您将看到取消删除。。。委托序列将从生成的代码中删除

为了实现您的功能,您可以做其他事情:

  1. 使用jquery的ajax的beforeSend选项显示确认对话框,alert不是确认,btw
  2. 使用jquery的ajax的success选项来更新视图,或者向用户显示重置完成的消息