How to calculate the total of variables into another variable from a multi-row variable set?

John Clyde Ap_a
Giga Expert

Is there a way to calculate the total for this? The quantity will still need to be multiplied based on the price per size of the drink and I wanted to store the total into another variable that is not inside the multi-row variable set.

find_real_file.png

Is there also a way so that every time they click add It'll update the total variable outside of the MRVS

find_real_file.png

 

 

1 ACCEPTED SOLUTION

John Clyde Ap_a
Giga Expert

Thread closed: 

Please refer to this link for displaying the data:  How to display data from a client script to an HTML widget and display its value in a text field.

In short, for the calculation and displaying of data. I used a custom with label variable and created a widget

find_real_file.png

with the following client script:

api.controller = function($scope) {
    /* widget controller */
    var c = this;
    $scope.$watch(function() {
        //Internal Name of Variable Set 'item_details'
        return $scope.page.g_form.getValue('choose_your_drink_s');
    }, function(value) {
        if (value) {
            var mrvsData = JSON.parse(value.replace(/'/g, ''));
            console.log(value);
            var totalCost = 0;

            for (var i = 0; i < mrvsData.length; i++) {
                var mrvs = mrvsData[i];
                totalCost += parseFloat(mrvs.pricePerSize) * parseFloat(mrvs.quantity);
                alert('Calculating: ' + totalCost + ' for --> ' + mrvs.pricePerSize);
            }
						
            //Set Value in the Total Cost
            alert(totalCost);
					 c.data.cost = totalCost;
           $scope.page.g_form.setValue('total_amount',totalCost);
						alert(c.data.cost);
        }
    });
};

and HTML:

<div>
<input type="text" value ={{c.data.cost}} readonly>
</div>

To achieve this:

find_real_file.png

 

~Melvin B.

View solution in original post

12 REPLIES 12

Hi @John Clyde Ap[as 

 

To show the total sum use the variable type Single Line Text instead of Custom with Label. Also, confirm if you are not receiving alerts as well.

Thanks,

Kartik Sethi

Hello Kartik,

I wanted to use a single line text type but I have created a custom widget for handling the calculation as per what others have said. How may I connect the widget to a single line text type? Is there a way for it? Also I have confirmed that the script works as the alerts are working and giving the correct values.

find_real_file.png

Hi @John Clyde Ap[as 

 

As per LOC#21, you should be able to set the value. Are you not getting the value? Also, confirm if you are getting the alert.

Thanks,

Kartik

Hello Kartik,

It is not setting the value on a single line text field nor a custom / custom mlabel variable, the alert works only if I use the custom label and connecting the widget to it. 

find_real_file.png

 

Here is the script of the widget.

find_real_file.png

 

Since I was basing it on the thread you were able to give an answer. I would just like to ask a question if the process is still the same?  How was this achieved?

find_real_file.png

I tried it on a single text field but unfortunately it still won't show the value nor are the alerts showing up. 

find_real_file.png

 

I apologize for the inconvenience. . .

 

John Clyde Ap_a
Giga Expert

Thread closed: 

Please refer to this link for displaying the data:  How to display data from a client script to an HTML widget and display its value in a text field.

In short, for the calculation and displaying of data. I used a custom with label variable and created a widget

find_real_file.png

with the following client script:

api.controller = function($scope) {
    /* widget controller */
    var c = this;
    $scope.$watch(function() {
        //Internal Name of Variable Set 'item_details'
        return $scope.page.g_form.getValue('choose_your_drink_s');
    }, function(value) {
        if (value) {
            var mrvsData = JSON.parse(value.replace(/'/g, ''));
            console.log(value);
            var totalCost = 0;

            for (var i = 0; i < mrvsData.length; i++) {
                var mrvs = mrvsData[i];
                totalCost += parseFloat(mrvs.pricePerSize) * parseFloat(mrvs.quantity);
                alert('Calculating: ' + totalCost + ' for --> ' + mrvs.pricePerSize);
            }
						
            //Set Value in the Total Cost
            alert(totalCost);
					 c.data.cost = totalCost;
           $scope.page.g_form.setValue('total_amount',totalCost);
						alert(c.data.cost);
        }
    });
};

and HTML:

<div>
<input type="text" value ={{c.data.cost}} readonly>
</div>

To achieve this:

find_real_file.png

 

~Melvin B.