0

I use jquery DataTables 1.10.0 to display some data on a table. This is how I load the data:

var layer_data = $("#layer_data").DataTable({
  "scrollX": true,
  "aaData":whole_array,
  "iDisplayLength": 10
});

Where the whole_array is a an array of arrays as:

[Array[7], Array[7], Array[7]

Each array has different values as:

 0:"<button id="1" class="btn btn-info btn-sm _edit_save_btn" style="margin-bottom:5px;width:68px; background-color:#a7a3a3;border-color:#a7a3a3">Edit</button></br><button id="fid__1" class="btn btn-info btn-sm _add_save_btn" data-toggle="modal" data-target="#add_geometry" data-fid="afghanistan_test.1" style="background-color:#a7a3a3; border-color:#a7a3a3;margin-bottom:5px;width:68px;">Geometry</button></br><button id="fid__cancel__1"  class="btn btn-info btn-sm _cancel_btn" style="margin-bottom:5px;width:68px; background-color:#a7a3a3;border-color:#a7a3a3">Cancel</button>"
 1:"Afghanistan"
 2:"Badakhshan"
 3:...

What I want to do is to redraw/refresh the table on a button click. I read there are ways to do this with ajax requests but I think this is not my case as the data come from a django view which is initialized on page load.

Community
  • 1
  • 1
user1919
  • 3,818
  • 17
  • 62
  • 97
  • Why not make an ajax request to (re)populate the table? – Sayse Sep 13 '16 at 09:31
  • #Sayse part of the data come from a django view and some other (like the html buttons) are build using jquery. How exactly would I repopulate the table using ajax? – user1919 Sep 13 '16 at 10:04

1 Answers1

1

You want to use something like JSONReponseMixin and have your view just send the data.

class MyView(JSONResponseMixin, AjaxResponseMixin , TemplateView):         
    def get_ajax(self, request, *args, **kwargs):
        data = ['Country', 'Country']
        return self.render_response(json.dumps(data))

When you you view from jquery ajax call it'll send back only the data and you generate the html.

You can also not do json and have the view return the html containing only the table and replace the html in your page.

Arnaud
  • 101
  • 5