0 Replies Latest reply on Sep 2, 2007 7:37 PM by JJNewbie

    DataGrid/XMLList/ArrayCollection

    JJNewbie
      Hello,
      I am new in Flex programming. I was trying to display DataGrid with an XMLList dataProvider. I could not make it working when I delete one of the XMLList entry: the DataGrid fails to remove the deleted row. It works fine if I use ArrayCollection dataProvider. Here is my code:

      MyGrid.mxml
      ==========

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application
      xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="absolute"
      creationComplete="srv1.send(); srv2.send()">

      <mx:Script>
      <![CDATA[

      import mx.collections.ArrayCollection;
      import mx.controls.Alert;

      [Bindable]
      private var acMenu:ArrayCollection;

      [Bindable]
      private var xlMenu:XMLList;

      private function addEntry():void{

      //Array Collection method
      var iMenuLen:int = acMenu.length;
      acMenu.addItem(new Object());
      acMenu[iMenuLen].key = "15";
      acMenu[iMenuLen].descr = "Soda";
      acMenu[iMenuLen].price = "1.00";

      //XML List method
      xlMenu += <item key="15">
      <descr>Soda</descr>
      <price>1.00</price>
      </item>;
      }

      private function delEntry():void{
      var i:int;

      //Array Collection method
      if(dgFirst.selectedItem){
      for(i=0; i<acMenu.length; i++){
      if(acMenu .key == dgFirst.selectedItem.key){
      acMenu.removeItemAt(i);
      break;
      }
      }
      }

      //XML List method
      if(dgSecond.selectedItem){
      for(i=0; i<xlMenu.length(); i++){
      if(xlMenu
      .@key == dgSecond.selectedItem.@key){
      delete xlMenu ;
      break;
      }
      }
      }
      }

      ]]>
      </mx:Script>

      <mx:HTTPService id="srv1"
      url="MyData.xml"
      result="acMenu = srv1.lastResult.styles.item"
      resultFormat="object" />

      <mx:HTTPService id="srv2"
      url="MyData.xml"
      result="xlMenu = srv2.lastResult.item"
      resultFormat="e4x" />


      <mx:VBox>
      <mx:DataGrid id="dgFirst" dataProvider="{acMenu}">
      <mx:columns>
      <mx:DataGridColumn headerText="key" dataField="key"/>
      <mx:DataGridColumn headerText="Description" dataField="descr"/>
      <mx:DataGridColumn headerText="Price" dataField="price"/>
      </mx:columns>
      </mx:DataGrid>

      <mx:DataGrid id="dgSecond" dataProvider="{xlMenu}">
      <mx:columns>
      <mx:DataGridColumn headerText="key" dataField="@key"/>
      <mx:DataGridColumn headerText="Description" dataField="descr"/>
      <mx:DataGridColumn headerText="Price" dataField="price"/>
      </mx:columns>
      </mx:DataGrid>
      <mx:Button label="Add" click="addEntry()" />
      <mx:Button label="Delete" click="delEntry()" />
      </mx:VBox>

      </mx:Application>


      and MyData.xml:
      =============
      <styles>
      <item key="1">
      <descr>Brisket</descr>
      <price>4.95</price>
      </item>
      <item key="3">
      <descr>Ribs</descr>
      <price>6.80</price>
      </item>
      <item key="4">
      <descr>Baby Back Ribs</descr>
      <price>7.50</price>
      </item>
      </styles>

      Please help. Thank you.
      JJ.