0

I am using w2ui to display a table of a django model. Instead of loading all the elements at once I am using autoLoading to load a 100 elements at a time. Below is the code for the table:

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);
});

The code that handles the json request is done via a django view, below is the code for it:

@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")

The table is able to retrieve the first 100 elements, but when I scroll all the way to the bottom the table does not load more elements. Instead of loading more elements it does nothing. I turned off autoLoad, but this still didn't do anything (the "Load More" button did not appear). There are a thousand elements in my table.

There are no errors being reported, and everything seems to be working except that it is not loading more elements when I scroll.

I am following the example below from the w2ui site: http://w2ui.com/web/demos/#!combo/combo-9

abden003
  • 1,325
  • 7
  • 24
  • 48

1 Answers1

0

The way total is being set at line

json_list["total"] = total

is wrong. Because it is saying that the total amount of elements is 100, even though you have more than 100 elements. "total" is used to indicate the total amount of elements you have not the total amount of elements you are sending in the json response.

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")
abden003
  • 1,325
  • 7
  • 24
  • 48