2 Replies Latest reply on Nov 5, 2009 7:16 AM by erthy

    Problem with XML and lists??

    erthy

      Hey! I tried a sample app and I can't get it to work properly. I changed it to my own variables... Help!

       

      http://www.adobe.com/devnet/flex/quickstart/accessing_xml_data/

       

      I tried the one before the last...

       

      // Constants
                  private const SELECTED_ITEM:uint = 0;
                  private const THE_LIST:uint = 1;
                  private const BEFORE:uint = 0;
                  private const AFTER:uint = 1;
                      
                  // Flag: is the form valid?
                  [Bindable]
                  private var formIsValid:Boolean;
                  
                  // Flag: does a selection exist in the data grid?
                  [Bindable]
                  private var selectionExists:Boolean;
                  
                  // Holds the index of the next item in the 
                  // data grid following the deletion of a book item.
                  private var newSelectedIndex:int;
              
                  // Model: XML structure describing 
                  // some of the books in my collection.    
                  [Bindable]
                  private var soubor_slovicek:XML = 
      
      <slovnik>
        <info>
          <jmeno>Testovací Slovník</jmeno>
          <verze>1</verze>
          <autor>David Roesel</autor>
          <web>http://david.roesel.cz/slovniky</web>
          <jazyky lang1="cs" lang2="en" desc1="Čeština" desc2="English"/>
          <poznamka>Toto je pokusný slovník určený pouze pro vývoj aplikace.</poznamka>
        </info>
        <slovicka>
          <slovicko cs="pes" en="dog"/>
          <slovicko cs="voda" en="water"/>
          <slovicko cs="slunce" en="sun"/>
          <slovicko cs="mrak" en="cloud"/>
          <slovicko cs="trpaslík" en="dwarf"/>
          <slovicko cs="bůh" en="god"/>
          <slovicko cs="kotě" en="kitten"/>
          <slovicko cs="les" en="forest"/>
          <slovicko cs="dřevo" en="wood"/>
          <slovicko cs="tulipán" en="tulip"/>
        </slovicka>
      </slovnik>
                      
                      
                   
                  
                  // Add a new book. 
                  private function addWord():void
                  {
      
                      // Create a new XML Object from the form information
                      var newWord:XML = 
                          <slovicko cs={zapis_jazyk1.text} en={zapis_jazyk2.text}/>
                            ;
                          
                      // Save the selected book's index so it can be reselected
                      // in the grid once the new book is added.
                      var selectedWordIndex:uint = myDataGrid.selectedIndex;
                                                          
                      // Does the user want to add the new
                      // book relative to the selected book in the
                      // data grid or relative to the list itself?
                      if (addRelativeTo.selectedIndex == SELECTED_ITEM)
      
                      {
                          // Add the new item relative to the selected item.
      
                          var selectedWord:XML = soubor_slovicek.slovicka.slovicko[selectedWordIndex];
                                              
                          // Does the user want to add it before or after
      
                          // the selected item?
                          if (addPosition.selectedIndex == BEFORE)
                          {    
                              // Add new item before selected item 
                              soubor_slovicek = soubor_slovicek.slovicka.insertChildBefore(selectedWord, newWord);
                          }
      
                          else
                          {
                              // Add new item after selected item
                              soubor_slovicek = soubor_slovicek.slovicka.insertChildAfter(selectedWord, newWord);
                          }
      
                      }
                      else
                      {
                          // Add the new item relative to the whole list of books.
                          if (addPosition.selectedIndex == BEFORE)
      
                          {
                              // Add new item at the start of the list. 
                              soubor_slovicek = soubor_slovicek.prependChild(newWord);
                          }
                          else
      
                          {
                              // Add new item at the end of the list.
                              soubor_slovicek = soubor_slovicek.appendChild(newWord);
                          }                    
                      }
      
                      // Select the previously selected item in the grid
                      // so the user doesn't lose their position. (If a new book was
                      // added before the currently selected item, that item's
                      // new index will be one greater, so select that.)
                      var newSelectedIndex:uint = (addPosition.selectedIndex == BEFORE) ? selectedWordIndex + 1: selectedWordIndex;
                      myDataGrid.selectedIndex = newSelectedIndex;
                      Alert.show(soubor_slovicek.toString());
                  }    
                  
                  // Delete selected book
      
                  private function deleteWord():void
                  {
                      // Save the currently selected index.
                      var selectedWordIndex:int = myDataGrid.selectedIndex;
                                                      
                      // Delete the currently selected book.
      
                      delete (soubor_slovicek.slovicka.slovicko[selectedWordIndex]);
                      
                      // Reselect the next logical item in the data grid.
                      newSelectedIndex = (selectedWordIndex==0) ? 0 : selectedWordIndex - 1;
      
                      // Change the selected index of the data grid
      
                      // at a later frame. See note on changeDataGridIndex()
                      // method for more details on this workaround. 
                      callLater ( changeDataGridIndex );            
                  }    
      
                  // This is a workaround for a known issue with
                  // List-based components where deleting an item 
                  // from the control's dataProvider leaves the 
      
                  // selectedIndex at an incorrect value. The workaround
                  // is to reassign the data provider at least a 
                  // frame later and to change the index there. 
                  private function changeDataGridIndex ():void
                  {
      
                      // Reassign the data grid's data provider.
                      myDataGrid.dataProvider = soubor_slovicek.slovicka.slovicko;
      
                      // Set the selected index.
                      myDataGrid.selectedIndex = newSelectedIndex;
      
                      // Validate the form to make sure that there
                      // is actually a selection (that the grid is 
                      // not empty).
      
                      validateForm();
                  }
                  
                  // Perform simple form validation.
                  private function validateForm():void
      
                  {
                      // Is the form valid?
                      formIsValid = 
                          zapis_jazyk1.text != "" 
                          && zapis_jazyk2.text != "";
      
                          //&& authorInput.text != ""
                          //&& amazonUrlInput.text != "";
                      
                      // Does a selection exist in the data grid?    
                      selectionExists = myDataGrid.selectedIndex != -1;
                  }        
      

       

      And

      <mx:Panel 
              title="Assigning XML data"
              paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"
           x="689" y="10" width="430">
              
              <!-- List of books -->
              <mx:DataGrid 
                  id="myDataGrid" 
                  dataProvider="{soubor_slovicek.slovicka.slovicko}"
                  change="validateForm();"
              >
                  
                  <mx:columns>
                      <mx:DataGridColumn dataField="@cs" headerText="Jazyk1" width="85"/>
                      <mx:DataGridColumn dataField="@en" headerText="Jazyk2"/>
                      
                <!--      <mx:DataGridColumn dataField="author" headerText="Author"/>
                      <mx:DataGridColumn dataField="amazonUrl" headerText="Web site"> 
                          
                          <mx:itemRenderer>
                              <mx:Component>
                                  <mx:LinkButton
                                      label="Visit"
                                      click="navigateToURL(new URLRequest(data.amazonUrl), 'blank');"
                                  />                                    
                              </mx:Component>
                          
                          </mx:itemRenderer>                        
                      </mx:DataGridColumn>  -->
                  </mx:columns>
              </mx:DataGrid>
              
              <!-- New book form. Prepopulated with a book for easier testing. -->
              
              <mx:Form width="100%" autoLayout="false">
                  <mx:FormHeading label="New book details"/>
                  
                  <mx:FormItem label="Jazyk1:" width="
                  100%">
                      <mx:TextInput 
                          id="zapis_jazyk1" 
                          width="100%"
                          text="bagr"
                          change="validateForm();"
                      />
                  
                  </mx:FormItem>
                  <mx:FormItem label="Title:" width="100%">
                      <mx:TextInput 
                          id="zapis_jazyk2" 
                          width="100%"
                          text="the bagrr"
                          change="validateForm();"
                      />
                  
                  </mx:FormItem>
                  <!-- <mx:FormItem label="Author:" width="100%">
                      <mx:TextInput 
                          id="authorInput"
                          width="100%"
                          text="Peter Elst"
                          change="validateForm();"
                      />
                  
                  </mx:FormItem>
                  <mx:FormItem label="Amazon Url" width="100%">
                      <mx:TextInput 
                          id="amazonUrlInput"
                          width="100%"
                          text="http://tinyurl.com/qxon2"
                          change="validateForm();"
                      />
                  
                  </mx:FormItem>  -->      
              </mx:Form>
                      
             <!-- <mx:TextArea id="deb" width="100%"/> -->       
              
              <mx:ControlBar>
                  
                  <mx:Button 
                      label="Add Word"
                      click="addWord();"
                      enabled="{formIsValid}"
                  />
                  <mx:ComboBox id="addPosition" enabled="{formIsValid}">
                      
                      <mx:String>before</mx:String>
                      <mx:String>after</mx:String>
                  </mx:ComboBox>
                  <mx:ComboBox 
                      id="addRelativeTo" 
                      enabled="{formIsValid}"
                  >
                      
                      <mx:String>selected item.</mx:String>
                      <mx:String>the list.</mx:String>
                  </mx:ComboBox>
                  <mx:VRule height="15"/>
                  
                  <mx:Button 
                      label="Delete Word"
                      click="deleteWord();"
                      enabled="{selectionExists}"
                  />
              </mx:ControlBar>
          
          </mx:Panel>
      

      Help??

        • 1. Re: Problem with XML and lists??
          _Natasha_ Level 4

          Hi,

          problem is in adding new child. You have more children rather then in example.

           

          if (addRelativeTo.selectedIndex == SELECTED_ITEM)

           

                          {

                              // Add the new item relative to the selected item.

           

                              var selectedWord:XML = soubor_slovicek.slovicka.slovicko[selectedWordIndex];

           

                              // Does the user want to add it before or after

           

                              // the selected item?

                              if (addPosition.selectedIndex == BEFORE)

                              {   

                                  // Add new item before selected item

                                  soubor_slovicek.slovicka.insertChildBefore(selectedWord, newWord);

                              }

           

                              else

                              {

                                  // Add new item after selected item

                                 soubor_slovicek.slovicka.insertChildAfter(selectedWord, newWord);

                              }

           

                          }

                          else

                          {

                              // Add the new item relative to the whole list of books.

                              if (addPosition.selectedIndex == BEFORE)

           

                              {

                                  // Add new item at the start of the list.

                                 soubor_slovicek.slovicka.prependChild(newWord);

                              }

                              else

           

                              {

                                  // Add new item at the end of the list.

                                 soubor_slovicek.slovicka.appendChild(newWord);

                              }                   

                          }

          • 2. Re: Problem with XML and lists??
            erthy Level 1

            Wonderful! Do you have any kind of a fanclub I could join??