0

I am trying to bind the sum of selected checkboxes from a table. I am almost there but I cannot figure out what I am doing wrong. The picture shows 2 selected boxes picture

you see the result of my code. I am open to suggestions if there is a better way of going about this.

  $http.get('/api/Products/').success(function (data, status) { $scope.productList = data; });
    $scope.selection = [];
    $scope.OrderAmount = []
    $scope.myTotal = 0;
    $scope.toggleSelection = function toggleSelection(ProductId) {
        var idx = $scope.selection.indexOf(ProductId);
        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        }
        else {
            $scope.selection.push(ProductId);
        }

        for (var i = 0; i < $scope.selection.length; i++) {
            var OrderProductId = $scope.selection[i]
            var data = Enumerable.From($scope.productList).Where("x => x.ProductId == '" + OrderProductId + "'").ToArray();

            $scope.OrderAmount.push(data[0].ProductPrice)

     // $scope.OrderAmount = ["8500", "8500"]

            for (var i = 0, len = $scope.OrderAmount.length; i < len; i++) {
                $scope.myTotal += $scope.OrderAmount[i][0];
            };
        };

        $scope.$watch('myTotal', function (value) {
            $scope.model.OrderAmount = value;
        });
    };

view

<table class="table">
    <th>Product</th>
    <th>Price</th>
    <tbody>
      <tr ng-repeat="model in products">
        <td>
           <div class="toggle-switch" data-ts-color="blue">
             <input id="{{model.ProductId}}" type="checkbox" hidden="hidden" ng-checked="selection.indexOf(model.ProductId) > -1" ng-click="toggleSelection(model.ProductId)">
            <label for="{{model.ProductId}}" class="ts-helper"></label>
           </div>
        </td>
        <td>{{model.ProductName}}</td>
        <td>{{model.ProductPrice}}</td>
       </tr>
     </tbody>
   </table>

  <div class="form-group">
            <input type="text" ng-model="model.OrderAmount" class="form-control fg-input">
   </div>

UPDATE to first answer pic

1 Answer 1

1

You are doing the data binding wrongly. The checked status should be bound using ng-model but not ng-checked. You can make this easy by using an attribute (in the example checked) inside model and then loop over products to calculate the sum.

<tr ng-repeat="model in products">
    <td>
       <div class="toggle-switch" data-ts-color="blue">
           <input id="{{model.ProductId}}" type="checkbox" hidden="hidden" ng-model="model.checked" ng-click="toggleSelection()">
           <label for="{{model.ProductId}}" class="ts-helper"></label>
       </div>
    </td>
    <td>{{model.ProductName}}</td>
    <td>{{model.ProductPrice}}</td>
</tr>

Controller:

$scope.toggleSelection = function() {
    var sum = 0;
    angular.forEach($scope.products, function(value){
        if (value.checked) sum += value.ProductPrice;
    });

    $scope.model.OrderAmount = sum;
}
Sign up to request clarification or add additional context in comments.

2 Comments

see added image. The 0 is being added to every ProductPrice and the result is not the sum, it is combing the numbers. thanks
It's a common problem in javascript - the + does string concatenation instead of addition. To fix that just add Number() around the subject, in this case Number(value.ProductPrice)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.