Explore varying sum values for the final column using AngularJS

I cannot seem to obtain the total of the last column; instead, it shows the same total as the 3rd column.

Is there a way to specifically calculate the total for only the last column?

<body ng-app="Test">
  <section style="margin-top:80px">

    <h3>Plastic Calculator Form Version 2.0</h3>

    <div ng-controller="TestController as test" >
      <p>Currently, <strong><u># of individuals</u></strong> on Earth have pledged to lower their disposable plastic waste from <strong><u>{{ test.approve | sumByColumn: 'amount' }}</u></strong> Items annually to <strong><u>reduced amount</u></strong>. This promises a decrease of <strong><u>items reduced</u></strong> per year! Be part of the solution. Take the pledge!</p>
      <table class="table">
        <tr>
          <th>Disposable Plastic Items</th>
          <th>Enter Your Weekly Usage Number</th>
          <th>Your Annual Usage Number is:</th>
          <th>How Many Can You Reduce Annually?</th>
          <th>Reduced amount</th>
        </tr>

        <tr ng-repeat="x in test.approve">
          <td> {{ x.name }} </td>
          <td> <input class="qty form-control" type="number" ng-model="x.number" ng-change="sumByColumn3()" min="0" restrict-to="[0-9]"/> </td>
          <td ng-model="x.amount"> {{ x.number*x.amount }} </td>
          <td> <input class="qty form-control" type="number" ng-model="x.reducedAmount" ng-change="sumByColumn2()" min="0" restrict-to="[0-9]"/> </td>
          <td ng-model="x.reducedTotal"> {{ x.reducedAmount*x.reducedTotal }} </td>
        </tr>
        <tr>
          <td>TOTALS</td>
          <td>{{ test.approve | sumByColumn3: 'numeral' }}</td>
          <td>{{ test.approve | sumByColumn: 'amount' }}</td>
          <td>{{ test.approve | sumByColumn2: 'reducedAmount' }}</td>
          <td>{{ test.approve | sumByColumn4: 'reducedTotal' }}</td>
        </tr>
      </table>
    </div>
  </section>

</body>        

You can view the complete code here https://codepen.io/tampham/pen/RzqLQV

Answer №1

In the adjustColumnValues function, it's important to update:

item[column]*item.number

to

item[column]*item.updatedAmount

Furthermore, there is no necessity to utilize parseInt() in this context or in calculateTotal.

To ensure accuracy of the figures, I also suggest revising the column header "How Many Less Can You Use Per Year?" to read "How Many Less Can You Use Per Week?" for clarity.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered an issue with accessing the property 'path' of undefined in nodejs

Below is my server side code snippet: var fs = require('fs'), MongoClient = require('mongodb').MongoClient, db; var url = "mongodb://localhost:27017/fullwardrobedb"; MongoClient.connect(url, {native_parser: true}, function (err, connec ...

What are the benefits of utilizing the $timeout function in AngularJS over using window.setTimeout?

Recently, someone recommended incorporating a timeout in the following way: $timeout(function() { // Loading complete - Display message for an additional 3 seconds. $timeout(function() { $scope.showMessage = false; }, 3000); }, 200 ...

What is the best way to identify a particular subtype of HTMLElement based on its tag name and then save that element within another one?

I have a function that generates a node and returns it. Here is an example implementation: addElement: function ({ parentNode, elementName, tagName, }) { // Creates and appends the new node. parentNode[elementName] = document.createEl ...

Is there a way to tally up the number of green items and display a <p> tag when every item has been marked green?

I have created a checklist that includes checkboxes. I am looking to display some text when all the checkboxes are checked and green. Can someone assist me with writing the code for this functionality? $(document).ready(function() { $("i").click(funct ...

Unlocking the potential of AngularJS by circumventing the $http promises object management system

I have a service set up like this: angular.module('module') .factory('UserList', function ($http) { return {getUserList: $http.get('/portal-services/NemesysPortalBackend/rest/user/all')}; }); This forces me to use Use ...

Unlocking the secrets of retrieving the URL query string in Angular2

I'm struggling to extract the URL query string returned by the security API, resulting in a URL format like this: www.mysite.com/profile#code=xxxx&id_token=xxx. My goal is to retrieve the values of code and id_token. In my ngOnInit() function, I ...

Store the information retrieved from an Ajax call regarding an artist on the page, ensuring that it is only saved once for that

I have been working on a single-page application that utilizes the Spotify REST API and JQuery to enable users to search for an artist and display their information on the page. However, I want to ensure that the app saves details about each artist only ...

What is the method for determining the length of a JavaScript "array"?

Lately, we've been heavily using javascript arrays and hashes and trying to find a universal way to count the items in both without needing to differentiate between the two. The .length method has proved to be unreliable as it only returns the value o ...

Prevent event listeners from being triggered during the '$destroy' event. Error: $on function is undefined

As I attempt to halt all event listeners when the scope is destroyed, a certain error arises. The following error is displayed: TypeError: vm.$on is not a function; Even using vm.on(..) does not resolve the issue angular.module('app.layout&apo ...

What is the process of combining two separate req items to generate a fresh document in MongoDB?

Within my application, both req.body and req.user are available for use. I am attempting to utilize both of these to generate a new document in the system. However, it appears that the Model.create function can only accept one input parameter. What would ...

What steps are involved in configuring Meteor-Angular with Twitter Bootstrap?

I'm currently struggling with setting up Twitter Bootstrap in my Meteor-Angular project. Here's what I've done so far: Installed Bower meteor add mquandalle:bower Added a dependency to bower.json: { ... "dependencies": { "a ...

What is the best way to adjust the size of an image within a hyperlink using jQuery?

I'm trying to grab an image from a hyperlink and resize it for display to the user. I've written the code below, but it's not working as expected. Can anyone lend a hand? if ($(this).find("img").attr("src") != null) { va ...

What is the best way to determine the number of matching characters between two strings?

Currently, I am working on developing a function to determine the number of identical characters in two strings that are provided as input. let [string1, string2] = target.split(",").map(p => p.trim()); let [length, char1, char2] = ""; let total ...

Can you please explain the purpose of the mysterious JavaScript function f => f?

Currently, I am utilizing a third-party library that utilizes a function with functions as arguments. During my conditional checks, I determine whether to add a particular function as a parameter or not. However, providing null in these cases results in er ...

What is the best way to access the references of 'child controllers' in NavigatorIOS within a React Native app?

After diving into react-native for the past few days, I've encountered a dilemma regarding the NavigatorIOS component. Is there a way to access and manipulate 'child controllers' of NavigatorIOS similar to how it's done in native object ...

Retrieving information from React elements

Recently, I ventured into the world of React and started building a mock website for online food ordering. One of my components is called Items, which utilizes props to display all the food items on the webpage. import { useState } from "react"; ...

What could be causing the .hover function to malfunction and how can I make it so that the .hover function only applies within the corner radius area?

I am attempting to make circles react to my jquery .hover function. Below is the JavaScript code I am using: jQuery.fn.center = function () { this.css("position","absolute"); this.css("top", Math.max(0, (($(window).height() - this.outerHeight()) / 2) + ...

Encountering a problem when attempting to utilize AngularFire's Cloud Messaging Angular module due to missing configuration values for the app

Working with Firebase Cloud Messaging in my Angular application using AngularFire2 has presented a challenge. I am facing an error when attempting to retrieve the user's current token. I have already set up the "firebase-messaging-sw.js" and "manifes ...

Sending information to Bootstrap 4 modal

Can you help me insert the variable into this link? <a href="#edit_task" data-toggle="modal"><i class="fas fa-edit fa-lg fa-fw"></i></a> This is my modal: <div class="modal fade" id=" ...

repeating the content within a dynamic popover using AngularJS and UI-Bootstrap

I am attempting to display a list of items in a popover within an ng-repeat loop using AngularJS and the ui-bootstrap package (http://angular-ui.github.io/bootstrap/). HTML <div ng-repeat='session in sessions'> <p popover="{{sessi ...