使用JavaScript对django生成的表值求和
我有一个表,它由下面的django for循环填充。我添加了一些JavaScript来汇总/添加asset.by_item_weight列中的所有总计 由于某些原因,我没有得到totalCol td中返回的值 我将script标记放在了这个.html的底部,希望在运行JS之前,django生成的值会加载到td中 这可能是因为我采用了错误的方法,或者这使用JavaScript对django生成的表值求和,javascript,jquery,python,html,django,Javascript,Jquery,Python,Html,Django,我有一个表,它由下面的django for循环填充。我添加了一些JavaScript来汇总/添加asset.by_item_weight列中的所有总计 由于某些原因,我没有得到totalCol td中返回的值 我将script标记放在了这个.html的底部,希望在运行JS之前,django生成的值会加载到td中 这可能是因为我采用了错误的方法,或者这应该在其他地方 我尝试将script标记放在personal/header.html中,放在closing body标记之前,没有任何区别 任何帮助
应该在其他地方
我尝试将script标记放在personal/header.html中,放在closing body标记之前,没有任何区别
任何帮助都将不胜感激
{% extends "personal/header.html" %}
{% block content %}
<h1 class='text-center'>This is the full asset list not split by owner</h1></br>
<table id="sum_table" class="well table table-striped text-center">
<thead>
<tr class="text-center titlerow">
<td class="text-center">Asset ID:</td>
<td class="text-center">Asset Name:</td>
<td class="text-center">Asset Quantity:</td>
<td class="text-center">Asset Weight / kg:</td>
<td class="text-center">Total Weight / kg:</td>
<td class="text-center">Asset Owner:</td>
</tr>
</thead>
<tbody>
<tr class="text-center">
{% for asset in object_list %}
<td><a href="/sam/assets/{{ asset.id }}">{{ asset.id }}</></td>
<td>{{ asset.asset_name }}</td>
<td>{{ asset.asset_quantity }}</td>
<td>{{ asset.asset_weight }}</td>
<td class="rowDataSd">{{ asset.by_item_weight }}</td>
<td><a href="/sam/owners/">{{ asset.asset_owner }}</></td>
</tr>
{% endfor %}
<tr class="totalColumn">
<td class=""></td>
<td class=""></td>
<td class=""></td>
<td class=""></td>
<td class="totalCol">Total:</td>
<td class=""></td>
</tr>
</tbody>
</table>
<p class="text-center">{% include "sam/includes/backtosam.html" %}</p>
{% endblock %}
<script>
var totals=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
$(document).ready(function(){
var $dataRows=$("#sum_table tr:not('.totalColumn, .titlerow')");
$dataRows.each(function() {
$(this).find('.rowDataSd').each(function(i){
totals[i]+=parseInt( $(this).html());
});
});
$("#sum_table td.totalCol").each(function(i){
$(this).html("total:"+totals[i]);
});
});
</script>
{%extends“personal/header.html”%}
{%block content%}
这是未按所有者拆分的完整资产列表
资产ID:
资产名称:
资产数量:
资产重量/千克:
总重量/千克:
资产所有者:
{对象_列表%中的资产的百分比}
{{asset.id}
{{asset.asset_name}
{{asset.asset_quantity}
{{asset.asset_weight}}
{{asset.by_item_weight}
{{asset.asset_owner}
{%endfor%}
总数:
{%include”sam/includes/backtosam.html%}
{%endblock%}
风险值总计=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
$(文档).ready(函数(){
var$dataRows=$(“#sum_table tr:not('.totalColumn.titlerow'));
$dataRows.each(函数(){
$(this.find('.rowDataSd')。每个(函数(i){
总计[i]+=parseInt($(this.html());
});
});
$(“#sum_table td.totalCol”)。每个(函数(i){
$(this.html(“总计:+totals[i]);
});
});
- 为什么在只有一个表的情况下迭代
#sum_table td.totalCol
- 为什么要用一堆零来预先填充数组
- 为什么要使用数组呢
var total = 0;
$('#sum_table tr td.rowDataSd').each(function() {
total += parseInt($(this).text());
});
$('#sum_table td.totalCol').text("total: " + total);
- 正如Daniel Roseman所说,你为什么要在JS中这样做