0 Replies Latest reply on Apr 20, 2009 2:09 AM by FlexIdea

    Render dynamic columns in AdvancedDataGrid

    FlexIdea

      HI,

       

       

      I have one flex application using  AdvancedDataGrid, implementing PureMVC Framework.

       

      Some of the columns in the grid are  static and some are dynamic

      Dynamic columns are being rendered  as “TextInput”.

      I am adding dynamic columns to the  Grid in the mediator. But while rendering, no. of columns gets increased and  extra columns are added to the top left corner of the grid. (set data is called  more times than the columns added )

       

      Here I have attached the code for  mediator (contains the code to create columns only, not complete code) and  Render.

       

       

      Any suggestion is  appreciable.

       

      Mediator Code.

       

      private function CreateLocationColumns():void
      {
          var intInitWidth:Number = Application.application.m_initTW;
          var intColsWidth:Number = 0;       
          var initPage:Number= Application.application.m_page_begin-1;
          var endPage:Number=Application.application.m_page_end;   
          var columnsToAdd:DataContainer = ApplicationFacade.getInstance().m_LocationColumns;
          var tabOrder:Number=0;
          if(columnsToAdd != null)
          {
              var keys:Array = columnsToAdd.getKeys();
              Application.application.m_page_total=keys.length;   
              if(endPage > keys.length){
                  endPage=keys.length;                                       
              }
              if(endPage!=keys.length){
                  getView().btnNext.enabled=true;
              }               
              var dgcg:AdvancedDataGridColumnGroup;
              var cols:Array = getView().dgAllocation.groupedColumns;
              for(var intCtr:Number=initPage;intCtr<endPage;intCtr++)
              {
                  var obj:Object = columnsToAdd.get(keys[intCtr]);
                 
                  if(obj!=null)
                  {
                      dgcg = new AdvancedDataGridColumnGroup();
                      dgcg.headerRenderer = new ClassFactory(LocationHeaderRenderer);
                      dgcg.headerText = obj.columnHeaderLabel;
                      dgcg.dataField = obj.columnDataField;                   
                      dgcg.resizable = false;
                      dgcg.sortable = false;
                     
                      // create Column Number User
                      var colUser:AdvancedDataGridColumn = new AdvancedDataGridColumn();
                      colUser.headerText = "User";
                      colUser.resizable = false;
                      colUser.sortable = false;
                      colUser.width = 50; // this size should also reflext to its ItemRenderer                                               
                     
                      colUser.headerRenderer = new ClassFactory(LocationUserHeaderRenderer);
                      colUser.itemRenderer = new ClassFactory(LocationUserRenderer);
                      colUser.dataField = obj.columnDataField;
                      colUser.rendererIsEditor = false;
                      colUser.editable=false;
                      colUser.editorUsesEnterKey= false;   
                     
                     
                      dgcg.children.push(colUser);
                     
                      cols.push(dgcg);
                         
                      intColsWidth += colUser.width;
                  }
              }
             
              // check if there are locations and Opportunity is not lock
              if(keys.length>0 && Application.application.m_blOptyLock==false)
              {
                  getView().toolbar.btnSave.enabled=true;
              }
             
              Application.application.m_page_end=endPage;               
              Application.application.m_ColsW = intColsWidth;
              getView().dgAllocation.groupedColumns = cols;
              //getView().dgAllocation.width = (getView().width<=(intInitWidth+intColsWidth))? getView().width : (intInitWidth+intColsWidth);
              getView().dgAllocation.width = getView().width;
              getView().dgAllocation.percentHeight = 100;
              getView().validateNow();
          }

       

      LocationUserRenderer.mxml

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:TextInput
          click="onClickHandler(event)"
          width="50"
          height="20"
          styleName="sfdcTextInput"
          visible="true"
          includeInLayout="true"
          restrict="0-9"
          maxChars="8"
          focusOut="NoUserChanged(event)"
          focusIn="keepLastValue(event)"   
          text="{UserCount}"
          xmlns:mx="http://www.adobe.com/2006/mxml">
          <mx:Style source="css/Moody.css" />
          <mx:Script>
              <![CDATA[
                  import mx.collections.IViewCursor;
                  import mx.managers.CursorManager;
                  import mx.formatters.CurrencyFormatter;
                  import mx.formatters.NumberFormatter;
                  import com.fujitsu.app.ApplicationFacade;
                  import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
                  import mx.collections.ArrayCollection;
                  import mx.core.Application;
                  import mx.formatters.NumberBaseRoundType;

       

                  private var columnDataField:String = "";
                  private var _userCount:String;
                 
                  public var m_verticalScrollPos:Number = 0; //Sets the position before refreshing the datagrid. Used in Validate click           
                 
                  private var iOldValue:Number;
                  private var iNewValue:Number;
                 
                  private function keepLastValue(evt:Event):void{
                      iOldValue = Number(this.text);
                  }
                 
                  [Bindable]
                  public function get UserCount():String
                  {
                      return _userCount;
                  }
                 
                  public function set UserCount(value:String):void
                  {
                      _userCount = value;
                  }

       

                  public override function set data( value:Object ) : void
                  {
                      super.data = value;
                      getData(value);
                  }

       

                  private function getData(dataList:Object):void
                  {
                      columnDataField = AdvancedDataGridListData(listData).dataField;
                      getNoOfUser();               
                      checkIfLock();               
                  }
                 
                  /***
                   * This function will check whether the opportunity is Lock or not
                   * **/
                  private function checkIfLock():void
                  {
                      if(Application.application.m_blOptyLock)
                      {
                          this.enabled = false;
                      }
                  }
                 
                  /***
                   * This function is the event handler for the text inputs change event
                   * and will compute totals based on the requirement
                   * **/
                  private function NoUserChanged(evt:Event):void
                  {
                      if(this.text == "") this.text = "0";
                      iNewValue = Number(this.text);
                      if(iOldValue == iNewValue){
                          //this.focusManager.getNextFocusManagerComponent();
                          return;                   
                      }               
                      callLater(UpdateAmount);
                  }
                 
                  private function UpdateAmount():void
                  {
                      var totalUser:Number = 0;
                      var totalAmt:Number = 0;
                     
                      // Get the assigned Location Information
                      for each(var item:Object in data.Location)
                      {
                          // If dataField == the name of the location
                          if(columnDataField==item.objName)
                          {
                              item.allocUser = (this.text=="")? 0:Number(this.text);
                              //var intNoUser:Number = item.allocUser;
                              if(data.UserBased)
                              {
                                  item.allocAmt = computeAmt(data.AllocAmt,data.AllocUser,item.allocUser);                           
                              }               
                              UserCount = item.allocUser;
                              totalUser+=item.allocUser;
                              //item.allocAmt = int(item.allocAmt*100)/100;
                              totalAmt+=item.allocAmt;
                          }
                          else
                          {
                              totalUser+=item.allocUser;
                              //item.allocAmt = int(item.allocAmt*100)/100;
                              totalAmt+=item.allocAmt;                       
                          }
                      }
                      // Round Off
                      //totalUser = int(totalUser*100)/100;
                      //totalAmt = int(totalAmt*100)/100;
                      totalAmt = Number(Number(totalAmt).toFixed(2));
                      data.TotalUser = totalUser;
                      data.TotalAmt = Math.round(totalAmt);       
                      // Update background ErrorIndicator
                      var isError:Boolean = false;
                      if(data.TotalUser != data.AllocUser)
                      {
                          isError=true;
                          Application.application.AddToUserArray(data.ProductName);
                      }
                      else
                      {
                          Application.application.RemoveToUserArray(data.ProductName);
                      }
                     
                      if(data.TotalAmt != data.AllocAmt)
                      {
                          isError=true;
                          Application.application.AddToAmtArray(data.ProductName);
                      }
                      else
                      {
                          Application.application.RemoveToAmtArray(data.ProductName);
                      }
                     
                      // Update background ErrorIndicator
                      data.isErrorRow  = (isError)? true:false;
                      m_verticalScrollPos=this.parentApplication.dgAllocation.verticalScrollPosition;
                      // Refresh Data and Grid UI               
                      this.parentApplication.dgAllocation.dataProvider.refresh();
                      //this.parentApplication.dgAllocation.dataProvider.refresh(); = this.parentApplication.dgAllocation.dataProvider;               
                      validateNow();
                      this.parentApplication.dgAllocation.verticalScrollPosition = m_verticalScrollPos;                   
                  }
                 
                  /***
                   * This function wil compute the Amount when Quote Line is User-Based
                   * **/
                  private function computeAmt(intQuoteSalesPrice:Number,intQuoteNoUser:Number,
                                              intLocNoUser:Number):Number
                  {
                      var retVal:Number = 0;
                      // OpportunityLocationLine.AllocatedUser*(QuoteLine.SalesPrice/QuoteLine.NumberOfUsers
                      // as per Moody Function v 2 7 5 Fujitsu.doc
                      if(intQuoteNoUser!=0)
                      {
                          var computedAmt:Number = intLocNoUser*(intQuoteSalesPrice/intQuoteNoUser);
                          // round to 2 decimal places
                          //retVal = int(computedAmt*100)/100;
                          retVal = Number(Number(computedAmt).toFixed(2));
                      }
                      else
                      {
                          retVal = 0;
                      }
                      return retVal;
                  }
                 
                  /**
                   * Function to handle click event. If the Textbox is enabled then it will set focus to this field.
                   * This is useful to handle focus in and out event. Event handler added by Siddhesh Kabe
                   *
                   * **/
                  private function onClickHandler(evt:Event):void{
                      if(this.enabled){
                          this.focusManager.getFocus().setFocus();
                      }
                  }
                 
                  /***
                   * This function will retrieve the no of user value from the Array of
                   * Location. If not found then make the gray mark visible
                   * **/
                  private function getNoOfUser():Boolean
                  {
                      var found:Boolean = false;
                     
                      if(data != null && data.Location != null)
                      {
                          for each(var item:Object in data.Location)
                          {
                              if(this.columnDataField == item.objName)
                              {
                                  UserCount = item.allocUser;
                                  found = true;
                                  break;
                              }
                          }
                      }
                      if(!found)
                      {
                          this.visible = false;
                          this.includeInLayout = false;
                          this.editable = false;
                          this.enabled = false;
                          //validateNow();
                          //ApplicationFacade.getInstance().m_locationItemRecord.refresh();
                      }else{
                          /*this.visible = true;
                          this.includeInLayout = true;
                          this.editable = true;
                          this.enabled = true;*/
                      }
                      return found;
                  }

       

              ]]>
          </mx:Script>
      </mx:TextInput>


          else
          {
              Application.application.m_page_end=0;
              getView().dgAllocation.visible=false;
              getView().BottomButtonBar.visible=false;
              sendNotification(LocationLineProxy.ERROR,ErrorMessages.ERROR_PREFIX+"No locations added. Please add locations.");
          }
      }