Using Angular JS to compare and display the changed value in a textbox from its previous value

This is the code I am working with

<body>
<div>
    <table ng-app='myApp' ng-controller="MainCtrl">
        <thead></thead>
        <tbody ng-repeat="prdElement in palletElement track by $index">
            <tr>
                <td>{{prdElement.name}}</td>
            </tr>
            <tr ng-repeat="data in prdElement.Data">
                <td>{{data.itemId}}</td>
                <td>{{data.shipmentId}}</td>
                <td>{{data.itemCode}}</td>
                <td>{{data.description}}</td>
                <td>{{data.handlingUnit}}</td>
                <td>{{data.weight}}</td>
                <td>{{data.class}}</td>
                <td>{{data.lenght}}</td>
                <td>{{data.width}}</td>
                <td>{{data.height}}</td>
                <td>{{data.flag}}</td>
                <td>
                    <input type="text" ng-model="data.quantity" placeholder=" Code" required />
                </td>
            </tr>
            <tr>
                <td>
                    <button ng-click="newPalletItem( prdElement,$event)">Submit</button>
                </td>
            </tr>
        
        </tbody>
    </table>
</div>
    (function () {
    angular.module('myApp', []).controller('MainCtrl', function ($scope) {

        var counter = 0;

        $scope.palletElement = [{
            name: 'Pallet 1',
            Data: [{
                name: 'item 1',
                itemId: '284307',
                shipmentId: 'eb44f690-c97a-40e3-be2a-0449559e171a',
                itemCode: '',
                description: 'Bicycle parts - frame',
                quantity: '31',
                handlingUnit: 'CTN',
                weight: '613.04',
                class: '',
                lenght: '102',
                width: '42',
                height: '61',
                flag: 'P'

            }, {
                name: 'item 2',
                itemId: '284308',
                shipmentId: 'eb44f690-c97a-40e3-be2a-0449559e171a',
                itemCode: '',
                description: 'Bicycle parts - fork',
                quantity: '22',
                handlingUnit: 'CTN',
                weight: '242.99',
                class: '',
                lenght: '75',
                width: '34',
                height: '18',
                flag: 'P'
            }]
        }]

        $scope.newPalletItem = function (palletElement, $event) {
            counter++;
            $scope.palletElement.push(palletElement);
        }

    });
}());

When a user changes the value in the last column textbox and clicks the submit button, I want to calculate [preloaded value in the textbox - (minus) current value in that textbox] and display it in the next row. Although I have managed to duplicate the entire data to the next row, I am unsure of how to achieve the rest. Can someone please provide a solution?

View Fiddle

Further details from Fiddle: In the first textbox, the current value is 31. If someone changes it to 10, when I duplicate the row below, I want the new textbox value to show 21 (which is 31-10).

Answer №1

Kindly refer to this link for further details: http://jsfiddle.net/8r8cxcup/

New Pallet Item Function:

$scope.newPalletItem = function (palletElement, $event) {
            var npalletElement = {};
            angular.copy(palletElement, npalletElement);
            counter++;

            angular.forEach(npalletElement.Data, function (row) {
                if (row.quantity != row.newquantity) {
                    row.quantity = row.quantity - row.newquantity;
                }

            });

            $scope.palletElement.push(npalletElement);
        };

    });

HTML Code:

<table ng-app='myApp' ng-controller="MainCtrl">
            <thead></thead>
            <tbody ng-repeat="prdElement in palletElement track by $index">
                <tr>
                    <td>{{prdElement.name}}</td>
                </tr>
                <tr ng-repeat="data in prdElement.Data" ng-init="data.newquantity  = data.quantity">
                    <td>{{data.itemId}}</td>
                    <td>{{data.shipmentId}}</td>
                    <td>{{data.itemCode}}</td>
                    <td>{{data.description}}</td>
                    <td>{{data.handlingUnit}}</td>
                    <td>{{data.weight}}</td>
                    <td>{{data.class}}</td>
                    <td>{{data.lenght}}</td>
                    <td>{{data.width}}</td>
                    <td>{{data.height}}</td>
                    <td>{{data.flag}}</td>
                    <td>
                        <input type="text" ng-model="data.newquantity" placeholder=" Code" required />
                    </td>
                </tr>
                <tr>
                    <td>
                        <button ng-click="newPalletItem( prdElement,$event)">Submit</button>
                    </td>
                </tr>
                
            </tbody>
        </table>

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

The AngularJS $rootscope $destroy event is not being triggered, resulting in timeouts not being cancelled

Below is the code snippet that I am currently working with: function initialize() { var defer = $q.defer(); var deferTimer = $q.defer(); var cancelTimeout = $timeout(function() { if (defer !== null) { ctrlr.setProcessingParameters('X ...

Incorporate a new item into an array within a JSON `document` using Node.js for every incoming request

I'm facing an issue with storing multiple sets of x, y coordinates in a file on the server side. Currently, when a user right clicks on an image on the frontend, the coordinates are sent to the node server via AJAX post request and saved as objects in ...

Automatically updating a database value in CodeIgniter after a countdown has expired

I am looking to automatically update a value in my MySQL database using CodeIgniter once a countdown timer reaches zero. However, I am struggling to figure out how to implement this. Here is an example: I have a database structured like this: [image lin ...

Quirks in TailwindCSS template literals behavior

Looking at this specific component: const SaleBadge = ({ textContent, badgeColor }) => { return ( <Badge className={`bg-${badgeColor}-200 hover:bg-${badgeColor}-300 animate-pulse align-middle ml-2`} variant="secondary"><Pe ...

What is the jQuery alternative for the classList property in vanilla JavaScript?

Currently, I am working on a collaborative project with two acquaintances. One of the requirements is to stick to either vanilla JavaScript selectors like document.getElementById("thisDiv"); or jQuery selectors such as $("#thisDiv"); to maintain consis ...

Creating a dynamic search bar using Ajax with support for multiple keywords

I recently attempted to create an ajax search bar for my website. It functions perfectly with a single keyword, however, I'm facing some difficulty when trying to make it work with two keywords. I thought about parsing the data in the input field, but ...

Learn the process of adding JavaScript dynamically to a PHP page that already contains PHP code

SOLVED IT <?php $initialPage = $_COOKIE["currentPage"];?> <script type="text/javascript"> var initialPageVal = <?php echo $initialPage; ?>; <?php echo base64_decode($js_code); ?> </script> where $js_code is the following cod ...

Enabling Bootstrap modal windows to seamlessly populate with AJAX content

I'm currently in the process of crafting a bootstrap modal that displays the outcome of an AJAX request. Below is my bootstrap code: {{--Bootstrap modal--}} <div id="exampleModal" class="modal" tabindex="-1" role="dialog"> <div class="m ...

`"Error: posts.map is not a function" - What steps can be taken to resolve this issue?`

While working on fetching data from my local Drupal instance in React JS, I encountered a situation where I needed to pass the data from Drupal using the JSON API module which sends the data in JSON format. You can view the JSON API data in the following i ...

Transferring information between controllers using AngularJS

I have two controllers and I am trying to send data from the first one to the second one without using a factory. module.controller('UpdatesController', function ($scope) { var update = {}; update.items = [{ title: 'Comp ...

Encountering a 415 Error while making an ajax call using formData

I am attempting to make an ajax call with formData that includes a list of image files and some strings. However, the call is not successful and I am receiving error 415. Here is the code: var file = picChooser.files[0]; var jobExecutionImagesContext = n ...

Exploring the applications of the `this` keyword within a jQuery-based JavaScript object

Recently, there has been a challenge in creating a JavaScript object with the specific structure outlined below: function colorDiv(div){ this.div=div; this.div.bind("click",this.changeColor) this.changeColor(){ this.div.css(" ...

What is the best way to send formatted date data to an API using react-datepicker?

I'm encountering an issue while attempting to send a date to my API using react-datepicker in conjunction with Formik to manage nested dates. Despite setting the dateFormat to yyyy/MM/dd, the value I receive is "2020-06-19T04:00:00.000Z". This value g ...

What kinds of data types does MongoDB support in JavaScript?

Regarding the mongodb node client, it allows insertion of JavaScript data using the following syntax: collection.insert({ alpha: 123, bravo: "hello", charlie: [1, 2, 3], }) I am curious to know if mongo supports newer JavaScript data types ...

Step-by-step guide on duplicating a div a set number of times

I am looking to replicate the entire div multiple times (e.g., on an A4 paper, top left corner, top right corner, bottom left corner, and bottom right corner). <script language="javascript" type='text/javascript'> function updateD ...

What is the best way to retrieve and display the highest selected element in AngularJS?

Is it possible to set a maximum limit for selected elements in Angular? I have a multiple selection element where users can choose multiple items, but I want to restrict them to selecting only three items. When a button is clicked, a pop-up appears for the ...

JavaScript Radio Buttons

Below are the different radiobuttons: Apple <input type="radio" id="one" name="apple" data-price="10" value="light"/> Light <input type="radio" id="two" name="apple" data-price="20" value="dark" /> Dark <input type="text" id="appleqty" name ...

At times, Chrome fails to load CSS files when requested

Whenever I visit this ASPX page, the stylesheets sometimes fail to load. There is no request made and even checking the network log in Chrome's debugger confirms that it didn't request or load from cache. Interestingly, all other resources such a ...

The Add/Remove button functionality is not functioning as expected for duplicated form fields when using jQuery

I am experiencing an issue with the functionality of the "Add another" and "Remove row" buttons in my form. I implemented code from a specific Stack Overflow answer (jquery clone form fields and increment id) to create a cloneable section, but only the ori ...

What steps can be taken to address issues with the counting system in a lootbox opening

I am currently developing a virtual loot box simulator and have encountered a challenging issue. My goal is to track the quantity of Normals, Legendaries, Epics, and Rares obtained by the user. However, I am facing a problem where instead of updating the c ...