Removing items from a Data Grid

Tags: CFMU,Flex,AS3
Word Count: 401

You may see many posts like this coming from me and to anyone who does Flex development will probably not get much out of them. With that being said for those of us still learning here is a quick tip for removing data from a data grid. In my latest project CFMU the browse button allows you to select a single file or multiple files to be uploaded. At the time I released the project the data grid did not allow multiple selections so if you wanted to remove an item before uploading you had to do it one at a time. This was pretty because all you had to do was reference the selectedIndex of the data grid. When the user selects a file the remove button is enabled and when the remove button is clicked the onRemove method is called.

This worked great but a user suggested that he should be able to remove multiple items at once. I agree and this actually a pretty easy fix. The first thing we need to do is allow a user to select multiple files once they are added to the data grid. The data grid tag has an attribute allowMultipleSelection that accepts a boolean.

Now that the user can select multiple files we need to update our remove method to delete multiple files. We know that calling selectedIndex will tell us what item is selected but the dg also has a property called selectedIndicies. As you can expect this will return an array of selected indexes. To update our remove method all we need to do is loop over that array remove each item.

Comments

#1 Posted By: Johan Posted On: 8/1/08 5:03 PM
I may be wrong but as you delete items the indices of the remaining items changes so you need to loop backwards over the list of indices.

For example the selected indices are 1,2,4 - if I delete 1 then 2 actually points to item that was 3 and 4 to item that was 5 so I end up deleting 3 and then 6 etc.
#2 Posted By: Dan Vega Posted On: 8/1/08 8:35 PM |
Author Comment
You make a good point, but weird enough it seems to work for me. Can you download the latest version and see if it works for you?
#3 Posted By: Johan Posted On: 8/1/08 11:06 PM
It works - from what I can find out the for each loop actually does loop backwards.
#4 Posted By: Johan Posted On: 8/1/08 11:11 PM
May be beter to specifically loop backwards:
http://www.darronschall.com/weblog/archives/000207...
#5 Posted By: Dan Vega Posted On: 8/1/08 11:14 PM |
Author Comment
I am not sure about that, it looks pretty random to me. I just added about 25 items to the grid and selected a bunch of random files, clicked removed and traced the remove order.

removed index 21
removed index 17
removed index 19
removed index 11
removed index 5
removed index 2
#6 Posted By: Gareth Arch Posted On: 8/3/08 10:25 PM
Did you trace all of the selectedIndices array? Perhaps that's getting updated as well (so that everything is shifted when the item is removed e.g. if another selected item was at index = 18 and was greater than the removed item, make it 17). Plus, after it's removed, it's no longer "selected", so it probably should be updating the indices of the selectedIndices array.
#7 Posted By: Dan Vega Posted On: 8/4/08 9:27 AM |
Author Comment
It was a good thought but the indicies are the same. If anyone knows how this works I would be really curious to find the answer now. I selected every other row and traced the item and selected indicies for each iteration.

Removed Item: 12
Selcted Indices: 12,10,8,6,4,2,0
Removed Item: 10
Selcted Indices: 12,10,8,6,4,2,0
Removed Item: 8
Selcted Indices: 12,10,8,6,4,2,0
Removed Item: 6
Selcted Indices: 12,10,8,6,4,2,0
Removed Item: 4
Selcted Indices: 12,10,8,6,4,2,0
Removed Item: 2
Selcted Indices: 12,10,8,6,4,2,0
Removed Item: 0
Selcted Indices: 12,10,8,6,4,2,0
#8 Posted By: Johan Posted On: 8/6/08 6:08 PM
It works because the for-each-in happens to loop backwards AND your selected items are all numbers. I still think it would be advisable to specifically loop backwards using a loop counter
#9 Posted By: Programmer from Armenia ! Posted On: 8/17/08 5:17 AM
Here is correct version:

private function onRemove(event:MouseEvent):void {
var selected:Array = dgFiles.selectedIndices;
for each(var i:uint in selected){
_files.removeItemAt(selected[i]);
selected = dgFiles.selectedIndices;
}
}
#10 Posted By: Peter Posted On: 8/31/08 9:40 AM
hello.

You can also sort an array of selectedIndices in descending order, so it is always the last index to be removed.


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.