(Original: http://lei3056.blog.163.com/blog/static/39532279201181105142902)
CGridview:
Array (
'Name' => 'remark ',
'Type' => 'Raw ',
'Value' => 'chtml: textField ("remark", $ data-> remark, array (
"Size" => 7,
// "Readonly" => "readonly ",
"Class" => "textfix ",
"Id" => "$ data-> id ",
))'
),
This paragraph is correct. Make up now.
View: unoutput
Yii: app ()-> clientscript-> registerScript ('posiiton ','
JQuery ("# person-grid. textfix"). live ("change", function () {// jquery's live method is actually a lot of JavaScript websites.
If (! Confirm ("are you sure you want to fix this data? ") Return false;
Var id = $ (this). attr ("id ");
Var val = $ (this). val ();
$. Ajax ({
Type: "POST ",
DataType: "json", // dataType (xml html script json jsonp text)
Data: {"id": id, "val": val}, // json data
Url: "'. Yii: app ()-> createUrl ("/admin/person/updateremark/").'", // address generated by YII
Success: function (json) {// json object is also obtained successfully
// $. Fn. yiiGridView. update ("ad-grid ");
Alert (json. val); // val attribute in the object
$ (This). val (json. val );
// Alert ("success ")
}
});
Return false;
});
');
Html: output the final result
JQuery ("# person-grid. textfix"). live ("change", function (){
If (! Confirm ("are you sure you want to fix this data? ") Return false;
Var id = $ (this). attr ("id ");
Var val = $ (this). val ();
$. Ajax ({
Type: "POST ",
DataType: "json ",
Data: {"id": id, "val": val },
Url: "/admin. php? R = admin/person/updateremark ",
Success: function (json ){
// $. Fn. yiiGridView. update ("ad-grid ");
Alert (json. val );
$ (This). val (json. val );
// Alert ("success ")
}
});
Return false;
});
Controller: receives json ajax requests
Public function actionUpdateRemark (){
If (Yii: app ()-> request-> isAjaxRequest) {// whether to request ajax
$ Id = (int) Yii: app ()-> request-> getParam ('id'); // getparam will get the get post variable, you can also receive json-processed variables.
$ Val = Yii: app ()-> request-> getParam ('val ');
$ Model = $ this-> loadModel ($ id );
$ Model-> remark = $ val;
If ($ model-> save ()){
Echo CJSON: encode (array ('val '=> $ model-> remark); // The Yii method processes the array into json data
}
}
}
My other article on CJSON
In addition, the editing are all in the UTF-8 environment, there is no garbled problem.
In JS data transmission under IE, JQUERY and YII are both doing well. If GET is used, there will be a problem in Chinese, and there will be no problem in JSON.