0
votes

I have an ArrayCollection that is the dataProvider for a spark.components.List, which has allowMultipleSelection="true". There is a "Remove Selected Items" button which initiates the removal of all the selected items from the ArrayCollection upon being clicked.

I currently use the following method:

myList.selectedIndices.sort(ascendingSort);

// remove items, counting backwards
for (var i:uint = myList.selectedIndices.length; i > 0; i--) {
  myArrayCollection.removeItemAt(myList.selectedIndices[i-1]);
}

where ascendingSort does what you expect ;). It works fine, and I know that it will always work.

However, I did take note that if I neglected the sort altogether, to my surprise the removal still worked. The reason for this turned out to be that, when the removeItemAt is called, the selectedIndices are correspondingly updated.

So my question is: Can one rely upon a removeItemAt call updating the values in the selectedIndices? or might that turn out to be different between runtimes and/or Flex SDK versions?

Obviously, if it is reliable then leaving out the sort would be a significant improvement.

2

2 Answers

1
votes

Can one rely upon a removeItemAt call updating the values in the selectedIndices?

Apparently in your use case, yes.

or might that turn out to be different between runtimes and/or Flex SDK versions?

It may very well change at some future point, or may have changed before. I know that in my experience with list based classes, sometimes modifying the dataProvider may cause the list to go back into the "no selection" state. Removing a single selectedItem on a list that does not allow multiple selection is a good example of that.

Usually, in the apps I've worked on, I'm not removing items from a list based on the user's selection in a list; instead they are usually removed (or filtered) out based on some criteria in the actual object. And that criteria usually is a Boolean value that relates to a checkbox shown in a DataGrid column.

0
votes
var indexes:Vector.<Object> = list.selectedItems;

while(indexes.length > 0 )
{
    var item:* = indexes.pop();
    var remindex:int = list.dataProvider.getItemIndex(item);

    if (remindex != -1)
    {
        list.dataProvider.removeItemAt(remindex);
    }
}