views:

1178

answers:

2

Hi,

I have a datagrid. I add a row to the datagrid using an ADD button. Once I add, I sort the datagrid based on a column. I also provide the serial numbers i.e. row numbers as first column to the datagrid. But, the serial number function does not apply after sorting. Hence, a new row added for e.g. row 5, based on sorting should be row 1, the serial number displayed is still row 5. The UI looks bad as numbers are not in correct order. the code is :


// Sorting Function :

  private function sortGrid():void 
  {
        sortGridColl = new ArrayCollection(sortGridArray);
            sortA = new Sort();
            sortByLevel = new SortField("Type", true, false);
            sortA.fields=[sortByLevel];
         sortGridColl.sort=sortA;
         sortGridColl.refresh();
         sortGrid.dataProvider=sortGridColl;
    sortGrid.rowCount=myDPColl.length +1; 
  }        

// Serial Number function :

private function sortGridSerialNumbers(oItem:Object,iCol:int):String
{
         myDPColl = new ArrayCollection(sortGridArray);
        var iIndex:int = myDPColl.getItemIndex(oItem) + 1;
        return String(iIndex);
}

// Adding new row to datagrid :

sortGrid.dataProvider.addItem
(
{
    Type : typeName.text
}
);
A: 

Is sortGridSerialNumbers supposed to return the current serial numbers after sorting? Because it seems to only display the serial numbers for your original state. It won't give the correct value for any item that was added since you added the item to the dataProvider, not the original array.

CookieOfFortune
Yes, I want the sortGridSerialNumbers to return current serial numbers after sorting. I tried pushing values inside the array, but that is actually making serial number count increase by 2 instead of increasing by 1. Also, i dont know if after adding to array it wil work or not. The thing is the serial number function works on an ArrayCollection and not an array. So, I create an array Collection myDPColl. My sortGrid Arrauy looks something like this :<pre> private var sortGridArray:Array = [{ Type:"Level0"}</pre>
Why not just keep your data in an ArrayCollection then? It seems you want to use the binding functionality in ArrayCollections.
CookieOfFortune
I should also add that when an ArrayCollection has sorting on, newly added items are inserted into their sorted positions. Your serial number function can then listen for the collectionChanged event and add/replace the serial numbers.
CookieOfFortune
In my above code, where and how do I push into an array collection. Also, how do I call the collection change event. Can you elaborate ?
Well, sortGridColl is an ArrayCollection, you should just use it and never refer to sortGridArray again. All modifications will then be made to sortGridColl, that is what you do when you add a new row to the dataGrid: "sortGrid.dataProvider.addItem" calls sortGridColl.addItem, but does not go back to sortGridArray. You can listen to collectionChanged events by adding a listener to the ArrayCollection, eg. sortGridColl.addEventListener(CollectionEvent.COLLECTION_CHANGED, listener);
CookieOfFortune
A: 

I like arrays better than array collections, so my choice would be to maintain the list as a raw array. Then on each add, push the new element, do a sortOn (myArray.sortOn("Type")), then iterate over all items to adjust the serial number. Then apply the array to the datagrid as a new dataprovider.

Glenn