3 Replies Latest reply on Oct 5, 2010 5:44 PM by mumcs01

    Changing selectindex in AS of a datagrid doesn't work!?

    mumcs01 Level 1

      I'm having a real problem changing the selectedindex of a datagrid on the fly after a re-sort (Using AS3 with Flex4). See below. Particularly the
      function call comments. My sort works and it gets assigned to ldimmer_checkChannel.selectedIndex with the correct value, but
      when the function call finishes ldimmer_checkChannel.selectedIndex revers to -1. Why???/

       

      protected function channelSortMode(event:IndexChangeEvent):void
      {
      var currentCircuit:int = current_rack[0].rackLevels.getItemAt(ldimmer_checkChannel.selectedIndex).circuit;
      var i:int=0;
      //Change to Circuit Mode
      if (event.newIndex == 1) {    
        current_rack[0].sortByCircuit();  //According to Debug this is resorting just fine.
        for (i=0; i < current_rack[0].circuits; i++)
        {
         if (current_rack[0].rackLevels[i].circuit == currentCircuit) {
         break;
         }
        }
      }                 

      //Change to UDN Mode
      if (event.newIndex == 0) {
        current_rack[0].sortByUdn();      //According to Debug this is resorting just fine.
        for (i=0; i < current_rack[0].circuits; i++)
        {
         if (current_rack[0].rackLevels[i].circuit == currentCircuit) {
         break;
         }
        }
      }

      ldimmer_checkChannel.selectedIndex = i;          //This is working and setting the correct index
      current_rack[0].currentSort = event.newIndex;
      }  // At the end of this function call ldimmer_checkChannel.selectedIndex is reverting to -1 according to debug.

       

      <mx:DataGrid id="ldimmer_checkChannel" x="21" y="33" height="124" width="199" selectedIndex="2" dataProvider="{ current_rack.getItemAt(0).rackLevels }">
      <mx:columns>
        <mx:DataGridColumn id="field1" dataField="udn" headerText="UDN" />
        <mx:DataGridColumn id="field2" dataField="circuit" headerText="Circuit" />
      </mx:columns> 
      </mx:DataGrid>

       

      <s:ComboBox x="10" y="5" id="channelMode" dataProvider="{ channelTypes }" change="channelSortMode(event)" selectedIndex="{ current_rack.getItemAt(0).currentSort }"/>

        • 1. Re: Changing selectindex in AS of a datagrid doesn't work!?
          Flex harUI Adobe Employee

          What kind of sort are you using?  If it deletes the current item from the DG

          as it moves items around in the sort, selectedIndex will be reset.

          • 2. Re: Changing selectindex in AS of a datagrid doesn't work!?
            mumcs01 Level 1

            Thanks for the response. I fear this may be my problem. Here are my sorts in the class:

             

             

            public function sortByUdn():void

            {

            var udnSort:Sort = new Sort()

            udnSort.fields = new Array();

            udnSort.fields.push(new SortField('udn',true,false,true));

            this.rackLevels.sort = udnSort;

            this.rackLevels.refresh();

            }

             

            public function sortByCircuit():void

            {

            var circuitSort:Sort = new Sort()

            circuitSort.fields = new Array();

            circuitSort.fields.push(new SortField('circuit',true,false,true));

            this.rackLevels.sort = circuitSort;

            this.rackLevels.refresh();

            }

             

             

            So maybe I'm going about this wrong. My knowledge of sorts is a bit rough. (I wish there was an adobeTV video on sorts). Here is my intent. rackLevels is loaded on initialization. It is an arraycollection of of 100-150 records each containing information, but the most important bits of information per instance are called 'circuit', and 'udn. These are the 2 fields I care about. The user sees both of these in the datagrid. Like below:

             

            UDN    Circuit

            1              56

            2              90

            4              80

            5              14  

             

            A seperate combo box exists to choose the sort:

            <s:ComboBox x="10" y="5" id="channelMode" dataProvider="{ channelTypes }" change="channelSortMode(event)" selectedIndex="{ current_rack.getItemAt(0).currentSort }"/>

             

            Which runs a function channelSortMode in the Rack class to choose the sort functon above to run. So here is what I'm trying to acheieve. The default sort is UDN. If the user has selected the row 4 (In this case UDN 4 / Circuit 80). I grab that record currently with the selectedIndex, and use the data in the row to perform another function. But if the user uses the combobox to then sort by Circuit the datagrid re-sorts but I lose the selectedIndex selection of UDN 4 / Circuit 80. When the re-sort happens I want to keep the current selection?

             

            Any help would be great!? Any ideas? Thanks!

            • 3. Re: Changing selectindex in AS of a datagrid doesn't work!?
              mumcs01 Level 1

              Can selectedIndex even be editable in Flex 4? It always seems to revert to -1?