如何在Yii框架中将数组和嵌套数组从控制器传递到Javascript

How to pass arrays and nested arrays from Controller to Javascript in View in Yii framework

本文关键字:数组 控制器 Javascript 嵌套 Yii 框架      更新时间:2023-09-26

我有一些数组在我的控制器和一些嵌套数组里面是从数据库查询的结果。我想通过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);
                    });
                });
            }
        });
    }
});