2016-08-14 29 views
1

我正在用angular和php構建一個項目。我在我的數據庫中有一個「股票」表,我可以檢索表中的所有數據。我想計算總「數量」列並顯示總計,但我不知道該怎麼做。任何人都可以請幫忙?如何使用angular和php進行計算?

PHP:

<?php 
    header('Content-Type: text/html; charset=utf-8'); 
    $connection = mysqli_connect('localhost','root','','hamatkin'); 

    mysqli_query($connection,"SET character_set_client = utf8"); 
    mysqli_query($connection,"SET character_set_connection = utf8"); 
    mysqli_query($connection,"SET character_set_results = utf8"); 

    if(!$connection){ 
    die("couldnt connect".mysqli_error); 
    } 

    $query="SELECT `stock_id`,`product_name`,`description`,`quantity` FROM `stock`"; 
    $queryResult = $connection->query($query); 
    $queryResult2 = array(); 
    if($queryResult->num_rows>0){ 
    while($row = $queryResult->fetch_assoc()){ 
     $queryResult2[] = $row; 
    } 
    } 
    $queryResult3 = json_encode($queryResult2); 
    echo json_encode($queryResult3); 
?> 

HTML:

</div> 

    <div class="table-responsive"> 
    <table class="customer-list table table-striped"> 
     <thead> 
     <tr> 
      <!-- <th>#</th> --> 
      <th class="Column-Header">קוד מוצר</th> 
      <th class="Column-Header">שם מוצר</th> 
      <th class="Column-Header">תיאור מוצר</th> 
      <!-- <th class="Column-Header">כמות במלאי</th> 
      <th class="Column-Header">סה"כ</th> --> 


     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="item in stockReport" ng-init="setTotals(item)"> 
       <td>{{item.ProductName}}</td> 
       <td>{{item.quantity}}</td> 
      </tr> 

     <tfoot> 
      <tr class="bg-warning"> 
       <td>Totals</td> 
       <td>{{invoiceCount}}</td> 
       <td></td> 
      </tr> 

     </tfoot> 


     </tbody> 
    </table> 

    </div> 

控制器:

"use strict"; 
angular.module('dataSystem').controller('reportsCtrl', function ($scope, $route, $location, $http) { 
    $http({method:'GET', url:'api/reports-tab/stock-report.php/'}) 
     .then(function(response) { 
     var arr = JSON.parse(JSON.parse(response.data)); 
     $scope.v = arr; 


     }) 

     // This will log you the error code and trace, if there is an error. 

     .catch(function(err) { 
     console.log('err', err) 
     }); 


     $scope.setTotals = function(item){ 
      if (item){ 
       item.total = item.quantity * item.unitCost; 
       $scope.invoiceCount += item.quantity; 

      } 
     } 



}); 

回答

0

你可以在JS如下功能,

JS:

$scope.setTotals = function(item){ 
     if (item){ 
      item.total = item.quantity * item.unitCost; 
      $scope.invoiceCount += item.quantity; 

     } 
    } 

查看

<tr ng-repeat="item in items" ng-init="setTotals(item)"> 
      <td>{{item.ProductName}}</td> 
      <td>{{item.quantity}}</td>     
     </tr> 
    </tbody> 
    <tfoot> 
     <tr class="bg-warning"> 
      <td>Totals</td> 
      <td>{{invoiceCount}}</td> 
      <td></td>     
     </tr> 

Working App

+0

感謝小費,但遺憾它沒有工作:( – tanyaa

+0

@tanyaa什麼沒有奏效? – Sajeetharan

+0

我會編輯代碼,它不會做任何事情 – tanyaa