Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript对django生成的表值求和_Javascript_Jquery_Python_Html_Django - Fatal编程技术网

使用JavaScript对django生成的表值求和

使用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标记之前,没有任何区别 任何帮助

我有一个表,它由下面的django for循环填充。我添加了一些JavaScript来汇总/添加asset.by_item_weight列中的所有总计

由于某些原因,我没有得到totalCol td中返回的值

我将script标记放在了这个.html的底部,希望在运行JS之前,django生成的值会加载到td中

这可能是因为我采用了错误的方法,或者这
应该在其他地方

我尝试将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中这样做

为什么要用JS进行计算?为什么不在您的视图中?嗨,Daniel,我曾尝试在视图中执行cal,但无法显示值。@M.Rob因此,要解决您无法在合适的上下文中处理的问题,您的解决方案是尝试在不合适的上下文中解决它,尽管您也无法管理它?注意问正确的问题。如果您尝试执行X,失败并尝试使用Y执行,但也未能执行Y。问如何做X,而不是如何做Y。嗨,安托万。一段时间以来,我一直在寻找解决这个问题的方法,我突然想到,也许我正试图用django做一些不合适或不合适的事情。我突然想到,我也许可以用一些JavaScript来解决这个问题。我在这两种语言中都没有足够的知识或经验来解决django或JavaScript中的问题,这就是我向更广泛的社区寻求建议的原因。我真的很感谢你花时间回复我的后记,你的观点的相关部分请Hi Antoine,why JS,绝望。我未能通过django找到合适的解决方案。我开始认为我试图在django实现的目标是不可能的,这就是为什么我试图用JS解决这个问题。显然,根据对这个问题的反应,JS不是前进的方向。谢谢你的回答,我感谢你抽出时间回答我的问题。