【问题标题】:Table not autoloading when scrolling滚动时表格不自动加载
【发布时间】:2015-07-30 22:45:37
【问题描述】:

我正在使用 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 个元素,但是当我一直滚动到底部时,表格不会加载更多元素。它没有加载更多元素,而是什么都不做。我关闭了自动加载,但这仍然没有做任何事情(“加载更多”按钮没有出现)。我的表中有上千个元素。

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

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

【问题讨论】:

    标签: javascript jquery json django w2ui


    【解决方案1】:

    total 设置在行的方式

    json_list["total"] = total
    

    错了。因为它说元素的总数是 100,即使你有超过 100 个元素。 "total" 用来表示你有的元素总数,而不是你在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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多