Yii框架:从JavaScript视图中获取结果html

Yii框架:从JavaScript视图中获取结果html,javascript,php,jquery,html,yii,Javascript,Php,Jquery,Html,Yii,这是一种特殊情况,但我在yii应用程序中有一个简单的.php视图,它根据传递给它的参数显示一些html。我这样称呼它: echo $this->renderPartial('/comments/view', array('comment'=>$comment)); 我这样做了好几次,因为页面中可能有多条评论 这很好,但是,一旦有人发布了新的评论,我想在页面上动态显示它,而不需要重新加载它。因此,AJAX发挥了神奇的作用,并在完成后调用了刷新函数,该函数需要刷新显示注释的div的内容

这是一种特殊情况,但我在yii应用程序中有一个简单的.php视图,它根据传递给它的参数显示一些html。我这样称呼它:

echo $this->renderPartial('/comments/view', array('comment'=>$comment));
我这样做了好几次,因为页面中可能有多条评论

这很好,但是,一旦有人发布了新的评论,我想在页面上动态显示它,而不需要重新加载它。因此,AJAX发挥了神奇的作用,并在完成后调用了刷新函数,该函数需要刷新显示注释的div的内容:

    function refreshComments()
    {
        var content = $('#users_posts').html();
        var newContent = "<?php $this->renderPartial('/comments/view', array('comment'=>Comments::getLatestComment($model->id))); ?>";
        $('#users_posts').html(newContent + content);
    }
因为我能够从数据库中新插入的注释中获取信息。问题显示在屏幕上,因为我必须用大量的html来结束它,这也取决于getLatestComment返回的值。视图做得很好,但是,如何从中获得结果并将其填充到JavaScript变量中,以便正确更新div的HTML


或者其他以这种方式提出的建议也非常受欢迎

要实现你想做的事情,几乎没有什么方法

第一种方法是更正现有代码,如下所示:-

评论控制器:-

public function actionGetLatestComments($id){ //$id variable is the ID of the latest comment on the page
    if(isset($_POST['post_id'])){
        $post_id = $_POST['post_id'];
        $post = Post::model()->findByPk($post_id);
        if($post){
            //check for any new comments here using the $id variable passed
            //echo out all the HTML of new comment(s) here
        }
    }
}
//inside the getLatestComments function
echo CJSON::encode(array(/* here array of new comments if available */));
用户视图侧:-

//inside refreshComments function 
var id = $('#users_post div').first().attr('id');
$.post('/comments/getLatestComments/'+id, {post_id:POST_ID_HERE}, function(data){
    data = JSON.parse(data);
    $.each(data, function(id, value){
        $('#users_post').append("<div id='"+value.id+"'>"+value.comment+"</div>");
    });
});
在这一方面,你需要跟踪你的帖子下的所有评论,比如前

<div id='users_posts'>
    <div id='2'>
        ...comment with ID 2...
    </div>
    <div id='1'>
        ...comment with ID 1...
    </div>
</div>
在JS函数refreshComments()中使用此ID,如下所示:-

function refreshComments(){
    var id = $('#users_post div').last().attr('id');
    $.post('/comments/getLatestComments/'+id, {post_id:POST_ID_HERE}, function(data){
        $('#users_posts').append(data);
    });
}
使用此方法将增加服务器的开销,因为如果有新的注释可用,服务器将一次又一次地返回整个HTML

或者,您可以使用JSON数据减少每个请求的开销,如下所示:-

function refreshComments(){
    var id = $('#users_post div').last().attr('id');
    $.post('/comments/getLatestComments/'+id, {post_id:POST_ID_HERE}, function(data){
        $('#users_posts').append(data);
    });
}
评论控制器:-

public function actionGetLatestComments($id){ //$id variable is the ID of the latest comment on the page
    if(isset($_POST['post_id'])){
        $post_id = $_POST['post_id'];
        $post = Post::model()->findByPk($post_id);
        if($post){
            //check for any new comments here using the $id variable passed
            //echo out all the HTML of new comment(s) here
        }
    }
}
//inside the getLatestComments function
echo CJSON::encode(array(/* here array of new comments if available */));
在用户的视图侧:-

//inside refreshComments function 
var id = $('#users_post div').first().attr('id');
$.post('/comments/getLatestComments/'+id, {post_id:POST_ID_HERE}, function(data){
    data = JSON.parse(data);
    $.each(data, function(id, value){
        $('#users_post').append("<div id='"+value.id+"'>"+value.comment+"</div>");
    });
});
//函数内部的refreshComments
var id=$('#users_post div').first().attr('id');
$.post('/comments/getLatestComments/'+id,{post\u id:post\u id\u HERE},函数(数据){
data=JSON.parse(数据);
$.each(数据、函数(id、值){
$('#users_post')。追加(“+value.comment+”);
});
});
通过这种方式,您可以使用JSON获取所有最新的注释,并使其更加高效


我希望这能解决您的问题。

完美答案!!我也会给你的+1您的代码有很多地方做错了。同样关于Ajax实现,使用JSON将数据发送到视图,并使用Jquery/Mootools等库将数据附加到视图中。查看下面@parry的答案。