None
None

Reputation: 5670

Unable to do mathematical calculations in angularJS

What I am trying to achieve is like this. There will be two columns one holds current quantity of products I have in my hand, next one is a pack-up column it will have a textbox with default 0 value, when I change value inside that textbox and press submit button I want that value to be subtracted from my first column(quantity) (which is done).subsequent to this i want to add up the value in textbox together in last column and go on, so that i can see how much items so far packed So far I have made something like this

<div ng-app='myApp' ng-controller="MainCtrl">
<div ng-repeat="prdElement in packageElement track by $index" class="package-grid">
    <table class="hovertable">
        <thead>
            <tr>
                <th>Line #</th>
                <th>Quantity in Plt</th>
                <th>Allready Packed</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="data in prdElement.Data" ng-init="data.newquantity  = 0">
                <td>{{data.itemId}}</td>
                <td>
                    <input type="text" ng-model="data.newquantity" placeholder="Quantity" required=required />
                </td>
                <td>{{data.packed}}</td>
            </tr>
            <tr>
                <td width="100%" colspan="4">
                    <button ng-show="prdElement.show" ng-click="newPackageItem( prdElement,$event)">Next Pallet</button>
                </td>
            </tr>
        </tbody>
    </table>
</div>

    (function () {
    angular.module('myApp', []).controller('MainCtrl', function ($scope) {
        var counter = 0;
        $scope.packageElement = [{
            name: counter,
            show: true,
            Data: [{
                name: 'item 1',
                itemId: '284307',
                quantity: '100',
                packed: 0

            }, {
                name: 'item 2',
                itemId: '284308',
                quantity: '200',
                packed: 0
            }]
        }];

        $scope.newPackageItem = function (packageElement, $event) {

            var npackageElement = {};
            angular.copy(packageElement, npackageElement);
            counter++;
            packageElement.show = false;

            npackageElement.name = counter;
            angular.forEach(npackageElement.Data, function (row) {
                if (row.quantity != row.newquantity || row.quantity != 0) {
                    row.quantity = row.quantity - row.newquantity;
                    row.packed = row.newquantity;
                }

            });

            npackageElement.show = true;
            angular.forEach(packageElement.Data, function (row) {
                row.packed = row.packed + row.newquantity;
            });
            $scope.packageElement.push(npackageElement);

        };
    });

}());

All done except when Quantity in Pltis added and pressed button i want that quantity to shown on last column (already packed) and follow up on subsequent rows. I have made an attempt as you can see here

 row.packed = row.packed + row.newquantity;

but addition does not works correctly, rather values concatenates. Fiddle

Upvotes: 0

Views: 79

Answers (2)

Pablo Ezequiel Leone
Pablo Ezequiel Leone

Reputation: 1387

Try

row.packed = parseInt(row.packed, 10) + parseInt(row.newquantity, 10);

Upvotes: 1

iJade
iJade

Reputation: 23811

Try

row.packed = Number(row.packed) + Number(row.newquantity);

Here is the fiddle

Upvotes: 1

Related Questions