Access ItemRenderer in a DataGrid

I have a data grid that has a tool for displaying checkbox elements in cloumn to allow row selection:

Main application:

<mx:DataGrid id="dg"> <mx:columns> <mx:DataGridColumn id="ir" itemRenderer="renderers.RowCheckbox" /> <mx:DataGridColumn dataField="Name" headerText="Name" /> &lt/mx:columns> </mx:DataGrid> 

Element rendering element:

 <-- RowCheckbox --> <?xml version="1.0" encoding="utf-8"?> <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center"> <mx:CheckBox id="chk"/> </mx:HBox>
<-- RowCheckbox --> <?xml version="1.0" encoding="utf-8"?> <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center"> <mx:CheckBox id="chk"/> </mx:HBox> 

How can I get the descriptor of a renderer / checkbox element so that I can determine which lines are marked?

+4
source share
4 answers

Just a tip: we had a similar problem in our application, and we solved this by adding the "selected" property to the objects in the datagrid dataparameter. The selected checkbox property was bound to the selected property of our object. To find out which ones were selected, we simply loop the objects in the dataprovider instead of rendering the elements. After many different approaches, this is truly the best option.

If I remember correctly, the problem was that itemrenderers did not remember the selected state correctly, and the datagrid got completely messed up when scrolling up and down. After scrolling, the wrong rows were selected.

Another option would be to dispatch the event in a renderer of the element, which will explode completely before the control on which the datagrid is placed. You can then listen to these events and update your model to reflect the changes.

+9
source

I ran into similar issues with a DataGrid and multiple element handlers and reusing element rendering when scrolling. To access the means of representing the DataGrid elements, I expanded the DataGrid. My first thought was to use ToIndex () indices, followed by indexToItemRenderer (). Unfortunately, these methods did not do what I expected, so I added the indexItemRenderer () method:

package com.whatever.controls {

 import mx.controls.DataGrid; import mx.controls.listClasses.IListItemRenderer; public class CustomDataGrid extends DataGrid { public function CustomDataGrid() { super(); } public function indicesToItemRenderer(rowIndex:int, colIndex:int):IListItemRenderer { var firstItemIndex:int = verticalScrollPosition - offscreenExtraRowsTop; if (rowIndex < firstItemIndex || rowIndex >= firstItemIndex + listItems.length ) { return null; } return listItems[rowIndex - firstItemIndex][colIndex]; } } 

To eliminate the means of reusing items while scrolling, see this article:

http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html

It comes down to redefining the means of data collection and storing properties in the data. For example, I had one column using the CheckBox itemRenderer element and another column using the ComboBox. For both, I listen to the change event and save the selected ones, selectedIndex, etc. Each time you change the properties and redefine the dataset to set these properties:

  override public function set data(value:Object):void { if (value != null) { super.data = value; if (data.hasOwnProperty('selected') && data.selected) { selected = data.selected; } else { selected = false; } } } 
+2
source

You can use the indexToItemRenderer() method open by all subclasses of ListBase.

For instance:

 private function someFunction(index:int):void { var rowCheckbox:RowCheckbox = dg.indexToItemRenderer(index) as RowCheckbox; trace(rowCheckbox.chk.selected.toString()); } 

... where index represents the index of the DataGrid element whose "chk" property you want to check.

0
source

In ItemRenderer, try installing the Checkbox component in VBox .. solves the scroll problem.

-1
source

All Articles