1

Hello I'm building a project with angular and php. I have a "select" option wich I can choose and it shows me all the "categories" I need and I need to select one. then it can calaculate the quantity of "products" when I pick a "category". but when I pick another "category" the total is no reset to 0 to count again it just counts more. can someone please help?

this is my code:

Html:

     <select ng-model="stockReport.selectedOption" 
ng-change="computeTotal()" ng-options = "item.category_name for item in stockReport |
            unique:'category_name'">

            <option value="">בחר קטגוריה</option>
            </select>



 <div class="table-responsive">
 <table class="customer-list table table-striped" >
            <thead>
                 <tr >
                     <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-if = "item.category_name == stockReport.selectedOption.category_name"
                  ng-init="setTotals(item)">
                     <td>{{item.category_name}}</td>
                     <td>{{item.stock_id}}</td>
                     <td>{{item.product_name}}</td>
                     <td>{{item.description}}</td>
                     <td>{{item.quantity}}</td>
                 </tr>
             </tbody>
             <tfoot>

                 <tr class="bg-warning">
                     <td><font size="6">סך הכל מוצרים במלאי:</font></td>
                     <td><font size="6">{{total}}</font></td>
                     <td></td>
                 </tr>
             </tfoot>
         </table>


 </div>

Controller function that calculate:

  $scope.total = 0;
      $scope.setTotals = function(item){
              // $scope.total = 0;
            if (item){


                 $scope.total += parseInt(item.quantity);
                 console.log($scope.total);
                 return $scope.total;


            }

        }
tanyaa
  • 155
  • 1
  • 12
  • **the total is no reset to 0 to count again it just counts more. can someone please help?** Can't get the meaning of this – Syam Pillai Aug 16 '16 at 16:45
  • @SyamPillai yes:) the total doesn't reset to - 0 each time I pick a category. it continues to count – tanyaa Aug 16 '16 at 16:47

2 Answers2

0

Seems like you want the sum of all the quantity of the table entry. Then you need to change the HTML code also

HTML:

<tbody>
             <tr ng-repeat="item in stockReport" ng-if = "item.category_name == stockReport.selectedOption.category_name"
              ng-init="setTotals(stockReport)">
                 <td>{{item.category_name}}</td>
                 <td>{{item.stock_id}}</td>
                 <td>{{item.product_name}}</td>
                 <td>{{item.description}}</td>
                 <td>{{item.quantity}}</td>
             </tr>
         </tbody>

OR

<select ng-options="......" ng-change="setTotals(stockReport)"></select> //this is better option and remove ng-init

JS:

$scope.setTotals = function(totalItem){
    $scope.total = 0;
     for(var item in totalItem){
       if (totalItem[item] && (totalItem[item].category_name == $scope.stockReport.selectedOption.category_name)){
         $scope.total += parseInt(totalItem[item].quantity);
         console.log($scope.total);
         return $scope.total;
       }
    }

}
Syam Pillai
  • 4,967
  • 2
  • 26
  • 42
  • than it calculates only one row(the last one) each time i pick a category. but yes it resets to -0 – tanyaa Aug 16 '16 at 16:46
  • So for each time you need to call the function twice?? ie., the final total will get after two iteration of the function?? Am I correct? – Syam Pillai Aug 16 '16 at 16:48
  • when I enter the page - total = 0:, I pick a category for example ("football") and I get 4 rows with team names and quantity of money each team has. then the total function will calculate how much money they all have. and when I choose another category "basketball", it will count the their money plus the total in the "football" category – tanyaa Aug 16 '16 at 16:51
  • So you don't want to add money of `football` with money of `basketball`. if i'm selecting `football`, it should show money of `football` only. like that for `basketball` also – Syam Pillai Aug 16 '16 at 16:57
  • This solution is bad: it uses ng-init, although it's clearly documented as "shouldn't be used". It computes the total for every item in the collection, although it doesn't need to be computed as many times. And it won't recomputed the total if there is no item in the collection after the category is changed. Use ng-change, as explained in my answer. – JB Nizet Aug 16 '16 at 17:08
  • Also, `for (var in ...)` is not how you should iterate on an array. – JB Nizet Aug 16 '16 at 17:10
  • But `for (var in ...)` should perfectly work. I'm sure – Syam Pillai Aug 16 '16 at 17:11
  • 1
    http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea – JB Nizet Aug 16 '16 at 17:16
  • @tanyaa Can you please provide your code in a jsFiddle?? – Syam Pillai Aug 16 '16 at 17:22
  • how ?I don't use jsFiddle – tanyaa Aug 16 '16 at 17:27
  • can we use the chat here? – tanyaa Aug 16 '16 at 17:27
  • Give more code of your javascript atleast. Basically the `$scope.stockReport` – Syam Pillai Aug 16 '16 at 17:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121086/discussion-between-tanyaa-and-syam-pillai). – tanyaa Aug 16 '16 at 17:32
0

ng-init should almost never be used. It's clearly said in the documentation.

If you want the total to be computed every time the selection changes, use ng-change:

<select ng-model="stockReport.selectedOption" ng-change="computeTotal()" ...>

Or, if the total computation is fast enough, just compute it every time you need it:

<td><font size="6">{{ computeTotal() }}</font></td>

The computeTotal function should look like this:

$scope.computeTotal = function() {
    var total = 0;
    $scope.stockReport.forEach(function(item) {
        if (item.category_name == stockReport.selectedOption.category_name) {
            total += parseInt(item.quantity)
        }
    });

    $scope.total = total;
    // or, if you choose the second option of computing it every time you need it:
    // return total;
}

Note that you shouldn't use <font>. Use CSS.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Then edit your question, and post the code you tried (HTML and JS) – JB Nizet Aug 16 '16 at 16:55
  • You haven't really read my answer. You're still using ng-init, although I started my answer by saying it should never be used. And you haven't even created a computeTotal() function: how could computeTotal() in the view do anything if the function doesn't even exist? I edited my answer to add the computeTotal() function, but really, you need to think about what you're doing. – JB Nizet Aug 16 '16 at 17:04
  • you didn't said to create a computeTotal() function. I have I function that I'm using for that. about the ng-init --> I forgot to delete it when I edtied here. I deleted in my code . thanks anyway for trying to help . I'm new with this and I'm sorry I didn't understand you completely. – tanyaa Aug 16 '16 at 17:08
  • That's extremely basic stuff. You can't expect a call a computeTotal() function in the view to work fine if that function doesn't even exist. I thought this was pretty obvious. – JB Nizet Aug 16 '16 at 17:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121084/discussion-between-tanyaa-and-jb-nizet). – tanyaa Aug 16 '16 at 17:17