Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get total Amount of the column in AngularJs

Tags:

c#

angularjs

I am now working on a project wherein I will need to get the sum of a certain column in a modal table. Yes, it calculates the amount but the decimal point is not included. (e.g I am expecting that the result will be 194,000.26 but the result shows only 193,000.00) so it just means it didn't add the decimal point. Anyone who can help me with my codes ??

Here is my view :

 <tr data-ng-repeat="model in models  | orderBy: sorting:reverse | filter : filter ">

                    <td>{{jsonDatetotext(model.RequestDate) | date:'MM/dd/yyyy'}}</td>
                    <td>
                        <a href="#" data-toggle="modal" data-target="#basicModalContent" data-ng-click="getSelectedPR(model)">
                            {{model.RequestID}}
                        </a>
                    </td>
                    <td>{{model.Number }}</td>
                    <td>{{model.ProgramName }}</td>
                    <td>{{model.FullName }}</td>
                    <td>{{model.PONo}}</td>
                    <td>{{StatusList[model.StatusID] | uppercase}}</td>
                    <td class="totalAmount"><span class="pull-right">{{model.TotalAmount | number:2}}</span>
                    </td>
                    <td>{{model.asadsf | lowercase}}</td>
                </tr>

            </table>
        </div>
        <!-- /.Modal Na ni -->

        <div class="modal fade" id="basicModalContent" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
            aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">

                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <div class="modal-body" id="exportablePRItems">
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
                                    <tr>

                                        <th>PR #
                                        </th>
                                        <th>Item Description
                                        </th>

                                        <th>Supplier 
                                        </th>

                                        <th>Account
                                        </th>

                                        <th>Currency
                                        </th>

                                        <th>Amount
                                        </th>

                                        <th>(USD) Amount
                                        </th>
                                    </tr>
                                </thead>

                                <tbody data-ng-repeat="selectedPR in selectedModal.ItemList">
                                    <tr>
                                        <td>{{selectedPR.RequestID}}</td>
                                        <td>{{selectedPR.PartDesc}}</td>
                                        <td>{{selectedPR.SupplierID }}</td>
                                        <td>{{selectedPR.AccountType}}</td>
                                        <td>{{selectedPR.CurrName }}</td>
                                        <td data-ng-model="amount" class="amount">{{selectedPR.Amount | number:2}}</td>
                                        <td>{{selectedPR.AmountUSD}}</td>
                                    </tr>

                                </tbody>
                                <tr>
                                    <td colspan="7"><b>Total Amount : </b>{{selectedModal.ItemList | sumbykey : 'Amount' | number:2}} </td>
                                </tr>
                            </table>
                        </div>
                    </div>

and here is my angular filter

PRApp.filter('sumbykey', function () {
            return function (data, key) {
                if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
                    return 0;
                }

                var sum = 0.00;
                for (var i = data.length - 1; i >= 0; i--) {
                    sum += parseInt(data[i][key]);
                }

                return sum;
            };

        });
like image 938
Anaiah Avatar asked Nov 09 '22 17:11

Anaiah


1 Answers

Maybe try changing parseInt to parseFloat in your filter?

like image 108
Jesus is Lord Avatar answered Nov 15 '22 08:11

Jesus is Lord