2 Replies Latest reply on Apr 9, 2008 8:27 PM by SijiGeevarghese

    Unable to retain the selected item in the comboBox using selectedIndex

    SijiGeevarghese
      Unable to retain the selected item in the comboBox using selectedIndex

      I am developing a usercontrol type of thing.
      The control has two parts,
      1) The parent part, which contains an amount and currencyId
      2) The child part, which contains a combo with a currency list as the data list
      The currency in the child component is selected from the parent component.
      But the comboItem can be selected from the parent only once.

      The child will be opened on clicking a button in the parent. the child is implemented using TitleWindow

      I have been using the selectedValue Property for the comboBox by creating my customCombo

      The function on the parent component is
      //Function to create and show th Pop-up
      private function createPopUp(evt:MouseEvent):void
      {
      ExchangeRateOnADateModel.currentCurrencyId = currencyId;
      //ExchangeRateOnADateModel.newCurrencyId = currencyId;
      ExchangeRateOnADateModel.companyId = companyId;
      ExchangeRateOnADateModel.exchangeRateOnDate = currencyBaseDate;

      //Firing the event for filling the fluctuation combo
      var getExchangeRateList:GetExchangeRateOnADateEvent = new GetExchangeRateOnADateEvent(currencyBaseDate);
      getExchangeRateList.dispatch();
      //Firing the event for getting the base currency
      var getBaseCurrencyOn:GetBaseCurrencyOnEvent = new GetBaseCurrencyOnEvent(companyId,currencyBaseDate);
      getBaseCurrencyOn.dispatch();

      var win:CurrencyConvertor = PopUpManager.createPopUp(this,CurrencyConvertor,true) as CurrencyConvertor;
      win.title = "Currency Convertor";
      win.showCloseButton = true;

      //Setting the position of the Currency convertor
      win.xpos = mouseX;
      win.ypos = mouseY;

      //Setting the Currency convertor values
      win.curConvertAmount = currencyAmount;
      win.curConvertBaseDate = currencyBaseDate;
      win.curConvertCurrencyId = currencyId;
      win.curConvertParent = this;
      win.cboExchangeRate.selectedIndex = -1;
      win.cboExchangeRate.selectedIndex = -1;

      win.cboExchangeRate.selectedValue = currencyId.toString();
      PopUpManager.centerPopUp(win);
      }

      The code in the child component is
      <Common:customComboBox width="124" id="cboExchangeRate" selectedId="CurrencyId" labelFunction="mergeColumns" dataProvider="{ExchangeRateOnADateModel.exchangeRateOnDateList}"/>

      Below is the code for the combo to expose the selectedValue Property
        • 1. Re: Unable to retain the selected item in the comboBox using selectedIndex
          dietmar.paulus
          Hey

          can you please post the necessary code-segment of your application

          Cheers

          Dietmar
          • 2. Re: Unable to retain the selected item in the comboBox using selectedIndex
            SijiGeevarghese Level 1
            Below is the script in the parent component
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="InitializeCurConvertor()" width="338" height="40">
            <mx:Script>
            <![CDATA[
            import mx.controls.Alert;
            import NeSTec.ISP.BDG.Events.Currency.GetBaseCurrencyOnEvent;
            import NeSTec.ISP.BDG.Events.Currency.GetExchangeRateOnADateEvent;
            import NeSTec.ISP.BDG.Model.ExchangeRateOnADateModelLocator;
            import mx.managers.PopUpManager;

            //Exposed variables of the Currency UserControl
            [Bindable]
            public var currencyAmount:Number;
            public var currencyBaseDate:Date;
            public var currencyId:Number;
            public var companyId:Number;

            public var ExchangeRateOnADateModel:ExchangeRateOnADateModelLocator = ExchangeRateOnADateModelLocator.getInstance();

            //Initialization function for setting the convertor variables and calling the start up functions
            public function InitializeCurConvertor():void
            {
            //Remove this on actual use.
            currencyAmount = 1000;
            currencyBaseDate = new Date("1/1/2008");
            currencyId = 8;
            companyId = 1;
            }

            //Function to create and show th Pop-up
            private function createPopUp(evt:MouseEvent):void
            {
            ExchangeRateOnADateModel.currentCurrencyId = currencyId;
            //ExchangeRateOnADateModel.newCurrencyId = currencyId;
            ExchangeRateOnADateModel.companyId = companyId;
            ExchangeRateOnADateModel.exchangeRateOnDate = currencyBaseDate;

            //Firing the event for filling the fluctuation combo
            var getExchangeRateList:GetExchangeRateOnADateEvent = new GetExchangeRateOnADateEvent(currencyBaseDate);
            getExchangeRateList.dispatch();
            //Firing the event for getting the base currency
            var getBaseCurrencyOn:GetBaseCurrencyOnEvent = new GetBaseCurrencyOnEvent(companyId,currencyBaseDate);
            getBaseCurrencyOn.dispatch();

            var win:CurrencyConvertor = PopUpManager.createPopUp(this,CurrencyConvertor,true) as CurrencyConvertor;
            win.title = "Currency Convertor";
            win.showCloseButton = true;

            //Setting the position of the Currency convertor
            win.xpos = mouseX;
            win.ypos = mouseY;

            //Setting the Currency convertor values
            win.curConvertAmount = currencyAmount;
            win.curConvertBaseDate = currencyBaseDate;
            win.curConvertCurrencyId = currencyId;
            win.curConvertParent = this;
            win.cboExchangeRate.selectedIndex = -1;
            win.cboExchangeRate.selectedIndex = -1;

            win.cboExchangeRate.selectedValue = currencyId.toString();
            PopUpManager.centerPopUp(win);
            }

            ]]>
            </mx:Script>
            <mx:TextInput id="myText" text="{currencyAmount}" x="58" y="10" textAlign="right" width="112"/>
            <mx:Label text="Rs" x="178" y="12" id="lblCurrencyCode"/>
            <mx:Button click="createPopUp(event)" id="image" x="205" y="8" width="60" height="26" label="Show"/>
            </mx:Canvas>

            Below is the script in child file
            <?xml version="1.0" encoding="utf-8"?>
            <mx:TitleWindow xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()" move="moveMe()" close="closePopUp()" autoLayout="true" xmlns:control="NeSTec.ISP.BDG.Control.*" xmlns:business="NeSTec.ISP.BDG.Business.*" xmlns:BDG="NeSTec.ISP.BDG.*" xmlns:Common="NeSTec.ISP.BDG.View.Components.Common.*">
            <mx:Script>
            <![CDATA[
            import mx.controls.List;
            import mx.controls.listClasses.ListData;
            import NeSTec.ISP.BDG.Events.Currency.GetBaseCurrencyOnEvent;
            import NeSTec.ISP.BDG.Events.Currency.GetExchangeRateOnADateEvent;
            import NeSTec.ISP.BDG.Model.ExchangeRateOnADateModelLocator;
            import mx.utils.ObjectUtil;
            import mx.collections.SortField;
            import mx.collections.Sort;
            import mx.collections.IViewCursor;
            import mx.events.ListEvent;
            import mx.binding.utils.ChangeWatcher;
            import mx.events.DataGridEvent;
            import mx.events.FlexEvent;
            import mx.events.CollectionEvent;
            import mx.collections.ArrayCollection;
            import mx.formatters.DateFormatter;
            import mx.formatters.Formatter;
            import mx.managers.PopUpManager;
            import mx.formatters.NumberBase;
            import mx.formatters.NumberFormatter;
            import mx.controls.Alert;

            //Setting the position of the control as fixed
            public var xpos:Number = -1;
            public var ypos:Number = -1;
            private function moveMe():void
            {
            this.move(xpos,ypos);
            }

            private function init():void
            {

            }

            //Currency Convertor varibles
            public var curConvertAmount:Number;
            [Bindable]
            public var curConvertBaseDate:Date;
            public var curConvertCurrencyId:Number;
            [Bindable]
            public var curConvertParent:Object;

            [Bindable]
            public var ExchangeRateOnADateModel:ExchangeRateOnADateModelLocator = ExchangeRateOnADateModelLocator.getInstance();

            //Function to merge the columns to show in the combobox
            private function mergeColumns(item:Object):String
            {
            var num:Number = new Number(item["ExchangeRate"]);
            return "("+item["CurrencyCode"].toString() + " - " + num.toFixed(4) + ")";
            }

            //Function to close the pop-up
            private function closePopUp():void
            {
            PopUpManager.removePopUp(this);
            }

            //Function to change the currency only
            private function changeCurrency():void
            {
            curConvertParent.currencyId = cboExchangeRate.selectedItem["CurrencyId"];
            curConvertParent.currencyBaseDate = dtEffectiveDate.selectedDate;

            //Sorting the array collection
            var sortDField:SortField = new SortField("CurrencyId")
            var sortD:Sort = new Sort();
            sortD.fields = new Array(sortDField);
            ExchangeRateOnADateModel.exchangeRateOnDateList.sort = sortD;
            ExchangeRateOnADateModel.exchangeRateOnDateList.refresh();

            //Creating the cursor
            var myCursor:IViewCursor;
            myCursor = ExchangeRateOnADateModel.exchangeRateOnDateList.createCursor();
            //Creating the search criteria for the selected currency
            var obj:Object = new Object();
            obj.CurrencyId = curConvertParent.currencyId;
            //Navigating to the first matching item
            myCursor.findFirst(obj);
            curConvertParent.lblCurrencyCode.text = myCursor.current["CurrencyCode"];
            //Closing the pop-up
            closePopUp();
            }

            //Function to convert the currency and its value
            private function convertCurrency():void
            {
            //Sorting the array collection
            var sortDField:SortField = new SortField("CurrencyId")
            var sortD:Sort = new Sort();
            sortD.fields = new Array(sortDField);
            ExchangeRateOnADateModel.exchangeRateOnDateList.sort = sortD;
            ExchangeRateOnADateModel.exchangeRateOnDateList.refresh();

            //Creating the cursor
            var myCursor:IViewCursor;
            myCursor = ExchangeRateOnADateModel.exchangeRateOnDateList.createCursor();

            //Creating the search criteria for the previous currency
            var obj:Object = new Object();
            obj.CurrencyId = curConvertParent.currencyId;
            //Navigating to the first matching item
            myCursor.findFirst(obj);
            var previousCurrencyExchangeRate:Number = myCursor.current["ExchangeRate"];

            curConvertParent.currencyId = cboExchangeRate.selectedItem["CurrencyId"];
            curConvertParent.currencyBaseDate = dtEffectiveDate.selectedDate;

            //Creating the search criteria for the selected currency
            obj.CurrencyId = curConvertParent.currencyId;
            //Navigating to the first matching item
            myCursor.findFirst(obj);
            var selectedCurrencyExchangeRate:Number = myCursor.current["ExchangeRate"];

            //Calculating the new currency amount value
            //new currency amount value = (currency amount / previousCurrencyExchangeRate) * selectedCurrencyExchangeRate
            var newCurrencyAmount:Number = ( curConvertAmount / previousCurrencyExchangeRate ) * selectedCurrencyExchangeRate;
            curConvertParent.currencyAmount = newCurrencyAmount;
            curConvertParent.myText.text = newCurrencyAmount.toString();
            curConvertParent.lblCurrencyCode.text = myCursor.current["CurrencyCode"];
            //Closing the pop-up
            closePopUp();
            }

            //Changing the Exchange Rates and base currency according to the new date
            private function EffectiveDateChange():void
            {
            ExchangeRateOnADateModel.exchangeRateOnDate = dtEffectiveDate.selectedDate;
            curConvertParent.currencyBaseDate = dtEffectiveDate.selectedDate;

            //Firing the event for filling the fluctuation combo
            var getExchangeRateList:GetExchangeRateOnADateEvent = new GetExchangeRateOnADateEvent(dtEffectiveDate.selectedDate);
            getExchangeRateList.dispatch();
            //Firing the event for getting the base currency
            var getBaseCurrencyOn:GetBaseCurrencyOnEvent = new GetBaseCurrencyOnEvent(curConvertParent.companyId,dtEffectiveDate.selectedDate);
            getBaseCurrencyOn.dispatch();
            //Alert.show(dtEffectiveDate.selectedDate.toDateString());
            }

            ]]>
            </mx:Script>
            <Common:customComboBox width="124" id="cboExchangeRate" selectedId="CurrencyId" labelFunction="mergeColumns" dataProvider="{ExchangeRateOnADateModel.exchangeRateOnDateList}"/>
            </mx:TitleWindow>