滚动时表不自动加载

时间:2015-07-30 22:45:37

标签: javascript jquery json django w2ui

我使用w2ui来显示django模型的表格。我没有一次加载所有元素,而是使用autoLoading一次加载100个元素。以下是该表的代码:

var config = {
    grid: {
        name: "grid",
        url: "retrieveData/",
        show: {
            footer:true,
            toolbar:true
        },
        header:  "List of RTNs",
        columns: [
            { field:"number", caption:"Number", size:"30%" },
            { field:"name", caption:"Name", size:"30%" },
            { field:"release", caption:"Release", size:"30%" }
        ]
    }
}
$(function() {
    $("#grid").w2grid(config.grid);
});

处理json请求的代码是通过django视图完成的,下面是代码:

@csrf_exempt
def retrieveData(request):
  cmd = request.POST.get("cmd", False)
  if cmd == "get-records":
    offset = int(request.POST.get("offset", False))
    limit = int(request.POST.get("limit", False))
    entries = Data.objects.all()[offset:limit+offset]
    json_list = {"status":"success"}
    records = []
    def notNone(x):
      if x != None and x != "": 
        return x.strftime("%Y-%m-%dT%H:%M:%S")  
      else: 
        return ""
    for entry in entries:
      records.append({ 
        "recid":entry.id,
        "number":entry.number,
        "name":entry.name,
        "release":entry.release,})
    total = len(records)
    json_list["total"] = total
    json_list["records"] = records
    return HttpResponse(json.dumps(json_list), content_type="application/json")

  else:
    json_list = {"status":"error"}
    json_list["message"] = "CMD: {0} is not recognized".format(cmd)  
    json_list["postData"] = request.GET
    return HttpResponse(json_dumps(json_list), content_type="application/json")

该表能够检索前100个元素,但是当我一直滚动到底部时,表格不会加载更多元素。而不是加载更多的元素,它什么都不做。我关闭了autoLoad,但这仍然没有做任何事情("加载更多"按钮没有出现)。我的桌子上有一千个元素。

没有报告任何错误,一切似乎都在工作,只是在我滚动时没有加载更多元素。

我在w2ui网站上关注以下示例: http://w2ui.com/web/demos/#!combo/combo-9

1 个答案:

答案 0 :(得分:0)

总计在行

设置的方式
json_list["total"] = total

错了。因为它说元素的总量是100,即使你有超过100个元素。 "总"用于表示您没有在json响应中发送的元素总数的元素总数。

Change the code to the following:

    @csrf_exempt
    def retrieveData(request):
      cmd = request.POST.get("cmd", False)
      if cmd == "get-records":
        offset = int(request.POST.get("offset", False))
        limit = int(request.POST.get("limit", False))
-->     entries = Data.objects.all()
-->     total = len(entries)
-->     entries = entries[offset:limit+offset]
        json_list = {"status":"success"}
        records = []
        def notNone(x):
          if x != None and x != "": 
        return x.strftime("%Y-%m-%dT%H:%M:%S")  
      else: 
        return ""
    for entry in entries:
      records.append({ 
        "recid":entry.id,
        "number":entry.number,
        "name":entry.name,
        "release":entry.release,})
    json_list["total"] = total
    json_list["records"] = records
    return HttpResponse(json.dumps(json_list), content_type="application/json")

  else:
    json_list = {"status":"error"}
    json_list["message"] = "CMD: {0} is not recognized".format(cmd)  
    json_list["postData"] = request.GET
    return HttpResponse(json_dumps(json_list), content_type="application/json")