Combobox selection inside datagrid

742 Views Asked by At

I have a datagrid. In this datagrid I have a combobox item editor. This datagrid also has multiple columns where a user inputs numbers in each column. These numbers are then calculated by formula where the sum is posted in the "total" column. In this combobox there are two options for the user to choose from and each option has a different formula for calculating the inputted numbers. What I want is for when a user chooses "option 1" one formula is used to do the calculation, when "option 2" is chosen by the user then formula two is used to the calculation.

Here's an example:

Combobox Option 1 (formula 1) is chosen by user = (Column2 - Column1) x column3 = "total" column

Combobox Option 2 (formula 2) is chosen by user = (Column1 - Column2) x column3 ="total" column

I realize you would use a conditional such as "if else" statement, but im just not sure how to do it. I've been trying to implement this for a while with no success so any help or suggestions would be greatly appreciated.

2

There are 2 best solutions below

0
On

Listen for the combobox's change event and implement the formula calculation in the change event handler according to the selectedItem.

public function changeEventHandler(event:Event){
   if(ComboBox(evt.target).selectedItem.label == forumla1) {
      //logic
   } else if(ComboBox(evt.target).selectedItem.label == formula2) {
      //logic
   } else {
      //do nothing
   }
}
0
On

That's interesting. You cannot add listeners directly because item renderers are reused and don't keep their identity. Some thoughts on problem:

  • when combobox' selected item is changed, it dispatches bubbling event EVENT.CHANGE.
  • you should make custom renderer for computed columns. When renderer is added to datagrid (use EVENT.ADDED), use owner property (that should be datagrid) to add listener to EVENT.CHANGE. Check that you getting that event (change renderer's text to "got it", for example).
  • now all your computed cells are getting notification when any combobox changed. First, you need to discard events from rows other than item's row. To do that, renderer needs to know its own rowIndex - see Creating custom List renderers, item 2. Compare rowIndex and datagrid's selected index to bail out if they don't match.
  • now you have combobox in event.target, rowIndex and datagrid - that should be enough to get needed formula and data from datagrid's columns.