I have a view that was created by Gii. As far as I understand, clicking the delete button in the grid should bring up a confirmation dialog and then send a POST request. However when I try it, I don’t get a confirmation and a GET request is sent.
Usually CGridView will write a javascript function for "delete" button like the following:
$(document).on('click','#your-grid-id a.delete',function() {
if(!confirm('Are you sure you want to delete this item?')) return false;
var th=this;
var afterDelete=function(){};
$.fn.yiiGridView.update('your-gird-id', {
type:'POST',
url:$(this).attr('href'),
data:{ 'YII_CSRF_TOKEN':'99e5ffb865cbe9186e92e0f65340c05078e25471' },
success:function(data) {
$.fn.yiiGridView.update('your-grid-id');
afterDelete(th,true,data);
},
error:function(XHR) {
return afterDelete(th,false,XHR);
}
});
return false;
});
Do you have something like this in your HTML output?
If yes, then by using the developer’s tool of your browser, you can set a break point in the first line of this function, and examine what’s happening.
I guess the chances are, as Keith suggests, there’s a javascript error in your code somewhere else other than this function.
I suppose that wouldn’t get hit when clicking the button as it’s just run once at the initial page setup, to attach the event.
Try writing your function in the same way as softark, as an anonymous function, rather than the serialized text that you’ve created. I imagine you could then set a break point within that function to determine if it was fired.
I just noticed something very strange in your code above… the line where the click event is binded for the delete does have "\n\t" in it. This should be a newline and a tab and not a literal "\n\t"
But the corrupt portion of the scripts should have been generated by Yii … maybe by CGridView or CButtonColumn. The corruption should not happen usually.
As softark said, the whole code is generated by Gii, didn’t write any of it myself.
I’m using Yii 1.1.12 and the “Yii Bootstrap 1.0” extension. Don’t think the later has any say in this though, the “bootstrap.widgets.TbGridView” and “zii.widgets.grid.CGridView” generate the exact same code (as posted above).
Any ideas on where to look for what’s going wrong? Don’t have a lot of experience with JS.
I had a look at the source for CButtonColumn. There is a function "registerClientScript", looks like this:
/**
* Registers the client scripts for the button column.
*/
protected function registerClientScript()
{
$js=array();
foreach($this->buttons as $id=>$button)
{
if(isset($button['click']))
{
$function=CJavaScript::encode($button['click']);
$class=preg_replace('/\s+/','.',$button['options']['class']);
$js[]="$(document).on('click','#{$this->grid->id} a.{$class}',$function);";
}
}
if($js!==array())
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, implode("\n",$js));
}
The line “$function=CJavaScript::encode($button[‘click’]);” causes the “messed up” result. When I change it to “$function=$button[‘click’];”, everything works like a charm with the delete buttons fully functional.
/**
* Registers the client scripts for the button column.
*/
protected function registerClientScript()
{
$js=array();
foreach($this->buttons as $id=>$button)
{
if(isset($button['click']))
{
//$function=CJavaScript::encode($button['click']);
$function=$button['click'];
$class=preg_replace('/\s+/','.',$button['options']['class']);
$js[]="$(document).on('click','#{$this->grid->id} a.{$class}',$function);";
}
}
if($js!==array())
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, implode("\n",$js));
}
That call is encoding a PHP variable value into javascript rapresentation so it should not be removed… and it does not explain why this is happening only to you.
Can you please debug further to see what in the encode() method is causing that problem to you?
Ok this is embarrassing - for whatever reason my CJavaScript.php was still on Version 1.1.10, while all the other files had been updated to 1.1.12. Thus CButtonColumn used the CJavascriptExpression but that class was not recognised by CJavaScript.
It’s all working fine now, thanks for your help guys.