n3ISe
n3ISe

Reputation: 159

yii cgridview ajaxlink only works in first page

I had a cgridview with an ajaxlink in one of the column. The ajaxlink works fine in the first page only. For other pages, no matter how i click the ajaxlink it will update the result in first page.

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'promotion-facility-grid',
'ajaxUpdate'=>true,
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    array( 'name'=>'pf_branch', 'value'=>'$data->pfBranch->branch_name'),
    array( 'name'=>'pf_category', 'value'=>'$data->pfCategory->pc_name'),
    array( 'name'=>'pf_photo', 'type' => 'raw', 'value'=>'CHtml::image(Yii::app()->baseUrl . "/images/facility/" .$data->pf_photo, "", array("width"=>"150px"))'),
    'pf_name',
    array(
        'name'  => 'pf_main_view',
        'htmlOptions'=>array('style'=>'text-align: center'),
        'value' => 'CHtml::ajaxLink("<span id=\'MV$data->pf_id\'>$data->pf_main_view</span>", array("promotionFacility/Ajaxcontent", "id"=>$data["pf_id"]),array("update" => "#MV$data->pf_id"))',
        'type'  => 'raw',
    ),
    /*
    'pf_price',
    'pf_currency',
    'pf_link',
    'pf_status',
    */
    array(
        'class'=>'CButtonColumn',
        'template'=>'{update}{delete}',
        'buttons'=>array
        (
            'update' => array
            (
                'label'=>'update',
                'url'=>'Yii::app()->createUrl("promotionFacility/updatepanel", array("id"=>$data->pf_id,"view"=>$data->pf_view,"branch"=>$data->pf_branch))',
            ),
        ),
    ),
),

));

controller

public function actionAjaxcontent($id)
{
    include("js/databaseconnection.php");

    $query = "select * from holiday_promotion_facility where pf_id='$id'";
    $result= mysql_query($query) or die (mysql_error());
    while ($row=mysql_fetch_array($result))
    {
        $pf_main_view = $row['pf_main_view'];
    }

    if ($pf_main_view == "yes")
    {
        mysql_query("update holiday_promotion_facility set pf_main_view='no' where pf_id='$id'");
        echo "no";
    }
    else
    {
        mysql_query("update holiday_promotion_facility set pf_main_view='yes' where pf_id='$id'");
        echo "yes";
    }
}

what do I miss out? is it anything to do with ajaxupdate or afterajaxupdate?

Upvotes: 0

Views: 712

Answers (1)

ineersa
ineersa

Reputation: 3445

Yes you miss afterAjaxUpdate(). You loading grid, but does'nt update events.

I recommend to do:

array(
        'name'  => 'pf_main_view',
        'htmlOptions'=>array('style'=>'text-align: center'),
        'value' => function($data){
                       return CHtml::link("<span id='MV$data->pf_id'>$data->pf_main_view</span>","",array("onclick"=>"js:ajax_function(".$data->pf_id.")")),
        },
        'type'  => 'raw',


    ),

In your js you'll have to do:

function ajax_function(id){
    $.ajax({
    //your request here
    //id is your parameter from grid
    });
}

In this case you don't need to do afterAjaxUpdate() and bind events by hands.

Upvotes: 2

Related Questions