So I have implemented the dataTables plugin with django server-side processing. Looks a little like this:
template:
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "/datatable/"
} );
} );
</script>
views:
def main_view(request):
return render_to_response('index.html')
def datatables_view(request):
start = request.GET['iDisplayStart']
length = request.GET['iDisplayLength']
query = myUser.objects.all()
total = query.count()
if request.GET.has_key('sSearch'):
# filtering...
query = query[start:start+length]
response = {"aaData": [(q.name, q.state, q.email) for q in query],
"iTotalRecords": total,
...additional params for dataTable... }
Then I use json.dump to serialize the data and send it like so
s = json.dump(response)
return HttpResponse(s.read())
this might be a little wrong, but that's because I'm not using my actual code but rather writing it down from memory. It isn't important if there's any mistakes here just get that this is the general idea... (also, I use json because I'm running this on django 1.5 where simplejson is deprecated).
So like I said, this works fine. The url for the page works with the main_view and the datatables sends its ajax calls to datatables_view and everything ticks. Now I want to send some additional data from the server (and not to the server). As in, I want to add some data to be used inside the template. This data has to come from the datatables_view because it relies on the current query and should be dynamic. say I add this to the response:
response = {"aaData": [(q.name, q.state, q.email) for q in query],
"iTotalRecords": total,
...additional params for dataTable...,
"sMeow": query.filter(name='cat')[:20]}
This is obviously an example, but the point is that when you change the query (say you filter by a search word or you change the ordering) then sMeow will also change. And then I want to access it somehow on the template. Obviously I can't just do this:
{{ sMeow }}
But I also can't use parseJSON because this isn't something that the documents receives, it's something that the datatable receives and I need to use the API to access it, though I can't find how to do that.
Since eventually I'd have a button that runs some script on the sMeow values (and send it to a different request) I had a look at the tabletools plugin but it doesn't really play well with server-side ("TableTools is more focused on the client-side" source).
I'm not shy from using additional plugins though I rather use the default capabilities of datatables. Any help would be appreciated!