如何将获取的数据返回到我的视图?

时间:2012-10-01 07:34:56

标签: php ajax jquery yii

如何将获取的数据(在本例中为$arr)返回到我的视图 (我可以像在桌子上一样操作)。 如果我通过ajax处理我的控制器动作:

public function actionFetchregularload(){
            $criteria = new CDbCriteria;
            $criteria->limit = 15;
            $criteria->condition='semester = 2';
            $criteria->select = array('subjCode','subjDesc','lec','lab','units');
            $criteria->addSearchCondition('yrLvl', $_GET['yrlevel']);
            $data = CompeProspectusA::model()->findAll($criteria);
            $arr = array();
            foreach ($data as $item){
                    $arr[] = array(
                            'subjcode' => $item->subjCode,
                            'subjdesc' => $item->subjDesc,
                            'lab' => $item->lab,
                            'lec' => $item->lec,
                            'units' => $item->units,
                    );
            }
    }

1 个答案:

答案 0 :(得分:1)

使用您的表格或其他内容制作视图,并在renderPartial()中使用actionFetchregularload(),如下所示:

$this->renderPartial('_table', array('arr'=>$arr), false, true);

在主视图中,您可以将CHtml::ajaxLink与选项一起使用(其中“#contentdiv”是您的表格div的ID):

CHtml::ajaxLink(
        "Update table",
        Yii::app()->createUrl('controller/fetchregularload'),
        array(    //ajaxOptions
            'type' => 'POST',
            'beforeSend' => "function(request) {
                $('#contentdiv').addClass('loading');   //add .loading class while loading - you can add background image
            }",
            'success' => "function(data) {
                $('#contentdiv').removeClass('loading');
                document.getElementById('contentdiv').innerHTML=data;
            }",
        ),
        //htmlOptions
        array('href' => Yii::app()->createUrl('controller/fetchregularload')); 
    );