北城已荒凉 2017-03-04 13:48 采纳率: 0%
浏览 25

如何汇总JSON总结果?

i have this script to take data from JSON Results, also this script filter results by ID

  $(document).ready(function () {
        //Call EmpDetails jsonResult Method
        $.getJSON("/smetkis/getTrosokList",
        function (json) {
            var tr;
              for (var i = 0; i < json.length; i++) {
                if (json[i].skID == '@Html.DisplayFor(model => model.smID)') {
                   tr = $('<tr />');
                    tr.append("<td >" + json[i].Artikal + "</td>");
                    tr.append("<td>" + json[i].kol + "</td>");
                    tr.append("<td>" + json[i].cena + "</td>");
                    tr.append($('<td class="vkupno1">' + json[i].vkupnot + '</td>'));
                    $('table').append(tr);
                }
            }
    });
    });

Also i have this script sor SUM Of column with class vkupno1, but not understand the results from cell.

 $(document).ready(function () {
    colSum();
});
function colSum() {
    var sum = 0;
        $(".vkupno1").each(function () {
        sum += parseFloat($(this).text());
    });
    $('#result').html((sum).toFixed(2).replace(/(\d)(?=(\d{6})+(?!\d))/g, "$1.") + " €");
}

How to sum (json[i].vkupnot)-value or cell classed "vkupno1"

here is JSON Results:

[{"smetki":null,"trId":1,"skID":1,"Artikal":"gdfgsdgfdg","kol":4.00,"cena":4.00,"vkupnot":16.00},{"smetki":null,"trId":4,"skID":4,"Artikal":"kjhkjhkjhk","kol":7.00,"cena":7.00,"vkupnot":47.00},{"smetki":null,"trId":5,"skID":4,"Artikal":"lkjlkjlk","kol":8.00,"cena":8.00,"vkupnot":64.00},{"smetki":null,"trId":6,"skID":5,"Artikal":"gdfg","kol":5.00,"cena":5.00,"vkupnot":25.00},{"smetki":null,"trId":7,"skID":6,"Artikal":"gdfg","kol":5.00,"cena":5.00,"vkupnot":25.00},{"smetki":null,"trId":8,"skID":7,"Artikal":"gagaggag","kol":5.00,"cena":55.00,"vkupnot":275.00},{"smetki":null,"trId":9,"skID":7,"Artikal":"ggg","kol":4.00,"cena":65.00,"vkupnot":260.00}]

  • 写回答

1条回答 默认 最新

  • bug^君 2017-03-04 13:57
    关注

    You can add the values from response itself without reading from DOM.

    Try this

    $(document).ready(function() {
      //Call EmpDetails jsonResult Method
      $.getJSON("/smetkis/getTrosokList",
        function(json) {
          var tr;
          var sum = 0; //initialising sum
          for (var i = 0; i < json.length; i++) {
            if (json[i].skID == '@Html.DisplayFor(model => model.smID)') {
              tr = $('<tr />');
              tr.append("<td >" + json[i].Artikal + "</td>");
              tr.append("<td>" + json[i].kol + "</td>");
              tr.append("<td>" + json[i].cena + "</td>");
              tr.append($('<td class="vkupno1">' + json[i].vkupnot + '</td>'));
              $('table').append(tr);
              sum += parseFloat(json[i].vkupnot); //adding directly from the response
            }
          }
          $('#result').html((sum).toFixed(2).replace(/(\d)(?=(\d{6})+(?!\d))/g, "$1.") + " €");
        });
    });
    

    And you need to wait for the response before reading the dynamically created DOM elements.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向