views:

30

answers:

2

I have a datagrid where users put in different numbers for 3 different columns. These values are then calculated after the user puts in each value for each column. I also have a combobox component inside my datagrid. What I want this combobox to do is perform a different mathematical formula based on what the user selects. For example, in the combobox if the user selects 'Long'(the first option in the combobox) it performs Column1*(col2-Col3)-col4=total column or if the user selects 'Short'(the second option in the combobox) it performs col1*(Col3-col2)+column4=total column. How would I do that? I've tried different ideas none which have seemed to work so any examples or suggestions would be greatly appreciated.

   public function getTotal(item:Object, column:DataGridColumn):String
    {
    switch(comboBox) 
        { 
            case "Long": 
                var sum:int = item.quantity*(item.exit-item.entry)-item.commission;
            return currencyFormatter.format(sum);


            case "Short": 
                sum = item.quantity*(item.entry-item.exit)-item.commission; 
            return currencyFormatter.format(sum);


        }

    }
A: 

Your question is a bit confusing. How can a user "put in different values" that "are then calculated" Users do not usually input calculated values!

That said, it sounds like you need a big switch statement to calculate the values.

switch(comboBox.selectedItem) 
{ 
    case ""ong": 
        calculatedValue = Column1*(col2-Col3)-col4; 
        break; 
    case "Short": 
        calculatedValue = col1*(Col3-col2)+column4; 
        break; 
}

I assume all the values displayed in your DataGrid relate to a specific object created for that row. You can put your calculate function in the renderer for your caluclated column and reference those items of your object's dataProvider directly:

switch(data['comboBoxSelectedItem']) 
{ 
    case ""ong": 
        data['calculatedValue'] = data['Column1Value']*(data['col2Value']-data['Col3Value'])-data['col4Value']; 
        break; 
    case "Short": 
        data['calculatedValue'] = data['col1Value']*(data['Col3Value']-data['col2Value'])+data['column4Value']; 
        break; 
}
www.Flextras.com
Alright so you put me in right direction but I keep getting "Function does not return a value"? ill post the code below.
Louie
Add the code to your original question and it can be formatted nicer.
www.Flextras.com
Alright Ive added it to the original question. Can you explain why I am getting code error 1170 in this function?
Louie
Your function gets the "will not return" a value error, because values are only returned conditionally. There is no guarantee [from the Flex Compiler] that something will be returned. At the end of the function add a "return '0' value or something similar.
www.Flextras.com
A: 

I took the idea that you gave me and used parameters for the function but like I said before I keep running into error 1170. I get that it is saying my function is not returning a value but I dont understand why? Any clarification is more than welcome.

Louie