如何在Yii框架中将数组和嵌套数组从控制器传递到Javascript
How to pass arrays and nested arrays from Controller to Javascript in View in Yii framework
我有一些数组在我的控制器和一些嵌套数组里面是从数据库查询的结果。我想通过AJAX将此数据传递给javascript,并在选中复选框时更新站点上的内容。请给我举个例子或给我一些建议。
控制器 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);
}
<<p> 视图/strong> <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>
数组
示例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 } } }
我解决了。这就是解决方案。谢谢你的意见。
控制器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);
}
<<p> 视图/strong> $.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);
});
});
}
});
}
});
相关文章:
- 将Javascript数组发送到控制器ASP.NET MVC
- 将角度材质设计复选框绑定到控制器中的数组
- $scope$观察数组中的一个特定对象——Angular JS控制器
- 使用查询将对象数组发布到asp.net-mvc控制器操作的正确方法是什么
- 如何将json文件中的数据提取到对象数组中,并在两个控制器之间共享
- 在angularJS中使用模态窗口时,在控制器之间共享对象数组
- Ember.js数组控制器计算属性的复选框
- 使用工厂/服务在控制器AngularJS之间推送数组中的数据
- 如何将从jQuery创建的数组传递给控制器方法
- 作为语法的控制器没有将新的obj传递给数组
- 将选定值数组发送到控制器-MVC
- 如何在Angular中设置异步HTTP服务的回调,以便在不定义数组的情况下在控制器中填充数组
- 我们可以将控制器中的JSON数据(MM/DD/YYYY格式)与今天的's日期,并在数组中显示筛选列表
- ASP.NET MVC:2d数组在从javascript传递到MVC控制器操作后为null
- 使用双向绑定将对象从角度视图动态添加/创建到控制器的数组
- 如何在 Ember 中循环排序的数组控制器.js
- Ember - 绑定到数组控制器中的过滤列表,然后添加新记录
- Ember.js如何在没有 ember 数据的情况下从数组控制器中删除对象
- Ember.js(pre4)数组控制器未保持状态
- Emberjs数组控制器问题