rob rob - 7 months ago 79
Ajax Question

DataTables Next button not working

I have a DataTable being initialized in a HTML web page, rendered piecewise by ajax calls to a Django backend.

Everything is working great, however, the [Next] and [Last] datatable buttons are not working. Pagination works fine, Prev and First also work fine.

DataTable code:

var object_table = $('#object_table').DataTable( {
"sDom": '<"H"lfr>t<ip>',
"sPaginationType": "full_numbers",
"iOverlayFade": 100,
"processing": true,
"serverSide": true,
"ajax": '/objects/object_list/',
"deferRender": true
"aoColumnDefs": [ { "aTargets": [ 0 ], "sWidth": "1%",
"searchable":false, "orderable":false, }
"columns": function ( row, data, index ) {... }]

And this is what the code looks like as it's passed from django on the datatables ajax call

def dt_object_list_loader:
r_sequence = int(request.GET.get('draw', 0))
r_length = int(request.GET.get('length', 10))
r_start = int(request.GET.get('start', 0))
objects = djangomodel.objects.filter(...)
result_list = [objects[r_start:r_start + r_length]
response_data = {}
response_data['recordsTotal'] = len(result_list)
response_data['draw'] = int(request.GET.get('draw', 0))
response_data['data'] = result_list

return HttpResponse(jsonpickle.encode(response_data), content_type='application/json')

Pagination works fine, and everything loads without error. But 'next/last' are blurred out, and when I click them, django fires the error:

Internal Server Error: /mcontrol/task_list/
Traceback (most recent call last):
r_start = int(request.GET.get('start', 0))
ValueError: invalid literal for int() with base 10: 'NaN'

DataTables also fires a cryptic Error 7 (general ajax error) when the python error throws (this doesn't provide anything aside from what table the error occurred in.

Okay great, so I know when next or last is clicked, the r_start value provided from the datatables API is NaN. I could normally code around this by just setting the range manually, but I have no way of knowing from django which button was clicked (range for last or range for next) without doing some inane jquery.

This datatable is copied almost exactly from another part of my program and it works fine, I just changed some of the python code to spit out a different object I'm pretty confused as to why this is busting.

I guess my question is, how can I make datatables send a more useful value on 'next' aside from just passing NaN and why are the next/last buttons blurred out?


You need to return recordsFiltered parameter in addition to recordsTotal.

response_data['recordsTotal'] = objects.count()
response_data['recordsFiltered'] = objects.count()

Since you're not performing search recordsFiltered parameter value should be equal to recordsTotal.