1 Reply Latest reply on Oct 7, 2009 12:20 PM by matthew horn

    Getting a total from Checkboxes

    mattcom Level 1

      Just thought I'd make a post of this since I couldn't find anything in the search engines on the topic.  Hope it helps someone.

       

      Code is generated from Flash Builder 4 Beta 2.  So you may have to change things like fx: and s: to mx: for your version of Flex Builder.

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application

       

       

      xmlns:fx="http://ns.adobe.com/mxml/2009

      "

      xmlns:s="

       

      library://ns.adobe.com/flex/spark

      "

      xmlns:mx="

       

      library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768"

      >

       

       

      <fx:Declarations>

       

       

      <!-- Place non-visual elements (e.g., services, value objects) here -->

       

       

      </fx:Declarations>

       

       

      <fx:Script>

      <![CDATA[

       

       

      import

      mx.formatters.NumberFormatter;

      [

       

      Bindable

      ]

       

       

      public var

      totalAmount:Number = 0;

       

       

       

       

      private function updateTotal(e:Event):void

      {

       

       

      var nf:NumberFormatter = new

      NumberFormatter();

      nf.precision = 2;

       

       

      if

      (e.currentTarget.selected) {

      totalAmount = totalAmount + Number(e.currentTarget.label);

      }

       

       

      if

      (!e.currentTarget.selected) {

      totalAmount = totalAmount - Number(e.currentTarget.label);

      }

       

       

      var

      amountString:String = totalAmount.toString();

      amountString = nf.format(amountString)

      total.text =

       

      "$"

      + amountString;

      }

      ]]>

       

       

      </fx:Script>

       

       

      <s:CheckBox x="122" y="86" label="6.95" id="twil" click="updateTotal(event)"

      />

       

       

      <s:CheckBox x="122" y="112" label="7.95" id="twil2" click="updateTotal(event)"

      />

       

       

      <s:CheckBox x="122" y="138" label="10.00" id="twil3" click="updateTotal(event)"

      />

       

       

      <s:Label x="122" y="173" text="Total: "

      />

       

       

      <s:Label x="168" y="173" id="total" text=""

      />

       

       

      </s:Application>