0 Replies Latest reply on May 10, 2016 12:19 AM by someyagoel

    Customized combobox does not populate data at first click after flex migration from 3.2 to 4.6

    someyagoel

      I am facing this issue after flex migration. The customized combobox does not show any data at first click but show data after first click.

      Below are the screenshots of the problem.

       

       

      <mx:GridItem width = "100%">

                       <controls:CustomizedSearchableComboBox width = "100%"

                           height="24"

                           styleName="unitDetails"

                           id="area"

                           labelField="areaName"

                          close="onFilterAreaClose(event)"

                          dataProvider="{areas}" />

      </mx:GridItem>

       

      package controls

      {

          import flash.events.Event;

         

          import mx.collections.IViewCursor;

          import mx.events.CollectionEvent;

          import mx.events.FlexEvent;

          import mx.events.ListEvent;

         

          public class CustomizedSearchableComboBox extends CustomizedPatchedComboBox {                   

             

              private static const MIN_VALUE:int = 5;

              private static const MID_VALUE:int = 10;

              private static const MAX_VALUE:int = 15;       

             

              override public function set showDataTips(value:Boolean):void {

                  super.showDataTips = value;

              }

             

              public function search(fieldName: String, fieldValue: *): Boolean {

                  if (collection && collection.length && fieldName) {

                      var cursor: IViewCursor = collection.createCursor();

                      var item: Object;

                      if (fieldValue is Number && isNaN(fieldValue))

                      {

                          fieldValue = null;

                      }

                      var currentValue: *;

                      while ((item = cursor.current)) {

                          if (item != null) {

                              try {

                                  currentValue = item[fieldName];

                                  if (typeof(currentValue) == "Number" && isNaN(currentValue)){

                                      currentValue = null;   

                                  }

                                     

                                  if (currentValue == fieldValue) {

                                      selectedItem = item;

                                      return true;

                                  }

                              }

                              catch (e: Error) { }

                          }

                          cursor.moveNext();

                      }

                  }

                  selectedIndex = -1;

                  return false;

              }

             

              /**

               * Overrides setting of dataprovider to this combobox.

               *

               * Also manipulates combobox row count based on the data provider's length.

               **/

              override public function set dataProvider(value:Object):void {           

                  if (value != null) {

                      super.dataProvider = value;

                      if (value.hasOwnProperty('length')) {

                          if (value.length != 0) {

                              changeRowCount(value.length);

                          }

                      }

                  }           

                  super.dataProvider.removeEventListener(CollectionEvent.COLLECTION_CHANGE, dataChangeHandler);

                  super.dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, dataChangeHandler);

              }

             

              private function dataChangeHandler(event:Event):void {

                  if (dataProvider.hasOwnProperty('length')) {

                      if (dataProvider.length != 0) {

                          changeRowCount(dataProvider.length);

                      }

                  }

              }

             

             

              /**

               * Changes combobox row count based on the data's length and constants defined in this class.

               **/        

              private function changeRowCount(value:int):void {

                  if (value > MAX_VALUE) {

                      this.rowCount = MAX_VALUE;

                  } else if (value > MID_VALUE && value < MAX_VALUE) {

                      this.rowCount = MID_VALUE;    

                  } else if (value > MIN_VALUE && value <= MID_VALUE) {

                      this.rowCount = value;

                  }

              }

             

          }

      }

       

      package controls

      {

          import mx.collections.IViewCursor;

          import mx.controls.ComboBox;

          import mx.core.ClassFactory;

         

          public class CustomizedPatchedComboBox extends AutoCompleteCombo {

              private var savedText: String;

              private var _classFactory:ClassFactory;

              private var _showDataTips:Boolean;

             

              public function CustomizedPatchedComboBox() {

                  super();

                  _classFactory = new ClassFactory(PatchedList);           

                  dropdownFactory = _classFactory;

                  lookAhead = false;

                  setStyle("arrowButtonWidth", 22);

                  filterFunction = Filter;

              }

             

              private function Filter(element:*, text:String):Boolean

              {

                  var label:String = itemToLabel(element);

                  if (label.length>0)

                  {

                      return (label.toLowerCase().indexOf(text.toLowerCase())>-1);

                  }

                   return true;

              }

             

              public function set showDataTips(value:Boolean):void {

                  this._showDataTips = value;           

                  setDataTipFunction();

              }

             

              public function get showDataTips():Boolean {

                  return _showDataTips;

              }

             

              public function set editorData(value: Object): void {

                  if (value is XMLList) {

                      for each (var child: XML in value){

                      if (child.localName() == "value") {

                          value = String(child.text()[0]);

                          break;

                      }

                      if (value is XMLList) {

                          value = "";

                      }

                      }

                  }

                  else if (value is XML) {

                      if (value.localName() == "value")

                      {

                          value = String(value.text()[0]);

                      }

                      else value = "";

                  }

                  if (value is String && value) {

                      text = String(value);

                      typedText = String(value);

                      for each (var item: Object in collection){

                      if (itemToLabel(item) == value) {

                          selectedItem = item;

                          return;

                      }

                      }

                  }

                  else if (value)

                      for each (item in collection){

                      if (item.id == value.id) {

                          selectedItem = item;

                          return;

                      } 

                      else text = "";

                  selectedIndex = -1;

                  }

              }

             

              public function get editorData(): Object {

                  return text;

              }

             

              public function setDefaultValue(value: Object): void {

                  editorData = value;

              }

             

             

              private function setDataTipFunction():void {           

                  _classFactory.properties = {showDataTips:showDataTips, dataTipFunction:dataTipFunction};

                  dropdownFactory = _classFactory;

              }

             

              private function dataTipFunction(item:Object): String {

                  return item.hasOwnProperty('toolTip') ? item.toolTip : "";

              }               

             

              override public function itemToLabel(item: Object, ...rest): String {

                  if (labelFunction == null && typeof(item) == "object")

                      try {

                          if (item[labelField] == null ||

                              item[labelField] is Number && isNaN(item[labelField])) return "";

                      }

                  catch (e: Error) {

                      return "";

                  }

                  return super.itemToLabel(item);

              }

          }

      }

       

      import mx.controls.List;

       

      class PatchedList extends List {       

         

          override public function itemToLabel(item: Object): String {

              if (labelFunction == null && typeof(item) == "object")

                  try {

                      if (item[labelField] == null ||

                          item[labelField] is Number && isNaN(item[labelField])) return "";

                  }

              catch (e: Error) {

                  return "";

              }

              return super.itemToLabel(item);

          }

      }