Have you cautiously attempted to fetch data using AJAX, and displaying it in a datatable works seamlessly, but the issue arises when attempting to search or sort within the table. It seems that upon doing so, the data is lost, necessitating a page reload. What could be causing this?
class MainGroup(models.Model):
admin = models.ForeignKey(User,on_delete=models.CASCADE)
main_type = models.CharField(max_length=40,unique=True)
date = models.DateTimeField(auto_now_add=True)
Here's a snippet of my views.py:
def list_maingroup(request):
lists = MainGroup.objects.all().order_by('-pk')
data = []
for obj in lists:
item = {
'id':obj.id,
'admin':obj.admin.username,
'main_type':obj.main_type,
'date':obj.date
}
data.append(item)
return JsonResponse({'data':data})
Exploring further into my templates...
Attempting serialization by adding this code:
def list_maingroup(request):
lists = MainGroup.objects.all().order_by('-pk')
list_serializers = serializers.serialize('json',lists)
return HttpResponse(list_serializers,content_type='application/json')
And utilizing this AJAX request:
Despite all efforts, while using datatable's search and sorting functionalities, the existing data is being lost, prompting the need for a page refresh to view it again. Is there any oversight on my end? Your insights would be greatly appreciated!