简体   繁体   中英

Extra Ajax on CGridView on Yii

I'm quite new with Yii and I'm dealing with the CGridView which is causing me troubles.

This CGridView is in a renderPartial view.

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ratesGrid',
'dataProvider'=>rates::model()->searchSameProperty($propertyid),
'columns'=>array(
    'name',
    'from',
    'to',
    'price',
    array( 
    'header'=>'html',
    'type'=>'raw',
    'value'=>'\'
       <a href="#" class="deleteRate btn btn-danger" data-rateid="\'.$data->id.\'">Delete Rate</a>
       \'', 
    ),
),));

Yii::app()->clientScript->registerScript('ratesdeletion', '
$("#ratesProperty").on("click", ".deleteRate", function(e){
    e.preventDefault();
    $.ajax({
        "url":"'.CController::createUrl('rates/delete').'",

            "type":"POST",
            "data":{
                "id":$(this).data("rateid"),
            },
            "success":function(data){
                $.fn.yiiGridView.update("ratesGrid");
            },

    });
});
', CClientScript::POS_END);

The good part is when I click the delete button, it makes the call to "rates/delete" with the correct id, but when "rates/delete" finishes its work, somehow another AJAX call is made (that I've never coded), please check the screenshot.

WebDev工具上的“网络”选项卡

So my questions are:

  1. Why Yii is creating this second ajax call that I've never created?

  2. How can I avoid this second ajax call?

EDIT:

I tried adding an anchor and then attach the ajax call in a click event and it keeps making the second call.

When you delete some items from a grid, first ajax call is for delete request to server and second request is for recreating the grid after your deletion. We can't avoid second ajax call while using ajax grid view like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM