6 Replies Latest reply on Jan 22, 2010 12:41 PM by Jerry62712

    How do I add a column

    Jerry62712 Level 1

      In certain cases I want to add a datagrid column to the ones that I already have setup from the mxml.  Specifically, I want to add a new column to the start of the datagrid when a value is put in a dropdown.  Here is where I'm starting from:

      private function searchButtonClick():void
      {
          
          dmSearch.SearchCriteria.FIRSTNAME = sFIRSTNAME.text;
          dmSearch.SearchCriteria.LASTNAME = sLASTNAME.text;
          dmSearch.SearchCriteria.MHDDID = sMHDDID.text;
          dmSearch.SearchCriteria.RIN = sRIN.text;
          if (sSODC.selectedLabel == "Any")
          {
              dmSearch.SearchCriteria.SODC = "%";
      //        dgSearchResults.a    to add SODC column when general search
          }
          else
          {
              dmSearch.SearchCriteria.SODC = sSODC.selectedLabel;
          }
          dmSearch.SearchCriteria.SSN = sSSN.text;

       

      And the current mxml:

                          <mx:FormItem id="lblSearchResults" 
                              indicatorGap="0" label="Selected Records">
                              <mx:DataGrid id="dgSearchResults" 
                                  dataProvider="{dmSearch.RecProcess}"
                                  itemClick="sDataGridClicked( event );" 
                                  keyDown="sDataGridPressed(event)"
                                  tabIndex="19" 
                                  rowCount="7">
                                  <mx:columns>
                                      <mx:DataGridColumn id="colLastName"
                                          dataField="LASTNAME"
                                          headerText="Last Name"
                                          labelFunction="sFixDataLastName"
                                          headerWordWrap="false" />
                                      <mx:DataGridColumn id="colFirstName"
                                          dataField="FIRSTNAME"
                                          headerText="First Name"
                                          labelFunction="sFixDataFirstName"
                                          headerWordWrap="false" />
                                      <mx:DataGridColumn id="colMHDDID"
                                          dataField="MHDDID"
                                          headerText="MHDDID"
                                          labelFunction="sFixDataMHDDID"
                                          wordWrap="false" />
                                      <mx:DataGridColumn id="colRIN"
                                          dataField="RIN"
                                          headerText="RIN"
                                          labelFunction="sFixDataRIN"
                                          wordWrap="false" />
                                      <mx:DataGridColumn id="colSSN"
                                          dataField="SSN"
                                          headerText="SSN"
                                          labelFunction="sFixDataSSN"
                                          wordWrap="false" />
                                  </mx:columns>
                              </mx:DataGrid>
                          </mx:FormItem>
      
      

       

      Code assist doesn't seem to give any "add column" option.  But I'll bet the experts here know how to do it.

        • 1. Re: How do I add a column
          msakrejda Level 4

          You can shift (add an item) onto the beginning of the columns Array and reassign the grid's columns property (note that you need to reassign the columns--if you just mutate it, the grid won't know there's been a change).

          • 2. Re: How do I add a column
            Jerry62712 Level 1

            Sounds like you know this stuff.  How do I shift it?  When I did a search in their docs (http://community.adobe.com/help/search.html?q=datagrid+scripting&x=15&y=13&lbl=flex_produc t_adobelr&hl=en_US&self=0&area=0))

            I got a lot of results - 39,200, but the first ones at least didn't include how to script it.

            • 3. Re: How do I add a column
              msakrejda Level 4
              private function searchButtonClick():void
              {
                 
                  dmSearch.SearchCriteria.FIRSTNAME = sFIRSTNAME.text;
                  dmSearch.SearchCriteria.LASTNAME = sLASTNAME.text;
                  dmSearch.SearchCriteria.MHDDID = sMHDDID.text;
                  dmSearch.SearchCriteria.RIN = sRIN.text;
                  if (sSODC.selectedLabel == "Any")
                  {
                      dmSearch.SearchCriteria.SODC =
              "%";
                      var newCol:DataGridColumn = new DataGridColumn('name');
                      newCol.dataField = 'foo';
                      var newCols:Array =
              dgSearchResults.columns;
                      newCols.shift(newCol);
                     
              dgSearchResults.columns = newCols;
                  }
                  else
                  {
                      dmSearch.SearchCriteria.SODC = sSODC.selectedLabel;
                  }
                  dmSearch.SearchCriteria.SSN = sSSN.text;
              }
              • 4. Re: How do I add a column
                Jerry62712 Level 1

                Thanks!  Worked like a charm.  Is there anyway to eliminate the column if the user isn't pressing the "Any" selection?

                • 5. Re: How do I add a column
                  msakrejda Level 4

                  Actually, you may want to just include the column in the default definition and toggle its visible property. This would solve this additional requirement as well as avoid much of the complexity here.

                   

                  private function searchButtonClick():void
                  {
                     
                      dmSearch.SearchCriteria.FIRSTNAME = sFIRSTNAME.text;
                      dmSearch.SearchCriteria.LASTNAME = sLASTNAME.text;
                      dmSearch.SearchCriteria.MHDDID = sMHDDID.text;
                      dmSearch.SearchCriteria.RIN = sRIN.text;
                      if (sSODC.selectedLabel == "Any")
                      {
                          dmSearch.SearchCriteria.SODC =
                  "%";
                          colAny.visible = true;
                      }
                      else
                      {
                          dmSearch.SearchCriteria.SODC = sSODC.selectedLabel;
                          colAny.visible = false;
                      }
                      dmSearch.SearchCriteria.SSN = sSSN.text;
                  }

                   

                                          <mx:DataGrid id="dgSearchResults" 
                                              dataProvider=
                  "{dmSearch.RecProcess}"
                                              itemClick=
                  "sDataGridClicked( event );"
                                              keyDown=
                  "sDataGridPressed(event)"
                                              tabIndex=
                  "19"
                                              rowCount=
                  "7">
                                              <mx:columns>
                                                  <mx:DataGridColumn id="colAny"
                                                      dataField=
                  "foo"
                                                      headerText=
                  "bar"
                                                      headerWordWrap="false" />

                                                  <mx:DataGridColumn id="colLastName"
                                                      dataField=
                  "LASTNAME"
                                                      headerText=
                  "Last Name"
                                                      labelFunction=
                  "sFixDataLastName"
                                                      headerWordWrap=
                  "false" />
                                                  ...
                                              </mx:columns>
                                          </mx:DataGrid>
                  • 6. Re: How do I add a column
                    Jerry62712 Level 1

                    I wish I could give you two "correct answers" because you did exactly that.  Your visible on/off did the job exactly.

                     

                    Thank you very much.