PDA

View Full Version : Accessing a Checkbox value in Datagrid ItemRenderer


MartinDowner
06-09-2008, 11:37 PM
Hey, i've got a problem with a project i'm currently dealing with. i've been searching the livedocs, many blogs and of course google but I can't seem to find the information I need.

Background of what I need it to do....
I have an AIR application, accessing a Sqlite database.
On the click of a button, popupmanager launches a new titlewindow, within this window is a bit of a form. Also this popup accesses data from a text field in the main application window.
The form contains information that was queried using the information from the textfield, and pulls back the results.

All this is working fine...

I then have a datagrid at the bottom, containing multiple results of a second query. The query contains the name, and a few other pieces of information about a person or multiple people that were brought back from the query.

The second column of the datagrid is an itemrenderer (id="isEntered") referencing an actionscript component that has within it a hbox and a checkbox.

The fourth column has an inline itemrenderer and an underlying component which pulls information into a combobox from a sqlite query. It also has its default selection as the value pulled from the DB too.

All this is working fine...

What i'm trying to do is submit this "form" i've created but what I need to do is know which people (listed as items in datagrid) have their checkboxes checked, and which don't. And also need to pass through the value of the first field (riderID-pulled from DB, which is listed in first column) and also need to send through the selected item that has been selected in the combobox. Whether the combobox's item has changed or not. I need this relevant information so that on the click of a "submit" button at the bottom of my form I can perform a SQL query updating the relevant results data.


<mx:DataGrid dataProvider="{getRiderDetails}" id="dgRiders" width="100%">
<mx:columns>
<mx:DataGridColumn width="35" id="riderID" dataField="riderID" />

<mx:DataGridColumn id="isEntered" visible="true" dataField="riderID"
itemRenderer="components.RBRenderer" editable="false" />

<mx:DataGridColumn headerText="#" dataField="rider_number" width="35" headerStyleName="centered"/>
<mx:DataGridColumn headerText="Rider Name" dataField="full_name" width="150"/>
<mx:DataGridColumn headerText="Entrant" dataField="entrant"/>
<mx:DataGridColumn headerText="Class" dataField="default_class" textAlign="center" headerStyleName="centered" width="180" editable="false">
<mx:itemRenderer>
<mx:Component>
<mx:HBox height="45">
<mx:Spacer width="100%"/>
<mx:Script>
<![CDATA[
......
SQL CODE HERE
......
]]>
</mx:Script>
<mx:ComboBox dataProvider="{classComboBoxData}" labelField="class_name" initialize="initPopup()" selectedIndex="{data.default_class-1}"></mx:ComboBox>
<mx:Spacer width="100%"/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>


The code for the item renderer is here....


<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" horizontalAlign="center">
<mx:CheckBox id="rdo" selected="true" click="clicked()"/>
</mx:HBox>


I'm sorry if the information i've provided isn't quite enough, i've tried to be as concise as possible without leaving too much out. Just ask if you need anything!

I've only been working with flex for 5 days, so please advise me where I'm going wrong!

Thanks
Martin Downer.

wGrand
06-10-2008, 02:35 AM
Funny, I've been struggling to solve the same problem. I don't know the answer, but here is a snippet of code that tracks which items are selected by UID. What cannot figure out is HOW TO ACQUIRE THE ITEM BY UID. itemToUid() only returns 'null'.

LangTileList.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:TileList xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="acToggled=new ArrayCollection();">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;

[Bindable]
public var acToggled:ArrayCollection;

]]>
</mx:Script>

</mx:TileList>

CheckBoxItemRenderer.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.controls.listClasses.IDropInListItemRenderer" initialize="myTL=this.owner as LangTileList">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.listClasses.BaseListData;
import mx.controls.TileList;
import mx.controls.CheckBox;
import LangTileList;

private var _listData:BaseListData;

private var checkedItems:ArrayCollection;

[Bindable]
public var key:String;

[Bindable("dataChange")]

private var myTL:LangTileList;

public function get listData():BaseListData {
return _listData;
}
public function set listData(value:BaseListData):void {
_listData=value;
myTL=_listData.owner as LangTileList;
key=_listData.uid;
}

private function onChange(e:Event):void {
var i:int=myTL.acToggled.getItemIndex(key);
if(i>=0 && !(e.target as CheckBox).selected)
myTL.acToggled.removeItemAt(i)
else if(i==-1 && (e.target as CheckBox).selected)
myTL.acToggled.addItem(key);

myTL.invalidateList();
}

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(key) {
if(myTL.acToggled.getItemIndex(key)>=0) //check for existence of this item
ckBx.selected=true;
else
ckBx.selected=false;
}
}


]]>
</mx:Script>
<mx:CheckBox id="ckBx" label="{data.label}" change="onChange(event)"/>
</mx:VBox>