【问题标题】:How to calculate total price of column in angular 9?如何计算角度 9 中列的总价?
【发布时间】:2020-09-04 00:59:27
【问题描述】:

我要计算每列的总价、总数量和总计数(#)。

|项目 |价格 |数量 |

1 |鼠标 | 500 | 1 | 2 |线材 | 100 | 2 | TI:3 | |目标价:600 | TQ:3 |

我的 Js 无法获得所需的输出?帮我搞定

HTML

     <table class="table table-hover" *ngIf="secondTableData.length" id="sum_table">
        <thead>
         <tr class="table-primary" class="titlerow">
            <td>#</td>
            <td>Item</td>
            <td>Quantity</td>
            <td>Price</td>
            <td>Action</td>
        </tr>
      </thead>
     <tbody>
        <tr *ngFor="let newdata of secondTableData let i=index;">
            <td class="netSn">{{i+1}}</td>
            <td>{{ newdata.itemName }}</td>
            <td class="netQt">{{ newdata.Quantity }}</td>
            <td class="netSum">{{ newdata.retailRate }}</td>
            
        </tr>
        <tr class="totalColumn">
            <td class="totalCol">TI:</td>  <!--I want to Diaplay Total counted #  -->
            <td></td>
            <td class="totalCol">TQ:</td>  <!--I want to Display Total Quantity  -->
            <td class="totalCol">TP:</td>  <!--I want to  Display Total Price  -->
            
        </tr>
      </tbody>
    </table>

Js:

    $(document).ready(function () {

        var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");

        $dataRows.each(function () {
            $(this).find('.netSn').each(function (i) {
                totals[i] += parseInt($(this).html());
            });

            $(this).find('.netQt').each(function (i) {
                totals[i] += parseInt($(this).html());
            });

            $(this).find('.netSum').each(function (i) {
                totals[i] += parseInt($(this).html());
            });
        });
        $("#sum_table td.totalCol").each(function (i) {
            $(this).html("" + totals[i]);
        });

    });

Component.ts:

    //To Copy data of First table to Second table after clicking first table
  secondTableData = [];
  updateSecondTable(data) {
    let foundItem = this.secondTableData.find((item) => item.itemName === data.itemName);
    if (foundItem) {
      foundItem.Quantity += 1;
      foundItem.retailRate += data.retailRate;
      return;
    }
    this.secondTableData.push({
      itemName: data.itemName,
      Quantity: 1,
      retailRate: data.retailRate,
    })
  }

【问题讨论】:

  • 要么更新模型,要么创建一个新模型并在内部组件代码隐藏文件(打字稿文件)中进行计算。然后在模板代码中访问模型的这些新属性。
  • 您可以在 tableData 上使用 reduce 来创建具有累积数据的对象。
  • 第一列应该输出什么。我认为这是第二张表中的索引,对吗?无论数量多少,都选择了多少个项目!
  • 第一列应该是表格中项目的总行。

标签: javascript html jquery angular


【解决方案1】:

你可以像下面这样尝试

工作stackblitz

模板文件

<tbody>
  <tr *ngFor="let newdata of secondTableData">
    <td>{{ newdata.Index }}</td>
    <td>{{ newdata.Item }}</td>
    <td>{{ newdata.Quantity }}</td>
    <td>{{ newdata.Price }}</td>
  </tr>
  <tr>
    <td>TI : {{ secondTableData.length }}</td>
    <td></td>
    <td>TQ : {{ totalQuantity }}</td>
    <td>TP : {{ totalPrice }}</td>
  </tr>
</tbody>

打字稿文件

export class AppComponent {
  totalQuantity = 0; <-- A variable to hold quantity total
  totalPrice = 0; <-- A variable to hold price total

  updateSecondTable(data) {
    let foundItem = this.secondTableData.find((item) => item.Item === data.Item);
    if (foundItem) {
      foundItem.Quantity += 1;
      foundItem.Price += data.Price;
      this.getColumnTotal(); // <-- A call here
      return;
    }
    this.secondTableData.push({
      Index: this.secondTableData.length + 1,
      Item: data.Item,
      Quantity: 1,
      Price: data.Price,
    })
    this.getColumnTotal(); // <-- A call here
  }

  // Here is the logic
  getColumnTotal() {
    const { Quantity, Price } = this.secondTableData.reduce((acc, item) => {
      acc.Quantity += item.Quantity;
      acc.Price += item.Price;
      return acc;
    }, {
      Quantity: 0,
      Price: 0
    });
    this.totalQuantity = Quantity;
    this.totalPrice = Price;
  }
}

【讨论】:

  • 现在我相信你是 Angular 的专业人士。太谢谢你了兄弟。 :-) 。它的工作..
  • 很高兴它有帮助 :) 而这次你也投了赞成票 :)
猜你喜欢
  • 2019-03-26
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 2022-11-15
  • 1970-01-01
相关资源
最近更新 更多