Munlau Yau
Munlau Yau

Reputation: 21

Calculation doesn't work - flex

I have made a function to calculate a totalprice but it doenst seem to work. The function is made to get 2 datafields from a arraycollection with these the two datafields I want to be able to calculate.

        [Bindable]public var total:Number=0;
        private function gridClickEvent(event:ListEvent):void {             
            var quantity:Number=acCart[event.columnIndex].quantity;
            var price:Number=acCart[event.columnIndex].price;
            total += quantity * price;

        }

My calculated total will be shown in a label

<s:Label id="prijs" text="{total}" />

What I want is to calculate a total price. I have an arraycollection with 3 fielddata's (Nameproduct, quantity and price). In a function I want to pull the data "quantity" and the data "price" out of the arraycollection so that I calculate a "totalprice".

At the moment the function i wrote is not working. I don't recieve any data.

Upvotes: 0

Views: 115

Answers (3)

Adrian Pirvulescu
Adrian Pirvulescu

Reputation: 4340

Why not just

private function gridClickEvent(event:ListEvent):void {
    .... // your math here

    trace("old total = "+ total);
    total += quantity * price;
    trace("new total = "+ total);

    // forget about binding and manually set the property
    prijs.text = total.toString();
}

Binding sometimes is crazy in flex, and even if you/I understand the mechanism of Binding perfectly it may still be the chance that the binding is not working or the values are set N times instead of just once (case of several MVC frameworks out there).

For this reason I hate binding, and I am reserved in using it.

PS: what would your trace output be ? (please "debug", not run, to get console output)

Upvotes: 0

Florian Salihovic
Florian Salihovic

Reputation: 3951

It's total+= not total=+. You have a syntax error in the operator.

Upvotes: 2

user1901867
user1901867

Reputation:

Do some debugging:

private function gridClickEvent(event:ListEvent):void {

    //see if getting expected values 
    trace(event.rowIndex);
    trace(acCart[event.rowIndex].quantity);
    trace(acCart[event.rowIndex].price);

    var quantity:Number=parseFloat(acCart[event.rowIndex].quantity);
    var price:Number=parseFloat(acCart[event.rowIndex].price);
    total += quantity * price;
}

Upvotes: 2

Related Questions