Question

I am using django tables2 to generate my tables dynamically. I display a table using a custom format. Now on clicking a button in a table I need to update the data in the table to something else without refreshing the whole page. Is it possible that I send in an updated "table" variable from the server side and and just parse through the whole table again with that variable instead of picking up each and every element by hand using queries and then replacing the data inside them.

My code is like this:

{% block table %}
<!--table parsing done here -->
{% endblock %}

I found similar question at this link but there was no answer:

Rendering JSON objects using a Django template after an Ajax call

Was it helpful?

Solution

Do you have an id on the table? if so you do something like this:

$("table[id]").each(function(i,item){
  var $table = $(item)
  var table_id = $table.attr('id')
  $table.find(".refresh-button").on("click", function() {
    $table.load(location.href + " " + table_id);
  })
})

This uses jquery to reload the page in an ajax call, and then swap the refreshed table into the document where the old data used to be.

ALTERNATELY:

you can define a new url, that just returns the rendered table, and call this in the inner function.

$table.load("/_tables/my_table/ " + table_id);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top