tags:

views:

22

answers:

1

Hi all!

I have a datagrid in Flex 4 with a NumericStepper as editor for a numeric value. I can set pre-defined maximum and minimum values for the NumericStepper, but I need to be able to have different limits for each row in the DataGrid. How can I do this?

This is the code for the datagrid:

<mx:DataGrid x="0" y="45" width="272" height="525" dataProvider="{dp}" variableRowHeight="true" editable="true" id="equipmentDG" verticalAlign="middle">                
    <mx:columns>                                        
        <mx:DataGridColumn headerText="Benämning" headerStyleName="gridheader" fontSize="12" width="128" dataField="name" editable="false"/>
        <mx:DataGridColumn headerText="Antal" headerStyleName="gridheader" width="40" dataField="antal" editorDataField="value" editable="true">
            <mx:itemEditor>
                <fx:Component>
                    <mx:NumericStepper minimum="0" maximum="50" stepSize="1" width="35" height="20"></mx:NumericStepper>                
                </fx:Component>
            </mx:itemEditor>
        </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>

Edit: Based on Flextras answer, I've changed the NumericStepper row to

<mx:NumericStepper minimum="{data.minNo}" maximum="{data.maxNo}" stepSize="1" width="35" height="20"></mx:NumericStepper>

but now I get a StackOverflowError when I click on the cell to change the value. I posted a new question regarding that here: http://stackoverflow.com/questions/3837185/stackoverflowerror-on-datagrid-row-specific-limits-in-a-numericstepper

+2  A: 

Te DataGrid only creates rows for what is displayed on screen. It does not create rows for each item in your dataProvider. This is called renderer recycling and is done for performance reasons.

I would recommend that you should try to specify the max and min values of your NumericStepper based on elements of your data object, not based on the position of the row. Basically, something like this:

<mx:DataGrid x="0" y="45" width="272" height="525" dataProvider="{dp}" variableRowHeight="true" editable="true" id="equipmentDG" verticalAlign="middle">                
    <mx:columns>                                        
        <mx:DataGridColumn headerText="Benämning" headerStyleName="gridheader" fontSize="12" width="128" dataField="name" editable="false"/>
        <mx:DataGridColumn headerText="Antal" headerStyleName="gridheader" width="40" dataField="antal" editorDataField="value" editable="true">
            <mx:itemEditor>
                <fx:Component>
                    <mx:NumericStepper minimum="{data['min']}" maximum="data['max']}" stepSize="1" width="35" height="20"></mx:NumericStepper>                
                </fx:Component>
            </mx:itemEditor>
        </mx:DataGridColumn>
    </mx:columns>
</mx:DataGrid>
www.Flextras.com
Thanks. Sorry for being unclear, I wasn't trying to do it based on row position, but as you said, on the row data. I also changed data['min'] to data.min, because I got the following warning: "Data binding will not be able to detect changes when using square bracket operator. For Array, please use ArrayCollection.getItemAt() instead." However, now I'm getting a StackOverflow Error (!) when clicking to change the value in the stepper. Any suggestions?
Lizzan
Instead of using binding, listen to the dataChange event of the itemEditor and set the min and max values of the NumericStepper in that.
www.Flextras.com
Thanks! I went with the suggestion I got in my other question, but it's always nice to have options! =)
Lizzan
This one: http://stackoverflow.com/questions/3837185/stackoverflowerror-on-datagrid-row-specific-limits-in-a-numericstepper ? Listening to the dataChange event is always my preference as opposed to overriding the set data method. But, both can be used to accomplish the same thing. :-)
www.Flextras.com
Why do you prefer that? I'm pretty new to Flex, and you seem to be a pro, so I'm curious to learn more.
Lizzan
I pinged my twitter friends and they were mixed. More preferred to override set data than to listen to the dataChange. I personally find listening to the event a bit cleaner; but there is no compelling reason to use one approach over the other.
www.Flextras.com