jquery - django datatables additional data from server to template -


so have implemented datatables plugin django server-side processing. looks little 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) q in query],                 "itotalrecords": total,                   ...additional params datatable... } 

then use json.dump serialize data , send so

s = json.dump(response) return httpresponse(s.read()) 

this might little wrong, that's because i'm not using actual code rather writing down memory. isn't important if there's mistakes here general idea... (also, use json because i'm running on django 1.5 simplejson deprecated).

so said, works fine. url page works main_view , datatables sends ajax calls datatables_view , ticks. want send additional data from server (and not to server). in, want add data used inside template. data has come datatables_view because relies on current query , should dynamic. add response:

    response = {"aadata": [(q.name, q.state, q.email) q in query],                 "itotalrecords": total,                  ...additional params datatable...,                 "smeow": query.filter(name='cat')[:20]} 

this example, point when change query (say filter search word or change ordering) smeow also change. , want access somehow on template. can't this:

{{ smeow }} 

but can't use parsejson because isn't documents receives, it's datatable receives , need use api access it, though can't find how that.

since i'd have button runs script on smeow values (and send different request) had @ tabletools plugin doesn't play server-side ("tabletools more focused on client-side" source).

i'm not shy using additional plugins though rather use default capabilities of datatables. appreciated!

i use datatables django, including server side processing. problem want achieve datatables making specific request url, , expecting data can update table.

you possibly implement custom fnserverdata function, extract smeow value , processing, , delegate fncallback datatables update table. however, allow use smeow values javascript, since processing happening client-side (so no django templating).

taking code example directly link above (and adding little, see comments):

$(document).ready(function() {     $('#example').datatable( {         "bprocessing": true,         "bserverside": true,         "sajaxsource": "../examples_support/server_processing.php",         "fnserverdata": function ( ssource, aodata, fncallback ) {             $.getjson( ssource, aodata, function (json) {                  var obj = json.parse(json);                 var smeow = obj['smeow'];                 // need smeow                 fncallback(json);             });         }     }); }); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -