问题与阿贾克斯追加 [英] Issue with Ajax Appending

查看:142
本文介绍了问题与阿贾克斯追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释模型进行分页,我想评论加载更多评论在同一页上只要单击Next按钮。我有句话怎么去这样做的想法,但任何人都可以告知如何去这样做。我有一些code了。

有关注释部分,而不是使我觉得可能要寻找micropost及其ID找到合适的意见,追加,但我不知道如何去了解这个捆绑在一起。

分页JS

  $(函数(){
  $(#CommentPagin一)。生活(点击,函数(){
    $ .getScript(this.href);
    返回false;
  });
});
 

显示JS

  $(#CC)追加('<%= escape_javascript(渲染:部分=>中的用户/评论)%>');
 

注释部分

 < D​​IV ID ='意见'>
  <%的意见= micropost.comments.paginate(:per_page => 5,:页=> PARAMS [:页面])%GT;
  < D​​IV ID =CommentPagin>
  <跨度类=CommentArrowIcon>< / SPAN>
  &其中;%= will_paginate注释:page_links =>假:类=>中分页%>
  < / DIV>
<%=渲染用户/评论%>
< / DIV>
 

注释渲染栏目

 < D​​IV ID =CC>
<%的意见= micropost.comments.paginate(:per_page => 5,:页=> PARAMS [:页面])%GT;
&其中;%=呈现注释%GT;
< / DIV>
 

用户控制

 高清节目
    @user = User.find(PARAMS [:ID])
    @school = School.find(PARAMS [:ID])
    @comment = Comment.find(PARAMS [:ID])
    @micropost = Micropost.new
    @comment = Comment.new
    @comment = @ micropost.comments.build(PARAMS [:评论])
    @microposts = @ user.microposts.order('created_at DESC)分页(:per_page => 10:页=> PARAMS [:页面])。
      respond_to代码做|格式|
      的format.html
      format.js
     结束
  结束
 

解决方案

我有点生锈的护栏带,所以这是有些通用的答案。

我将加载从路由/动作呈现只是您的的下n评论发表评论描画部作为HTML

只是把它看成你在哪里,从自己的API请求资产并利用它们来更新页面。

分页JS

  / **
* jQuery的1.7+
*使用.delegate()的旧版本。
** /
$(#CommentPagin)。在('点击','A',函数(E){
    //从服务器获取数据 - 确保网址有PARAMS为per_page和页面。
    $。获得($(本).attr(HREF),功能(数据){
        //刷新客户端数据
        $(#CC)追加(数据)。
    });
});
 

I have a comment model that is paginated and I would like the comments to load more comments on the same page whenever the next button is clicked. I have somewhat of an idea of how to go about doing this but can anyone advise how to go about doing this. I have some code already.

For the comment section instead of render I think it may have to be looking for the micropost and its id to find the right comments to append but I am unsure about how to go about tying this all together.

Pagination JS

$(function() {
  $("#CommentPagin a").live("click", function() {
    $.getScript(this.href);
    return false;
  });
});

Show JS

$("#cc").append('<%= escape_javascript(render :partial => "users/comments" )%>');

Comment Section

<div id='comments'>
  <% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %>
  <div id="CommentPagin">
  <span class="CommentArrowIcon"></span>
  <%= will_paginate comments, :page_links => false , :class =>"pagination" %>
  </div>
<%= render 'users/comments' %>
</div>

Comment Rendering Section

<div id="cc">
<% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %>
<%= render comments %>
</div>

User Controller

  def show
    @user = User.find(params[:id])
    @school = School.find(params[:id])
    @comment = Comment.find(params[:id])
    @micropost = Micropost.new
    @comment = Comment.new
    @comment = @micropost.comments.build(params[:comment])
    @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 10, :page => params[:page])
      respond_to do |format|
      format.html
      format.js
     end
  end

解决方案

I´m a bit rusty with rails so this is somewhat generic answer.

I would load the next n comments from a route / action that renders just your Comment Rendering Section as HTML

Just think of it as you where requesting assets from your own API and using them to update the page.

Pagination JS

/**
* jQuery 1.7+
* use .delegate() for older versions.
**/
$("#CommentPagin").on('click', 'a', function(e){
    // Get data from server - make sure url has params for per_page and page.
    $.get($(this).attr('href'), function(data){
        // refresh client with data
        $("#cc").append(data);
    });
});

这篇关于问题与阿贾克斯追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆