Download all data loaded in Django-serverside Datatable
Download all data loaded in Django-serverside Datatable
standinibarra
Posts: 15Questions: 5Answers: 0
in DataTables
Hi people, I have this button to download all data stored in DataTable but somehow it only downloads what is shown in the current pagination. So if in Page 1 there's a 10 record and in Page 2 there is 5 records, If I clicked the button while in Page 2 it only downloads 5 records instead of 15 records. Help me out people.
here's my datatable settings:
var dTable = $('#dt_ScannedData').DataTable({
dom: 'lBfrtip',
"serverSide": true,
"ajax": { "url": "/dashboard/SS-ScannedData/" },
"columns": [
{ "data": "establishment" },
{ "data": "username" },
{ "data": "email" },
{
"data": null,
defaultContent: '',
render: function (data, type, row) {
let name = data["firstname"] + " " + data["middlename"] + " " + data["lastname"] + " " + data["suffix"]
name = name.trim();
return name;
}
},
{ "data": "timein_string" },
],
buttons: [
{
extend: 'excelHtml5',
exportOptions: {
columns: [0, 1, 2, 3, 4]
}
},
],
"lengthMenu": [[10, 25, 50, 100, 100000000], [10, 25, 50, 100, "All"]],
order: [[0, 'asc']],
responsive: true
});
here's the url I used in ajax:
path('SS-ScannedData/', ScanData, name='SS-ScannedData'),
here's my views.py:
@api_view(['GET'])
@permission_classes((permissions.AllowAny,))
def ScanData(request):
scandata = query_custom_scan_by_args(**request.query_params)
serializers = TaskSerializerScan(scandata['items'], many=True)
response = {
'draw': scandata['draw'],
'recordsTotal': scandata['total'],
'recordsFiltered': scandata['total'],
'data': serializers.data,
}
return Response(response)
def query_custom_scan_by_args(**kwargs):
draw = int(kwargs.get('draw')[0])
length = int(kwargs.get('length')[0])
start = int(kwargs.get('start')[0])
search_value = kwargs.get('search[value]')[0]
order_column = kwargs.get('order[0][column]')[0]
order = kwargs.get('order[0][dir]')[0]
order_column = SCAN_ORDER_COLUMN_CHOICES[order_column]
if order == 'desc':
order_column = '-' + order_column
queryset = ScannedData.objects.all()
if search_value:
queryset = queryset.filter(Q(id__icontains=search_value) |
Q(establishment__icontains=search_value) |
Q(username__icontains=search_value) |
Q(email__icontains=search_value) |
Q(suffix__icontains=search_value) |
Q(firstname__icontains=search_value) |
Q(middlename__icontains=search_value) |
Q(lastname__icontains=search_value) |
Q(timein_string__icontains=search_value))
total = queryset.count()
count = queryset.count()
queryset = queryset.order_by(order_column)[start:start + length]
return {
'items': queryset,
'count': count,
'total': total,
'draw': draw
}
class TaskSerializerScan(serializers.ModelSerializer):
class Meta:
model = ScannedData
fields ='__all__'
SCAN_ORDER_COLUMN_CHOICES = Choices(
('0', 'id'),
('1', 'establishment'),
('2', 'username'),
('3', 'email'),
('4', 'suffix'),
('5', 'firstname'),
('6', 'middlename'),
('7', 'lastname'),
('8', 'timein_string'),
)
SOS!!
This discussion has been closed.
Answers
Yep, that's because you've got
serverSide
enabled - so only the data displayed is available on the client. There are a few threads discussing that may help, see here and here,Colin