1 Reply Latest reply on Nov 17, 2006 10:05 AM by ntsiii

    Problem in removing the selectedIndex

    DataGrid+XML
      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" width="500"
      height="600" >
      <mx:Model id="contact" source="datamodel/contactlist.xml"/>
      <mx:Script><![CDATA[

      import mx.events.*;
      import mx.collections.*;
      import mx.controls.Alert;

      // Add event information to a log (displayed in the TextArea).
      // Helper function for adding information to the log.

      // Add a person to the ArrayCollection.
      public function addPerson():void {
      // ac.addItem({first:firstInput.text, last:lastInput.text,
      // email:emailInput.text});
      //clearInputs();
      }

      // Remove a person from the ArrayCollection.
      public function removePerson():void {
      // Make sure an item is selected.
      if (dg.selectedIndex >= 0) {
      dg.removeChildAt(1);
      }

      }

      // Update an existing person in the ArrayCollection.
      public function updatePerson():void {
      // Make sure an item is selected.
      //if (dg.selectedItem !== null) {
      // ac.setItemAt({first:firstInput.text, last:lastInput.text,
      // email:emailInput.text}, dg.selectedIndex);
      // }
      }

      // The change event listener for the DataGrid.
      // Clears the text input controls and updates them with the contents
      // of the selected item.
      public function dgChangeHandler():void {
      clearInputs();
      firstInput.text = dg.selectedItem.first;
      lastInput.text = dg.selectedItem.last;
      emailInput.text = dg.selectedItem.email;

      }

      // Clear the text from the input controls.
      public function clearInputs():void {
      firstInput.text = "";
      lastInput.text = "";
      emailInput.text = "";
      }

      // The labelFunction for the ComboBox;
      // Puts first and last names in the ComboBox.
      public function myLabelFunc(item:Object):String {
      return item.first + " " + item.last;
      }
      ]]>
      </mx:Script>

      <mx:DataGrid width="450" id="dg" dataProvider="{contact.contact}">
      <mx:columns>
      <mx:DataGridColumn dataField="first_name" headerText="First Name"/>
      <mx:DataGridColumn dataField="last_name" headerText="Last Name"/>
      <mx:DataGridColumn dataField="phone" headerText="Mobile Number"/>
      </mx:columns>
      </mx:DataGrid>

      <!-- The ComboBox and DataGrid controls share an ArrayCollection as their
      data provider.
      The ComboBox control uses the labelFunction property to construct the
      labels from the dataProvider fields. -->
      <mx:HBox paddingTop="20">
      <!-- Buttons to initiate operations on the collection. -->
      <mx:Button label="Add New" click="addPerson()"/>
      <mx:Button label="Update Selected" click="updatePerson()"/>
      <mx:Button label="Remove Selected" click="removePerson()"/>
      <!-- Clear the text input fields. -->
      <mx:Button label="Clear" click="clearInputs()"/>
      </mx:HBox>



      <!-- Form for data to add or change in the ArrayCollection. -->
      <mx:Form>
      <mx:FormItem label="First Name">
      <mx:TextInput id="firstInput"/>
      </mx:FormItem>
      <mx:FormItem label="Last Name">
      <mx:TextInput id="lastInput"/>
      </mx:FormItem>
      <mx:FormItem label="Email">
      <mx:TextInput id="emailInput"/>
      </mx:FormItem>
      </mx:Form>


      </mx:Application>