weixin_33675507 2012-05-06 23:42 采纳率: 0%
浏览 33

创建ajax分页

I am trying to do an ajax pagination with the following code:

// AJAX pagination
$(".pages .prev").live('click', function(event) {
    event.preventDefault()
    var current_page = parseInt(getParameterByName('page'))-1;
    $.get('/ajax/financial_page/', {'page': current_page}, function(response) {
        $(".content table").replaceWith(response)
    });
})

And in my view function:

def financial_page(request):
    """
    Returns a single financials page, without extra HTML (used in AJAX calls).
    """
    page = int(request.GET.get('page', 1))
    if request.user.is_superuser:
        fs = FinancialStatements.objects.order_by('-date',  'statement_id')
    else:
        up = request.user.get_profile()
        providers = up.provider.all()
        fs = FinancialStatements.objects.filter(provider__in=providers).order_by('-date', 'statement_id')

    fs_objects, current_page_object, page_range = paginator(request, objects=fs, page=page, number_per_page=30)
    data = {  'fs':fs_objects, 
              'page_range': page_range, 
              'current_page': current_page_object,
           }
    page = render_to_string('financial_section.html', data, RequestContext(request))
    return HttpResponse(simplejson.dumps([page]))

However, there are two problems I'm running into. The first is that the response is not really HTML, and has a bunch of n\t\t \t\t \t \t \t \t\t \t\, etc. Also, I'm having trouble keeping track of the current page/changing the url as needed. How would I build a functional ajax pagination here?

Update: I figured out the first one, by doing response = $.parseJSON(response);. How would I keep track of which page I am on though?

  • 写回答

4条回答 默认 最新

  • weixin_33688840 2012-05-06 23:47
    关注

    Try using the javascript String.replace() method:

    // AJAX pagination
    $(".pages .prev").live('click', function(event) {
        event.preventDefault()
        var current_page = parseInt(getParameterByName('page'))-1;
        $.post('/ajax/financial_page/', {'page': current_page}, function(response) {
            response = response.replace(/
    /g,'<br>').replace(/\t/,'&nbsp;&nbsp;');
            $(".content table").replaceWith(response)
        });
    })
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题