【问题标题】:how to generate invoice in angularJs within ng-repeat?如何在 ng-repeat 中的 angularJs 中生成发票?
【发布时间】:2017-01-21 21:20:44
【问题描述】:

如果我放置按钮并使用 ng-click 事件进行显示(数据)功能,它只计算一种产品的答案,但我想要所有产品的答案,我能做什么。这是我的 ng-repeat 部分,它显示产品数据。

<tr ng-repeat="data in data" ng-init="show(data)">
         <td>{{data.product_name}}</td>    
         <td>{{data.type}}</td>
         <td>{{data.quantity}}</td>
         <td>{{data.sale_prize}}</td>
         <td>{{data.sale_prize*data.quantity}}</td>
         <td>{{data.discount}}</td>
</tr>

现在我要计算总价、折扣价、税价和所有总价 由后续部分显示。

<div id="invoice" hidden="hidden">
    <label>Sub Total:</label>
          <input type="text"  placeholder="sale price"  ng-value="data.sale_prize * data.quantity" readonly="true"/><br />

          <label >Tax(2%):</label>
          <input type="text" readonly="true"  ng-value="((data.sale_prize * data.quantity)*2)/100" placeholder="tax"/>
          <label >Discount(%):</label>
          <input type="text" readonly="true"  ng-value="((data.sale_prize * data.quantity)*data.discount)/100"  placeholder="discount(%)" /><br />
          <label>Total Price:</label>

          <input type="text"  ng-value="(((data.sale_prize * data.quantity)+((data.sale_prize * data.quantity)*2)/100)-((data.sale_prize * data.quantity)*data.discount)/100) "readonly="true" placeholder="Total price"/>
</div>

js部分

$scope.show = function(data)
{
      $("#invoice").fadeIn(2000);
      //what to do here for calculate answer for multiple product
}

现在,如果我想在我的 show(data) 函数中为所有产品使用这个答案,那该怎么办?那么,我的show(data) 函数是什么?

【问题讨论】:

  • 到目前为止您尝试过什么?你能分享你的jsfiddle吗
  • 对不起,我没有工作的 jsfiddle。
  • 但是这个 ng-value 只显示了一个产品的计算答案,但我想要多个产品,那该怎么办? @正在加载..
  • 计算也在 ng-repeat 中?你能告诉我你的 json 数据吗?
  • 这个show(data)函数是显示计算。

标签: php html angularjs


【解决方案1】:
   <table>
         <tr ng-repeat="data in data">
           <td>{{data.product_name}}</td>    
           <td>{{data.type}}</td>
           <td>{{data.quantity}}</td>
           <td>{{data.sale_prize}}</td>
           <td>{{data.sale_prize*data.quantity}}</td>
           <td>{{data.discount}}</td>
         </tr>
   </table>

   <button ng-click="showinvoice()">Show Invoice</button>

   <br/><Br/>

     <div id="invoice" style="display:none">
       <label>Sub Total:</label>
        <input type="text"  placeholder="sale price"  
         ng-value="invoice.total" readonly="true"/><br />

         <label >Tax(2%):</label>
         <input type="text" readonly="true"  
         ng-value="invoice.tax" placeholder="tax"/>

         <label >Discount(%):</label>
          <input type="text" readonly="true"  
         ng-value="invoice.discount" 
         placeholder="discount(%)" /><br />
            <label>Total Price:</label>

         <input type="text" 
         ng-value="invoice.total+invoice.tax-invoice.discount"readonly="true" placeholder="Total price"/>

         </div>

Js 会是这个样子:

  $scope.data = [
    {
  'product_name' : 'sample_one',
  'type' : 'sanple_type',
  'quantity' : 1,
  'sale_prize' : 5000,
  'discount' : 3

},
    {
  'product_name' : 'sample_two',
  'type' : 'sanple_type',
  'quantity' : 5,
  'sale_prize' : 5000,
  'discount' : 3

},
    {
  'product_name' : 'sample_three',
  'type' : 'sanple_type',
  'quantity' : 1,
  'sale_prize' : 5000,
  'discount' : 5

}
 ]

  $scope.invoice = {};
  var total = 0;
  var discount = 0;
  for (var i=0; i < $scope.data.length; i++) {
     total = total + $scope.data[i].sale_prize*$scope.data[i].quantity;
     discount = discount+$scope.data[i].discount;
  }
  $scope.invoice['total'] = total;
  $scope.invoice['tax'] = (total*2)/100;
  $scope.invoice['discount'] = (total*discount)/100;


  $scope.showinvoice = function () {
    $('#invoice').fadeIn()
 }

我正在分享一个工作演示。可能对你有帮助

演示:https://jsbin.com/gipilo/15/edit?html,js,output

【讨论】:

  • 你在静态中显示的内容,但在我的情况下,我正在从数据库@KrishCdbry 获取数据
  • 是的,我以示例数据向您展示了一个示例,但您可以从数据库中获取数据并将其分配给 $scope.data
  • 所以它会像 $http.get('some API link').success(function(data) { $scope.data = data; } 这取决于你的 API 响应。跨度>
  • 没有数据包含您需要一一显示的项目,发票是最终的发票,将通过计算所有数据项目生成。因此,您需要将数据库响应分配给 $scope.data,invoice 将计算所有数据项。
  • 我按照你说的做了,但我没有得到结果。@KrishCdbry
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 2023-03-19
  • 2015-11-10
相关资源
最近更新 更多