The actions of these custom buttons are called using get variables but I need to use ajax instead. Just for testing, I tried to mimic what the delete button does. I included in my custom button:
That is the same code that the CButtonColumn delete button uses, and I am calling the same delete function in my controller.
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
The problem is that when I try to use my custom delete button, the action throws the exception saying it is not a POST request.
So how could I make my button work so it calls the action using POST/Ajax ?
In that case, how can I make the same with my custom button? (make the ajax call for the action and refresh the grid, like the default delete button does)
Digging into the CButtonClass I found this (which I think it is the ajax call you mention):
The problem is that I don’t understand ajax. I imagine I have to write something similar using registerscript. Maybe someone who understands better how this ajax thing works could share the code to make the button call the function using ajax. Also, if there a link where I could learn more on how to use those strange ajax functions that start with “$.” I would really appreciate it.
Yes that is the PHP part that is a combination of PHP and javascript… but if you check the page source of the generated page with a default delete button, you will get there the resulting function completely in javascript / jQuery.
Of course the easiest thing would be to give you the code that works but as you say you don’t understand nor even know how or what it does, in my opinion it’s better for you to learn first a bit about jQuery so you can understand what that code does.
Try to find some basic jQuery tutorials so at lest you understand a bit, of course for this you would need to know at least the basics of javascript, too.
So, for what I understand what that js script does is to stop the link and instead call the function (in this case the .fn.yiiGridView.update() function). One of the things I do not understand is why ".fn.yiiGridView.update"? I am guessing that the function not only does the ajax call but updates the data in the grid. But how someone is supposed to find out the .fn.yiiGridView.update function name? I can not find it in the yii documentation (or maybe I am not looking in the right place).