【发布时间】:2016-05-11 23:57:54
【问题描述】:
我有一个表格 Product 显示一组产品的信息。
<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>
{% for 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>
我正在使用下面的 Ajax 调用你更新表。
$(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
如何使用 Ajax 调用中的“结果”更新变量 product_list?
这应该更新表格吧?
谢谢
【问题讨论】:
-
您必须序列化您的新产品列表项,将它们传递回您的 ajax 回调,然后迭代表体,通过 JS 为每个新项目创建一个新行。这当然是可能的,但@doniyors 的回答更聪明、更高效。
标签: jquery ajax django django-templates