1 Reply Latest reply on Sep 29, 2010 11:35 AM by saisri2k2

    Datagrid multiple selection with indices, to then add data to new array

    djh88ukwb Level 1

      Hello all.

       

      Basically what i am trying to do is follows -

       

      Have one arraycollection with a load of data, this data is being viewed in an datagrid.  I have turned allowmultipleselection on, and that is fine.

       

      I have also got an array of indices that have been selected. so i know the row position of each selected item.

       

      What i want to create is a function that will loop,  taking each indice at a time and put it into a new arraycollection.

       

      I am having troubles with this part thou.  Here is my code

       

       

      <?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/mx" minWidth="955" minHeight="600">
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <fx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.events.ListEvent;
                 
                  [Bindable] public var i:int=0;
                  [Bindable] public var tempArray:ArrayCollection = new ArrayCollection();
                  [Bindable] public var selectedind:Array = new Array;
                  [Bindable] private var ac:ArrayCollection=new ArrayCollection([
                      {ID: "1", Name: "John", Score:"25", Rating:"3", sel: false},
                      {ID: "2", Name: "Joe", Score:"45", Rating:"5", sel: false},
                      {ID: "3", Name: "Bob", Score:"27", Rating:"2", sel: false},
                      {ID: "4", Name: "Dave", Score:"37", Rating:"4", sel: false},
                      {ID: "5", Name: "Tom", Score:"21", Rating:"3", sel: false},
                      {ID: "6", Name: "Dan", Score:"39", Rating:"2", sel: false},
                      {ID: "7", Name: "Jack", Score:"47", Rating:"5", sel: false},
                      {ID: "8", Name: "Jill", Score:"35", Rating:"2", sel: false}])

                     
                     
                     
                  protected function dg_itemClickHandler(event:ListEvent):void
                  {
                       selectedind = dg.selectedIndices;
                      selectedind.sort(Array.NUMERIC);
                      for(var index:int = selectedind.length-1; index>=0; index--)
                      selectedrows.text = selectedind.toString();
                      //tempArray.addItem({ID:event.itemRenderer.data.ID, Name:event.itemRenderer.data.Name, Score:event.itemRenderer.data.Score, Rating:event.itemRenderer.data.Rating});
                  }
                  protected function savetoNewArray():void {
                      tempArray.addItem(ac.getItemAt(1));
                  }

              ]]>
          </fx:Script>
         
          <mx:DataGrid id="dg" dataProvider="{ac}" itemClick="dg_itemClickHandler(event)" x="10" y="10" width="596" height="356" allowMultipleSelection="true">
              <mx:columns>
                  <mx:DataGridColumn headerText="Name" dataField="Name"/>
                  <mx:DataGridColumn headerText="Score" dataField="Score" width="50"/>
                  <mx:DataGridColumn headerText="Rating" dataField="Rating" width="100"/>
              </mx:columns>
          </mx:DataGrid>
         
          <mx:DataGrid x="10" y="374" width="596" dataProvider="{tempArray}" height="142">
              <mx:columns>
                  <mx:DataGridColumn headerText="Name" dataField="Name"/>
                  <mx:DataGridColumn headerText="Score" dataField="Score"/>
                  <mx:DataGridColumn headerText="Rating" dataField="Rating"/>
              </mx:columns>
          </mx:DataGrid>
          <s:Button id="but" x="10" y="524" label="Button" click="savetoNewArray()"/>
          <s:Label x="10" y="553" id="selectedrows"/>
      </s:Application>

       

      I know i can copy the data from one arraycollection to the other using this.

      tempArray.addItem(ac.getItemAt(1));

       

      I just need a loop that will change 1 with the numbers in the indices arraycollection.

       

      Thanks in advance !