jquery - How do you update a django template context variable after an AJAX call? -
i have table product shows information of group of products.
<table id="item_table" class="table table-sm table-hover table-bordered"> <thead class="thead-inverse"> <tr> <th colspan="2">date</th> <th colspan="6">product name</th> <th colspan="2">category</th> <th colspan="2">amount</th> </tr> </thead> <tbody> {% item in product_list %} <tr> <td colspan="2">{{ item.date }}</td> <td id="item_name_format" colspan="6">{{ item.name }}</td> {% if item.category_id %} <td id="item_name_format" colspan="2">{{ item.category_id.level1_desc }}</td> {% endif %} <td id="item_amt_format" colspan="2">${{ item.amount|intcomma }}</td> </tr> {% endfor %} </tbody> </table>
i using below ajax call update table.
$(document).ready(function(){ // submit post on submit $('.item_num').on('click', function(event){ event.preventdefault(); var item_num = $(this).attr('id'); update_item(item_num); }); function update_item(item_num) { console.log(item_num) // sanity check $.ajax({ type: 'get', url:'update_items', data: { 'item_num': item_num }, success: function(result){ console.log(result); ???$('item_table').product_list = result;??? }, ... more code
how update variable product_list 'result' ajax call?
this should update table right?
thanks
you cannot way. better way load part of html via ajax.
your ajax view:
def update_items(request): product_list = your_data return render(request, 'table_body.html', {'product_list':product_list})
your main html:
<tbody class="table_body"> {% include 'table_body.html' %} </tbody>
table_body.html:
{% item in product_list %} <tr> <td colspan="2">{{ item.date }}</td> <td id="item_name_format" colspan="6">{{ item.name }}</td> {% if item.category_id %} <td id="item_name_format" colspan="2">{{ item.category_id.level1_desc }}</td> {% endif %} <td id="item_amt_format" colspan="2">${{ item.amount|intcomma }}</td> </tr> {% endfor %}
your ajax this:
function update_item(item_num) { console.log(item_num) // sanity check $('.table_body').html('').load( "{% url 'update_items' %}?item_num=" + item_num ); // <--- code instead of $.ajax(lala)
you use load() here
Comments
Post a Comment