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

Thank you for the details with the code, a quick follow question, how can we send this "value" back to the catalog client script on the catalog item. In the client controller code, console.log displays very well the MRVS content on every change, but how do I get this changed value of MRVS to be used in the catalog client script ?

 console.log(value);

Hello,

 

This is so helpfull but i have a question.

My "Total amount" don't take decimal in count.

 

For exemple, i have 10 computers at 189,50 euros each one.

In total amount, it write 1890 instead of 1895.

 

How can i correct this please?

 

Thanks in advance.

jamesmcwhinney
Giga Guru

Here is a more generic solution.

Generic Widget (Can re-use for any MRVS or other use cases potentially):

api.controller = function($scope) {
    /* widget controller */
    var c = this;
    $scope.$watch(function(){return MrvsValueMonitor();}, function(value) { MrvsValueChanged(value);});
};

 

Catalog Client Script (OnLoad) for specific use case:

function onLoad() {
	//Usual onload logic here...
}

//Create a function with named "MrvsValueMonitor" for the widget to monitor for changes
MrvsValueMonitor = function(){
	return g_form.getValue('catering_items');
};

//Create a function named "MrvsValueChanged " for the widget to fire when it detects changes.
MrvsValueChanged = function(value){
	alert('The value of the catering items MRVS has changed to: ' + value);
};

 

If anyone sees any major issues with this approach please let me know.

Thanks!