我正在使用的部分是由Yii 1.4.x提供支持的Web应用程序的模块之一。我的索引视图文件中有一个CGridView。然后在里面,我有自定义按钮。其中一个按钮是弹出一个对话框。这里是为我所提到如何使用Yii 1.4.x中的CJuiDialog框内容修复此会话?
'see' => array(
'label' => 'View',
'url' => 'Yii::app()->controller->createUrl("myControllerName/view",array("id" => "$data->id"))',
'options' => array(
'ajax' => array(
'type' => 'POST',
'url' => "js:$(this).attr('href')",
'dataType' => "json",
'async' => false,
'success' => 'function(data){
$("#detail-dialog").dialog("open"); return false;
}',
'update' => '#detail'
)
)
),
这里说的自定义按钮的代码段,在我的视图文件的CGridView底部名为“视图”控制器
public function actionView(){
$gid = $_GET['id'];
$data = array();
if(Yii::app()->request->isAjaxRequest) {
$model = MyModelName::model()->findById($gid);
if (!empty($model) || !is_null($model)) {
$attributes = $model->getAttributes();
$result = MyModelName::model()->findByKey($attributes['key'], $attributes['new_key']);
if(!empty($result)){
foreach($result as $key => $val){
$data['model'][$key] = $val;
}
$data['code'] = self::CODE_AJAX_SUCCESS;
}
} else {
$data['code'] = self::CODE_AJAX_ERROR;
}
$_SESSION['ajaxresponse'] = json_encode($data);
echo json_encode($data);
exit;
}
控制器动作的片段,我有这个片段的CJuiDialog
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id' => 'detail-dialog',
'options' => array(
'title' => 'Edit Data',
'autoOpen' => false,
'modal' => false,
'width' => 600,
'height' => 500,
'close' => 'js:function(){
//nothing here
}'
),
)); ?>
$result = $_SESSION['ajaxresponse'];
$this->renderPartial('_view',array('viewajaxresponse' => $result));
$this->endWidget('zii.widgets.jui.CJuiDialog');
每当我点击CGridView一排,它弹出的CJuiDialog框,里面Ajax响应沿因为我从会话中呈现了部分内容。
现在问题出现了,比方说我关闭了CJuiDialog框,然后点击了CGridView中的第二行或另一行,对话框的内容仍保留着我单击的第一行,除非强制刷新浏览器在对话框中的数据改变之前两到三次。如何解决这个问题?
我甚至为了终止会话
$.ajax({
url: "'.Yii::app()->controller->createUrl("MyControllerName/destroysession", array("s"=>"ajaxresponse")).'",
type: "POST",
dataType: "json",
async: true,
success: function(data){
document.location.reload(true);
}
});
,然后在控制器动作
public function actionDestroysession(){
$name = !empty($_GET['s'])?$_GET['s']:'';
if(isset($name)){
unset($_SESSION[$name]);
$data['code'] = self::CODE_AJAX_SUCCESS;
echo json_encode($data);
exit;
}
echo $data['code'] = self::CODE_AJAX_ERROR;
exit;
}
问题这次实施的CJuiDialog箱的“关闭”事件的AJAX功能即使你在函数中看到,它也会完全终止会话,因为我传递了名称,所以我只杀掉了一个特定的会话变量,但是当我单击CGridView中的任何行时,CJuiDialog框中没有显示任何数据。那么如何解决这个会话问题呢?