3 Replies Latest reply on Dec 21, 2009 2:09 PM by saisri2k2

    ArrayCollection removeItemAt behavior

    dunn1974

      Hi,

       

      I've got a DataGrid with an ArrayCollection as the dataProvider.  The last cell on each row contains a button that removes that row.  The function for the button is just:

       

      var dg:DataGrid = DataGrid(DataGridListData(listData).owner);

      if(dg.rowCount==1) return;

      var dp:ArrayCollection = dg.dataProvider as ArrayCollection;

      dp.removeItemAt(dp.getItemIndex(data));

      dg.rowCount--;

       

      Another button, outside the DataGrid, allows for adding rows:

       

      var row:Object = {

        dateObject : new Date(),

        quantity : 1,

        lineTotal : 0,

        description : "coding;"

      }

      lineItems.addItem(row);  // lineItems is the dataProvider of the DataGrid

      dg.rowCount++;

       

      Both work fine generally, but I'm seeing 2 strange behaviors:

       

      1.  If the data in a row has been changed (specifically: one cell is a ComboBox, and its selection has changed), when deleting the row, the NEXT row displays that data.  At first I assumed I was just targeting the wrong row, but experimenting (adding +1 or -1 to the getItemIndex return, or getting the index to delete through the data property, or the DataGrid's selectedIndex) shows this is not the case.

       

      2. If the data in a row has been changed (per issue #1) and then deleted, and then a new row is added, that deleted row's changed value will appear when adding the new row.  So if the ComboBox default is "Item 1", and it has been changed to "Item 3", then deleted, then more rows are added, the first newly added row will show "Item 3".  If two or more are deleted, and multiple rows are added again, the duplication will occur for exactly as many changed rows that were deleted.

       

      The behavior described in issue #2 appears to be without regard to issue #1 (for example, when deleting the last row, issue #1 doesn't happen since there is no "next" row - but the duplicated changed value described in issue #2 DOES still happen).

       

      What seems to happen is that removeItemAt takes the item out of the ArrayCollection, but when subsequently calling addItem, it is replaced, rather than the newly generated object being added.  I've tried deleting the reference and setting it to null to qualify it for garbage collection, but have had no success.  Of course, I could very well be misinterpreting the issue entirely.

       

      Any advice would be appreciated.  Thanks.

        • 1. Re: ArrayCollection removeItemAt behavior
          Flex harUI Adobe Employee

          Are you using a custom ComboBox renderer?  If so, post the code.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: ArrayCollection removeItemAt behavior
            dunn1974 Level 1

            package renderers {

             

             

              import flash.events.Event;

             

              import mx.controls.ComboBox;

              import mx.controls.DataGrid;

              import mx.controls.dataGridClasses.*;

             

             

              public class UOMBox extends ComboBox {

             

             

                private function changeHandler(event:Event):void{

                  data.unitRate = selectedItem.RATE;

                  data.unitOfMeasure = selectedItem.LABEL;

                  var total:Number = Number(data.unitRate) * Number(data.quantity);

                  if(isNaN(total)) total = 0;

                  data.lineTotal = total;

                  var dg:DataGrid = DataGrid(DataGridListData(listData).owner);

                  dg.invalidateList();

                }

             

                public function UOMBox():void{

                  labelField = "LABEL";

                  addEventListener("change", changeHandler, false, 0, true);

                }

             

              }

            }

            • 3. Re: ArrayCollection removeItemAt behavior
              saisri2k2 Level 4

              Try putting the 'dp' array collection as the data provider to the DataGrid soon after you delete or update. and make the dp as [bindable].