explorer
(Xdim11)
September 18, 2013, 6:39am
1
Hi,
I have a form in CGridView and implement ajax for submitting the form:
In my view:
Yii::app()->clientScript->registerScript('approve-item',
<<<JS
function ajaxApproveItem()
{
$(".approve-item").submit(function() {
frm = $(this);
frm.append('<input type="hidden" name="ajax" value="1">');
$.post(frm.attr('action'), frm.serialize(),
function(result) {
if (result.code==1) {
$('#data-grid').yiiGridView('update');
} else {
alert(result.message);
}
}
, "json");
return false;
});
}
JS
, CClientScript::POS_HEAD);
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'data-grid',
...
'afterAjaxUpdate' => 'ajaxApproveItem()',
...
'columns'=>array(
...
...
array(
'type'=>'raw',
'value'=> function($data, $row) {
return '<form class="approve-item" action="'.Yii::app()->createUrl("item/approveitem").'" method="POST">
<input type="hidden" name="id" value="'.$data->id.'" />
<input type="hidden" name="token" value="'.app()->request->getCsrfToken().'" />
<input type="image" src="'.Yii::app()->request->baseUrl.'/images/update.png'.'" />
</form>';
}
),
The problem is, it works at initial run ( when I submit the form, the ajax runs perfectly and the grid is refreshed by calling $(’#data-grid ’).yiiGridView(‘update’) ), but after that the ajax doesn’t work anymore (when I submit the form, it is submitted like no ajax event bound).
Could someone help me? Thanks.
Ankit_Modi
(Ankit Modi)
September 18, 2013, 7:10am
2
explorer:
Hi,
I have a form in CGridView and implement ajax for submitting the form:
In my view:
Yii::app()->clientScript->registerScript('approve-item',
<<<JS
function ajaxApproveItem()
{
$(".approve-item").submit(function() {
frm = $(this);
frm.append('<input type="hidden" name="ajax" value="1">');
$.post(frm.attr('action'), frm.serialize(),
function(result) {
if (result.code==1) {
$('#data-grid').yiiGridView('update');
} else {
alert(result.message);
}
}
, "json");
return false;
});
}
JS
, CClientScript::POS_HEAD);
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'data-grid',
...
'afterAjaxUpdate' => 'ajaxApproveItem()',
...
'columns'=>array(
...
...
array(
'type'=>'raw',
'value'=> function($data, $row) {
return '<form class="approve-item" action="'.Yii::app()->createUrl("item/approveitem").'" method="POST">
<input type="hidden" name="id" value="'.$data->id.'" />
<input type="hidden" name="token" value="'.app()->request->getCsrfToken().'" />
<input type="image" src="'.Yii::app()->request->baseUrl.'/images/update.png'.'" />
</form>';
}
),
The problem is, it works at initial run ( when I submit the form, the ajax runs perfectly and the grid is refreshed by calling $(’#data-grid ’).yiiGridView(‘update’) ), but after that the ajax doesn’t work anymore (when I submit the form, it is submitted like no ajax event bound).
Could someone help me? Thanks.
i think you can write something look like this
'afterAjaxUpdate'=>'function(){
ajaxApproveItem();
}',
Ankit_Modi
(Ankit Modi)
September 18, 2013, 9:21am
4
explorer:
It doesn’t work either…
ok so best way you can call a onclick function on update button.it’s so easy and it’s work perfectly.
explorer
(Xdim11)
September 18, 2013, 10:00am
5
At last I make it working… the problem is I need to write it like this:
'afterAjaxUpdate' => 'ajaxApproveItem', // without () at the end
and then I need to make initial call to the function at document ready state:
Yii::app()->clientScript->registerScript('approve-item',
"ajaxApproveItem();"
);
Damn…took the whole day to figure it out, had tried many possibilites and none was working… I guess it needs to be more clear in the documentation…