如何将参数传递CGridView
给它的afterAjaxUpdate
功能?目前,我在 HTML 中手动设置了一些值(在隐藏字段中),然后从网格的函数中读取它们afterAjaxUpdate
。
这确实有效,但听起来并不专业。有什么建议吗?
这是我的“查看”代码示例:
<div style="width:<?php $asDialog==1?'700px':'800px' ?>;">
<?php
$appartmentUpdateLink = CJavaScript::encode($this->createUrl('appartment/update', array('id'=>'')));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'appartment-grid-milimeters-editable',
'dataProvider'=>$model->search3(),
'cssFile'=>Html::cssUrl('gridstyles'),
'columns'=>array(
array( //needed for keyboard arrow up/down binding scripts.
'id' => 'selectedIds',
'class' => 'CCheckBoxColumn',
'checkBoxHtmlOptions'=>array('tabindex'=>'-1'),
),
array(
'name'=>'number',
'class' => 'phaEditColumn2',
'actionUrl' => array('appartment/setAttribute', 'attribute'=>'number'),
'htmlOptions'=>array('width'=>'15px'),
),
array(
'name'=>'name',
'header'=>'Διαμ.',
'class' => 'phaEditColumn2',
'actionUrl' => array('appartment/setAttribute', 'attribute'=>'name'),
'htmlOptions'=>array('width'=>'25px'),
),
),
'afterAjaxUpdate' => 'function(id, data) {
var foundOnPage = false;
var erroredIdsString = $("#recentIdDialogForError").text();
var erroredIdsArray = erroredIdsString.split(",");
var errorsLength = erroredIdsArray.length;
//reset errored rows (red outline).
$("#"+id).children(".items").children("tbody").find("input.select-on-check").closest("tr").css("outline", "0px");
$("#"+id).children(".items").children("tbody").find("input.select-on-check").each(function () {
for(var i=0; i<errorsLength; i++){
if($(this).prop("value") == erroredIdsArray[i]){
$(this).closest("tr").css("outline", "1px solid red");
}
}
if($(this).prop("value") == $("#recentIdDialog").text()){ //return to page after normal create/update
foundOnPage = true;
$(this).closest("tr").addClass("selected");
$(this).prop("checked", true);
$(".appartmentUpdateButton").prop("disabled", false);
$(".appartmentUpdateButton").parent().attr({
href: '. $appartmentUpdateLink .'+$("#recentIdDialog").text()+"?returnPage=2",
});
} //if
else if($(this).prop("value") == $("#recentIdDialogForNext").text()){ //just updated record(s) inline at the editable grid
if(!$(this).closest("tr").is(":last-child")){
var nextTr = $(this).closest("tr").next();
foundOnPage = true;
$(nextTr).addClass("selected");
$(nextTr).find("input.select-on-check").prop("checked", true);
$(nextTr).find(".button-column a.update").trigger("click");
$(".appartmentUpdateButton").prop("disabled", false);
$(".appartmentUpdateButton").parent().attr({
href: '. $appartmentUpdateLink .'+$(nextTr).find("input.select-on-check").prop("value")+"?returnPage=2",
});
}else{ //it is the last child -- go add a new record!
$(".newButton").trigger("click");
}
}//else if
if(foundOnPage == false){
$(".appartmentUpdateButton").prop("disabled", true);
$(".appartmentUpdateButton").parent().removeAttr("href");
}
?>
</div>
<div id='recentIdDialog' style='display:none'></div>
<div id='recentIdDialogForNext' style='display:none'></div>
<div id='recentIdDialogForNextSoft' style='display:none'></div>
<div id='recentIdDialogForError' style='display:none'></div>
<?php echo CHtml::link(CHtml::button('Διόρθωση', array('disabled'=>'disabled', 'id'=>'appartmentUpdateButton2', 'class'=>'appartmentUpdateButton', 'style'=>'width:120px')),array('appartment/update', 'id'=>''));?>
和“视图”脚本之一:
function sendData3(myUrl, dataObject){
return $.ajax({
type: "POST",
cache: false,
url: myUrl,
data: {myData: dataObject},
success: function(answer){
var obj = $.parseJSON(answer);
var successArray = obj.success;
var failureArray = obj.failure;
var printableAnswer = "";
if(successArray.length > 0){
printableAnswer += obj.msgSuccess;
$("#recentIdDialog").html("");
$("#recentIdDialogForNext").html(successArray[successArray.length-1]);
$("#recentIdDialogForNextSoft").html("");
$("recentIdDialogForError").html("");
}
if(failureArray.length > 0){
printableAnswer += obj.msgFailure;
var errors = "";
$("recentIdDialogForError").html("");
$.each(obj.errors, function(key, val) {
errors += "<div class=\"flash-error\">" + val + "</div>";
});
printableAnswer += errors;
$("#recentIdDialog").html((obj.failure)[0]); //FIRST errored line of the grid remains selected.
$("#recentIdDialogForNext").html("");
$("#recentIdDialogForNextSoft").html("");
for(var j=0; j<failureArray.length; j++){
$("#recentIdDialogForError").append(failureArray[j] + ",");
}
}
$("#statusMsgDialog").html(printableAnswer);
$("#statusMsgDialog").show();
if(failureArray.length <= 0){
$("#statusMsgDialog").delay(7000).fadeOut("slow");
}
},
});
}
和控制器:
$dataObject = $_POST['myData'];
$item = $dataObject['item'];
$model=$this->loadModel($item);
$model->attributes = $dataObject;
$valid = $model->validate();
if($valid){
if($model->save()){
$answer = array(
'status'=>'success',
'id'=>$model->id,
'msg'=>"<div class='flash-success'>Ajax - success [".($model->name?$model->name:$model->id)."].</div>");
echo json_encode($answer);
Yii::app()->end();
}
}else{
$error = CActiveForm::validate($model);
$errorArray = json_decode($error, true);
$answer = array(
'status'=>'error',
'id'=>$model->id,
'msg'=>"<div class='flash-error'>Ajax - error [".($model->name?$model->name:$model->id)."].</div>",
'errors'=>$errorArray);
echo json_encode($answer);
Yii::app()->end();
}