【问题标题】:Reload part of page, not whole page, once Ajax is successful. Laravel一旦 Ajax 成功,重新加载部分页面,而不是整个页面。拉拉维尔
【发布时间】:2015-03-01 10:28:32
【问题描述】:

我在我的项目中使用 Laravel 4。我有一个帖子的“新闻源”。每个帖子都可以评论。当用户在帖子上重新加载页面时,新的评论会显示在评论框中。我怎么不希望整个页面重新加载。因此,我可以使用 ajax 提交帖子。我提交表单并阻止页面刷新。但是,现在我不知道如何在不手动刷新页面的情况下让评论出现在帖子上?

这是我发布 cmets 的路线

Route::post('post/{id}/comment', ['as' => 'commentPost', 'uses' => 'CommentsController@postComment']);

将此评论发布到数据库的控制器

public function postComment()
 {
     extract(Input::only('user_id', 'resource_id', 'body'));
     $this->execute(new PostCommentCommand($user_id, $resource_id, $body));

     return Redirect::back();
 }

我使用命令总线最终将评论保存到数据库,如下所示:

public function leaveComment($user_id, $resource_id, $body)
{
    $comment = Comment::leavePostComment($resource_id, $body);

    User::findOrFail($user_id)->comments()->save($comment);

    return $comment;
}

评论模型:

public static function leavePostComment($resource_id, $body)
{
    return new static([
        'resource_id' => $resource_id,
        'body' => $body
    ]);
}

这是加载评论框的视图(包含属于该帖子的所有 cmets 和一个用于发布新 cmets 的类型框。当用户点击“输入/返回”时,我使用 JS 发布评论" 键):

<div class="comment-box-container">
<div class="comment-box">
@if ($type->comments)
    @foreach ($type->comments as $comment)

<div class="user-comment-box">

<div class="user-comment">
    <p class="comment">
    <!-- starts off with users name in blue followed by their comment-->
        <span class="tag-user"><a href="{{ route('profile', $comment->owner->id) }}">{{ $comment->owner->first_name }}&nbsp;{{ $comment->owner->last_name }}</a>&nbsp;</span>{{ $comment->body }}
    </p>
    <!-- Show when the user posted comments-->
    <div class="com-details">
    <div class="com-time-container">
        &nbsp;{{ $comment->created_at->diffForHumans() }} ·
    </div>
    </div>

  </div><!--user-comment end-->
</div><!--user-comment-box end-->
@endforeach
@endif

<!--type box-->
    <div class="type-comment">
        <div class="type-box">
        {{ Form::open(['data-remote', 'route' => ['commentPost', $id], 'class' => 'comments_create-form']) }}
            {{ Form::hidden('user_id', $currentUser->id) }}
            {{ Form::hidden($idType, $id) }}
            {{--{{ Form::hidden('user_id', $currentUser->id) }}--}}
            {{ Form::textarea('body', null, ['class' =>'type-box d-light-solid-bg', 'placeholder' => 'Write a comment...', 'rows' => '1']) }}
        {{ Form::close() }}
        </div><!--type-box end-->
    </div><!--type-comment-->


</div><!--comment-box end-->

这是我用于 ajax 的 Javascript

(function(){

$('form[data-remote]').on('submit', function(e){
    var form = $(this);
    var method = form.find('input[name="_method"]').val() || 'POST';
    var url = form.prop('action');

    $.ajax({
        type: method,
        url: url,
        data: form.serialize(),
        success: function() {

              ** I DON'T KNOW WHAT TO PUT HERE TO MAKE THE 
              COMMENT-BOX RELOAD AND DISPLAY THE NEWLY 
              POSTED COMMENT **

            }
    });

    e.preventDefault();
});
})();

请注意,我对 JavaScript 非常陌生,甚至对 Ajax 也很陌生。任何帮助将不胜感激!

【问题讨论】:

  • 在 ajax 调用的成功事件中,将一个变量添加到参数部分...类似于 success: function(data) .... 然后您在该变量中有服务器响应,从那里你可以只用 jQuery 来更新一个元素或者你希望如何将更改应用到你的页面。

标签: javascript php jquery ajax laravel


【解决方案1】:

类似的东西:

$.ajax({
    type: method,
    url: url,
    data: form.serialize(),
    success: function(data) {
        // use a temp wrapper element to workaround weak jQuery HTML parser
        var tmp = $('<div>');
        tmp.html(data);
        // update the comment box with the new one
        $('.comment-box').html(tmp.find('.comment-box').html());
        // update the form (so it eventually shows validations errors)
        // don't forget to empty it via PHP if the submission was
        // successful
        $('.type-box').html(tmp.find('.type-box').html());
        tmp.destroy();
    }
});

【讨论】:

  • 嗯,我仍然没有得到任何东西。我必须手动刷新页面才能显示新评论
  • Ops,我的示例代码中的类选择器错误。现在试试。顺便说一句,这只是一个概念证明:)
  • 是的,我明白了。仍然不适合我。可能是因为我在页面上有多个评论框吗?或者这甚至重要吗?
  • 如果他们共享同一个类,则可能。尝试给每个人一个唯一的 id 再试一次,并在你的选择器中使用它。还要检查你的 javascript 控制台。
  • 我不知道如何使用 id 使其工作,因为我将有数千个帖子,这是否意味着我必须重复此代码数千次?我不确定如何实现这一点。我基本上是在尝试制作与 facebook 类似的评论系统。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2017-11-26
  • 2013-03-02
  • 2014-02-16
  • 1970-01-01
  • 2011-12-14
相关资源
最近更新 更多