Correction: the app is actually removing ½
of the selected items with each click of the button. The new version of the
code below has additional items in the data model in order to make this easier
to test. Any ideas? Am I just missing something
obvious? <?xml version="1.0"
encoding="utf-8"?> <mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" > <mx:Model
id="mTest"> <services> <service
type="Plumbing" cost="$100" description="Fix leaky
drain" /> <service
type="Electrical" cost="$200" description="Wire
stairwell lighting"/> <service
type="Roofing" cost="$300" description="Install vent
pipe boot"/> <service
type="Carpentry" cost="$400" description="Replace
entry door"/> <service
type="Plumbing" cost="$500" description="Install bath
tub"/> <service
type="Electrical" cost="$600" description="Wire home
office"/> <service
type="Roofing" cost="$700" description="Install ice
& water shield"/> <service
type="Carpentry" cost="$800" description="Install
sliding patio door"/> <service
type="Plumbing" cost="$900" description="Install hot
tub"/> <service
type="Electrical" cost="$1000" description="Install
service box"/> <service
type="Roofing" cost="$1100" description="Shingle
one-car garage"/> <service
type="Carpentry" cost="%1200" description="Build small
deck"/> </services> </mx:Model> <mx:ArrayCollection
id="acTest" source="{mTest.services.service}" /> <mx:Script> <![CDATA[ import
mx.collections.ArrayCollection;
private function removeSelected():void{
for(var
i:int = 0; i < dgTest.selectedIndices.length; i++){
acTest.removeItemAt(dgTest.selectedIndices[i]);
}
}
]]> </mx:Script> <mx:VBox
width="400" height="600"> <mx:DataGrid
id="dgTest" dataProvider="{acTest}" width="400"
height="500" allowMultipleSelection="true"> <mx:columns> <mx:DataGridColumn
headerText="Type" dataField="type" /> <mx:DataGridColumn
headerText="Service" dataField="description" /> <mx:DataGridColumn
headerText="Cost" dataField="cost" /> </mx:columns> </mx:DataGrid> <mx:Button
id="btnTest" click="removeSelected();" label="Remove
Selected" /> </mx:VBox> </mx:Application> From: Jim Robson
[mailto:[EMAIL PROTECTED] I am trying to give users the ability to remove selected
items from a DataGrid. The idea is that the user selects the items to remove,
then clicks the “Remove Selected” button. The problem is that if
the user selects more than one item, then not all of the selected items will be
removed. If the user selects 3 items, the app usually removes 2 of them (although
it occasionally only removes 1). If the user selects 4 items, it usually
removes 3, and so forth. Can anyone see the problem in the code below? <?xml version="1.0"
encoding="utf-8"?> <mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Model id="mTest">
<services>
<service type="Plumbing" cost="$100">Fix leaky
drain</service>
<service type="Electrical" cost="$200">Wire stairwell
lighting</service>
<service type="Roofing" cost="$300">Install vent pipe
boot</service>
<service type="Carpentry" cost="$400">Replace entry
door</service>
<service type="Plumbing" cost="$500">Install bath
tub</service>
<service type="Electrical" cost="$600">Wire home
office</service>
<service type="Roofing" cost="$700">Install ice
& water shield</service>
<service type="Carpentry" cost="$800">Install sliding
patio door</service>
</services>
</mx:Model>
<mx:ArrayCollection id="acTest"
source="{mTest.services.service}" />
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private function removeSelected():void{
for(var
i:int=0; i<dgTest.selectedIndices.length; i++){
acTest.removeItemAt(dgTest.selectedIndices[i]);
}
}
]]>
</mx:Script>
<mx:VBox width="400" height="600">
<mx:DataGrid id="dgTest" dataProvider="{acTest}"
width="400" height="500"
allowMultipleSelection="true">
<mx:columns>
<mx:DataGridColumn headerText="Type"
dataField="type" />
<mx:DataGridColumn headerText="Service"
dataField="service" />
<mx:DataGridColumn headerText="Cost"
dataField="cost" />
</mx:columns>
</mx:DataGrid>
<mx:Button id="btnTest" click="removeSelected();" label="Remove
Selected" />
</mx:VBox>
</mx:Application> -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
YAHOO! GROUPS LINKS
|
- [flexcoders] RE: Remove selected items from an ArrayCollection... Jim Robson