我在其中一个列中有一个带有ajaxlink的cgridview。 ajaxlink仅在第一页中正常工作。对于其他页面,无论我如何点击ajaxlink,它都会在第一页更新结果。
$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))',
),
),
),
),
));
控制器
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";
}
}
我错过了什么?与ajaxupdate
或afterajaxupdate
有什么关系?
答案 0 :(得分:2)
是的,你错过了afterAjaxUpdate()
。您加载网格,但不会更新事件。
我建议:
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',
),
在你的js中,你必须这样做:
function ajax_function(id){
$.ajax({
//your request here
//id is your parameter from grid
});
}
在这种情况下,您不需要在AjaxUpdate()之后进行操作并手动绑定事件。