[英]How to pass arrays and nested arrays from Controller to Javascript in View in Yii framework
I have some array in my Controller and some nested arrays inside which is result of query from database. 我的控制器中有一些数组,而其中的一些嵌套数组是数据库查询的结果。 I want to pass this data to javascript by AJAX and update content on site when I check the checkbox.
我想通过AJAX将此数据传递给javascript,并在选中此复选框时更新网站上的内容。 Pls show me an example or give me some advice how to do it.
请给我看一个例子或给我一些建议。
Controller 控制者
public function actionTestResponse(){
$id = $_POST['id'];
$checkval = $_POST['checkval'];
$this->layout = 'newhome';
$criteria=new CDbCriteria();
if($id == 'checkFemale'){
$criteria->compare('sex', 'female');
} else if ($id == 'checkFemale'){
$criteria->compare('sex', 'male');
}
$items = User::model()->findAll($criteria);
//if($items==null||empty($items)){$items='NULL';}
//$response = array('id'=>$id, 'checkval'=>$checkval, array('data' => $items));
$response = array('id'=>$id, 'checkval'=>$checkval);
//$data = array('data'=>$items);
//$result = array_merge($response, $data);
//var_dump($result);
//die;
echo json_encode($response);
}
View 视图
<input type="checkbox" id="checkFemale" class="checktest">Female
<script type="text/javascript">
$(function() {
$(".checktest").click(function() {
var id = $(this).attr("id");
if($(this).is(':checked'))
{
var checkval = 1;
} else
{
var checkval = 0;
}
var string = 'id='+ id + '&checkval='+ checkval;
$.ajax({
type: "POST",
url: "testResponse",
data: string,
dataType: "json",
success: function(response){
if(response.checkval == 1)
{
$("#"+response.id).prop('checked', true);
} else
{
$("#"+response.id).removeAttr('checked');
}
}
});
return false;
});
});
</script>
Example of array 数组的例子
array(3) { ["id"]=> checkFemale ["checkval"]=> 0 ["data"]=> array(2) { [0]=> object(User)#60 (11) { ["_new":"CActiveRecord":private]=> bool(false) ["_attributes":"CActiveRecord":private]=> array(6) { ["id"]=> int(1) ["user_id"]=> int(46) ["sex"]=> string(6) "female" ["certificate"]=> int(1) ["date_added"]=> string(10) "2015-02-02" ["date_of_birth"]=> string(10) "2015-01-01" } ["_related":"CActiveRecord":private]=> array(0) { } ["_c":"CActiveRecord":private]=> NULL ["_pk":"CActiveRecord":private]=> int(1) ["_alias":"CActiveRecord":private]=> string(1) "t" ["_errors":"CModel":private]=> array(0) { } ["_validators":"CModel":private]=> NULL ["_scenario":"CModel":private]=> string(6) "update" ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } [1]=> object(User)#61 (11) { ["_new":"CActiveRecord":private]=> bool(false) ["_attributes":"CActiveRecord":private]=> array(6) { ["id"]=> int(3) ["user_id"]=> int(53) ["sex"]=> string(4) "male" ["certificate"]=> int(0) ["date_added"]=> string(10) "2015-02-02" ["date_of_birth"]=> string(10) "2013-06-08" } ["_related":"CActiveRecord":private]=> array(0) { } ["_c":"CActiveRecord":private]=> NULL ["_pk":"CActiveRecord":private]=> int(3) ["_alias":"CActiveRecord":private]=> string(1) "t" ["_errors":"CModel":private]=> array(0) { } ["_validators":"CModel":private]=> NULL ["_scenario":"CModel":private]=> string(6) "update" ["_e":"CComponent":private]=> NULL ["_m":"CComponent":private]=> NULL } } }
I solved it. 我解决了 Here is the solution.
这是解决方案。 Thank you for your comments.
谢谢您的意见。
Controller 控制者
public function actionTestResponse(){
$id = $_POST['id'];
$checkval = $_POST['checkval'];
$this->layout = 'newhome';
$criteria=new CDbCriteria();
if($id == 'checkFemale'){
$criteria->compare('sex', 'female');
} else if ($id == 'checkFemale'){
$criteria->compare('sex', 'male');
}
$items = User::model()->findAll($criteria);
$list = array();
foreach ( $items as $item ) {
$list[] = array(
'id' => $item->id,
'sex' => $item->sex,
);
}
$result = array('id'=>$id, 'checkval'=>$checkval, 'data' => $list);
echo json_encode($result);
}
View 视图
$.ajax({
type: "POST",
url: "testResponse",
data: string,
dataType: "json",
success: function(response){
$.each(response, function (item, value) {
if(item=="checkval"){
if(value == 1){
$("#"+response.id).prop('checked', true);
} else {
$("#"+response.id).removeAttr('checked');
}
}
else if(item=="data"){
$.each(value, function (i, v) {
$.each(v, function (it, va) {
console.log(it);
});
});
}
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.