Adobe Flex Error Trying To Render DropdownList in Grid - apache-flex

Let me start by saying that I know absolutely NOTHING about Adobe Flex. This application was created by a contractor then brought back in-house and none of the developers here know anything about Flex. I am trying to render a simple dropdownlist but receive the error "Access of Undefined Property shipstatusArray".
Here is the xml of the dropdownlist:
<s:GridColumn headerText = "Shipping Status">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<s:DropDownList id="myDDL" width="140" dataProvider="{shipstatusArray}" >
</s:DropDownList>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
Here is the definition:
[Bindable]public var shipstatusArray:ArrayCollection = new ArrayCollection([{id:1, name:'In Progress'},
{id:2, name:'Completed'}]);
I wasn't sure what was needed so here is the entire code page:
<?xml version="1.0" encoding="utf-8"?>
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import mx.utils.ObjectUtil;
import mx.utils.StringUtil;
import spark.events.IndexChangedEvent;
import qs.utils.StringUtils;
[Bindable]public var model:PECToolsModel = PECToolsModel.getInstance();
[Bindable]public var company:CompanyDetailsVO;
[Bindable]public var selectedItem:Object = new Object();
[Bindable]public var mainDataObj:Object = new Object();
[Bindable]public var TypeID:int;
[Bindable]public var DepartmentID:int;
[Bindable]public var gridtype:int;
[Bindable]public var WOVO:WorkOrderHomeVO;
[Bindable]public var EditWODetails:WorkOrderDetailsHome;
[Bindable]public var shipstatusArray:ArrayCollection = new ArrayCollection([{id:1, name:'In Progress'},
{id:2, name:'Completed'}]);
public function dgIbWorkOrders_doubleClickHandler(event:MouseEvent):void
{
if(TypeID == 2)
{
if(dgWorkOrders.selectedItem)
{
if(!(dgWorkOrders.selectedItem.WorkOrderReturnID))
{
model.isFromPendingDocuments = false;
model.isfromCompUserlist = false;
model.isfromMyDashBoard = false;
model.isfromSalesDashBoard = false;
model.reportsObj = null;
model.isFromIBD = false;
model.isFromMenu = false;
Setpop();
}
else
Alert.show(model.AlertWork12 ,model.alertLabel2);
}
else
Alert.show(model.alertLabel3, model.alertLabel2);
}
}
public function Setpop():void
{
WOVO = new WorkOrderHomeVO();
WOVO.WorkOrderID = dgWorkOrders.selectedItem.WorkOrderID;
EditWODetails = model.tabManager.openWO(WOVO );
if(EditWODetails != null)
{
model.WorkOrderIDArray.push(WOVO.WorkOrderID);
new UpdateWorkOrderAuditProcessEvent(String(dgWorkOrders.selectedItem.WorkOrderID), 1,null).dispatch();
EditWODetails.mainDataObj = this;
EditWODetails.mode = "Edit";
EditWODetails.SelectedWOObj = dgWorkOrders.selectedItem as Object;
EditWODetails.isFromCompModule = false;
EditWODetails.isFromWODashboard = true;
EditWODetails.isFromWOModule = false;
EditWODetails.isFromAccDashboard = false;
EditWODetails.gridtype = gridtype;
EditWODetails.CreateControl();
}
}
public function SetLabel():void
{
if(TypeID == 1)//Total
{
if(mainDataObj.WorkOrderlistArray[1].value != 0)
{
frm1.title = selectedItem.name ;
mainvg.visible = true;
mainvg.includeInLayout =true;
}
else
{
frm1.title = selectedItem.name + " : No Data";
mainvg.visible = false;
mainvg.includeInLayout = false;
}
}
else if(TypeID == 2)//inprogress/To be processed
{
if(mainDataObj.WorkOrderlistArray[2].value != 0)
{
frm1.title = selectedItem.name ;
mainvg.visible = true;
mainvg.includeInLayout =true;
}
else
{
frm1.title = selectedItem.name + " : No Data";
mainvg.visible = false;
mainvg.includeInLayout = false;
}
}
else if(TypeID == 3)//Due/processed
{
if(mainDataObj.WorkOrderlistArray[3].value != 0)
{
frm1.title = selectedItem.name ;
mainvg.visible = true;
mainvg.includeInLayout =true;
}
else
{
frm1.title = selectedItem.name + " : No Data";
mainvg.visible = false;
mainvg.includeInLayout = false;
}
}
}
protected function group1_creationCompleteHandler(event:FlexEvent):void
{
SetLabel();
}
public function frm1_ViewHandler(event:Event):void
{
if(dgWorkOrders.selectedItem)
{
printInvoice();
}
else
Alert.show(model.alertLabel3 ,model.alertLabel2)
}
private function printInvoice():void
{
model.isCompanyOrderPayment = false;
var strSessionID:String = "";
strSessionID = Utils.generateRandomString(8);
new UpdateUserSessionForFileViewEvent(model.loggedinUser.UserID, strSessionID, dgWorkOrders.selectedItem.WorkOrderID, this, 2).dispatch(); //TypeID=2, for Invoice PDF
}
public function viewDocumentBySessionID(invoiceSessionID:String):void
{
navigateToURL(new URLRequest(model.WorkOrderUrlPDF + invoiceSessionID),"_blank");
}
]]>
</fx:Script>
<fx:Declarations>
<mx:DateFormatter id="dtFormatter" formatString="MM/DD/YYYY"/>
<mx:DateFormatter id="dtformat" formatString="MM/DD/YYYY H:NN:SS A"/>
</fx:Declarations>
<controls:CustomLayoutPanel id="frm1" width="100%" height="100%" >
<s:VGroup width="100%" height="100%" visible="false" id="mainvg"
includeInLayout="false">
<s:DataGrid id="dgWorkOrders" dataProvider="{mainDataObj.UserWorkorderArray}"
doubleClickEnabled="{mainDataObj.UserWorkorderArray.length > 0}" doubleClick="dgIbWorkOrders_doubleClickHandler(event)"
variableRowHeight="true" width="100%" height="100%"
toolTip="{(mainDataObj.UserWorkorderArray.length > 0)&&(TypeID==2) ? 'Double-Click to view Work Orders' : ''}"
skinClass="com.pec.tools.controls.VerificationGridHeader" >
<s:rowBackground>
<fx:Component>
<home:WorkOrderGridRowHighlightRenderer/>
</fx:Component>
</s:rowBackground>
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="intSSQID" headerText="Company ID" width="90" visible="{TypeID ==2?true:false}">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<s:Label text="{data.intSSQID}" visible="{data.intSSQID > 9990000 ? true : false}" includeInLayout="{data.intSSQID > 9990000 ? true: false}" verticalCenter="0" horizontalCenter="0" verticalAlign="middle"/>
<mx:LinkButton label="{data.intSSQID}" visible="{(data.intSSQID != 0 && data.intSSQID < 9990000)}" includeInLayout="{data.intSSQID != 0 && data.intSSQID < 9990000 }" verticalCenter="0" horizontalCenter="0"
textDecoration="underline" color="blue" click="{outerDocument.dgIbWorkOrders_doubleClickHandler(null)}"/>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
<s:GridColumn dataField="vchinvoiceno" headerText="Invoice #" width="100">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<mx:LinkButton label="{data.vchInvoiceNo}" visible="{data.vchinvoiceno != ''}" includeInLayout="{data.vchinvoiceno != ''}" paddingLeft="15" height="100%"
textDecoration="underline" toolTip="Click here to print invoice" color="blue" click="{outerDocument.frm1_ViewHandler(null)}"/>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
<s:GridColumn dataField="dteInvoiceDate" headerText="Invoice Date" width="100">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer>
<s:Label text="{outerDocument.dtFormatter.format(data.dteInvoiceDate)}" verticalCenter="0" horizontalCenter="0" verticalAlign="middle"/>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
<s:GridColumn id = "compicon" headerText="Company Name" >
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<s:HGroup verticalAlign="middle" width="100%" height="100%" >
<s:Image id="profileImage" source="#Embed('/assets/priority.png')" visible="{data.PriorityIDs != 3}" includeInLayout = "{data.PriorityIDs != 3}" />
<s:Label text="{data.vchCompanyName}" maxDisplayedLines="3" paddingTop="3" paddingLeft="2" width="100%"/>
</s:HGroup>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
<s:GridColumn dataField="WorkOrderStatus" headerText="Status" visible="{TypeID==1||TypeID==3}" width="90" >
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<s:Label text="{data.WorkOrderStatus}" horizontalCenter="0" verticalAlign="middle" verticalCenter="0"/>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
<s:GridColumn headerText="Created Date" >
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<s:Label text="{outerDocument.dtformat.format(data.dteCreatedDate)}" horizontalCenter="0" verticalAlign="middle" verticalCenter="0"/>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
<s:GridColumn headerText = "Shipping Status">
<s:itemRenderer>
<fx:Component>
<s:GridItemRenderer >
<s:DropDownList id="myDDL" width="140" dataProvider="{shipstatusArray}" >
</s:DropDownList>
</s:GridItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
<s:HGroup width="100%" paddingLeft="7" paddingBottom="3" verticalAlign="middle">
<s:Label text="Total WorkOrders: {mainDataObj.UserWorkorderArray.length}" />
<s:HGroup width="100%" horizontalAlign="right" paddingRight="3" visible="{mainDataObj.UserWorkorderArray.length > 0}">
<mx:Box backgroundColor="0xF7FE2E">
<mx:Text text="Work Order in Process by another User" fontWeight="bold"/>
</mx:Box>
</s:HGroup>
</s:HGroup>
</s:VGroup>
</controls:CustomLayoutPanel>
ANY assistance is greatly appreciated!

The code snippet you posted seems to be missing the start tag <fx:Script>. Please add that.
Add double quotes to your values.
[Bindable]public var shipstatusArray:ArrayCollection = new ArrayCollection([{id:"1", name:"In Progress"}, {id:"2", name:"Completed"}]);
Use labelField to indicate which field you want to display.
<s:DropDownList id="myDDL" width="140" dataProvider="{shipstatusArray}" labelField="name" />
You can use this link as a reference.

Related

Connecting Flex to SQLite

In Flash builder, I'm struggling with basics of data retrieval from local database. Using Lita, I created a SQLite database with a single basic (item) table located in a "DAO" folder .It is meant to populate a List. and I have 2 problems:
How to embed the database (with all its pre-populated data) without recreating it from scratch as shown in many tutorials ?
For the purpose of prototyping, how to link the data retrieved a single MXML file directly in the list without creating many other classes (ok, in this cases the number of required classes would be limited) such as :
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title="HomeView" >
<fx:Script>
<![CDATA[
import flash.data.SQLConnection
import flash.data.SQLStatement;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import mx.collections.ArrayCollection;`
private function get myData():ArrayCollection
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = new SQLConnection();
stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath("dao/MyDatabase.db"));
stmt.text = "SELECT id, name FROM Item";
stmt.execute();
var result:Array = stmt.getResult().data;
if (result)
{
var list:ArrayCollection = new ArrayCollection();
list.source(result);
return list;
} else {
return null;
}
}
]]>
</fx:Script>
<s:List id="list" top="0" bottom="0" left="0" right="0"
dataProvider="{myData}" >
<s:itemRenderer>
<fx:Component>
<s:IconItemRenderer label="{myData.name}">
</s:IconItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:View>
For the question 1 you can add the database as an asset of the project, during the export release it will be embeded into the installer then if you want to place it into the localstore folder you can copy/move it from code...
For the number 2
import flash.data.SQLConnection
import flash.data.SQLStatement;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import mx.collections.ArrayCollection;`
[Bindable]private var resultArr:ArrayCollection = new ArrayCollection();
private function getData():ArrayCollection
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = new SQLConnection();
stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath("dao/MyDatabase.db"));
stmt.text = "SELECT id, name FROM Item";
stmt.execute();
var result:Array = stmt.getResult().data;
resultArr = new ArrayCollection();
if (result)
{
resultArr.source = result;
}
}
]]>
</fx:Script>
<s:List id="list" top="0" bottom="0" left="0" right="0"
dataProvider="{resultArr}" labelField="name" >
</s:List>
Thanks to Marcx and Marcos Placona's Blog entry on copying database locally I came up with this:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title="HomeView" >
<fx:Script>
<![CDATA[
import flash.data.SQLConnection
import flash.data.SQLStatement;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import mx.collections.ArrayCollection;
private function get myData():ArrayCollection
{
var myData:String = "dao/MyDatabase.db";
var embededSessionDB:File = File.applicationDirectory.resolvePath(myData);
var writeSessionDB:File = File.applicationStorageDirectory.resolvePath(myData);
// If a writable DB doesn't exist, we then copy it into the app folder so it's writteable
if (!writeSessionDB.exists)
{
embededSessionDB.copyTo(writeSessionDB);
}
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = new SQLConnection();
stmt.sqlConnection.open(File.applicationStorageDirectory.resolvePath(myData));
stmt.text = "SELECT id, name FROM Item";
stmt.execute();
var result:Array = stmt.getResult().data;
stmt.execute();
var result:Array = stmt.getResult().data;
var r:ArrayCollection = new ArrayCollection();
if (result)
{
r.source = result;
return r;
}else{
return null;
}
}
[Bindable]private var resultArr:ArrayCollection = getData();
]]>
</fx:Script>
<s:List id="list" top="0" bottom="0" left="0" right="0"
dataProvider="{myData}" label="name">
</s:List>
</s:View>

How to get selected values (using checkBox) from DataGrid in flex

i have a datagrid which is getting values from a XML file (getting this xml file from database using PHP and HTTP request in flex). i have created a checkbox in every row in data grid. and here is my requirement:
i want to select tow or three check-box and would like to get all the values form that particular ROWs in some form , prefered arraycollection (such that i can pass this array directly to a bar chart) .. can some one help me as i am new to flex .
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="siteData.send()">
<mx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.controls.*;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
[Bindable] private var fullXML:XMLList;
private function contentHandler(evt:ResultEvent):void{
fullXML = evt.result.values;
}
]]>
</mx:Script>
<mx:VBox>
<mx:Label text="This Data Grid is loading the full XML file"/>
<mx:DataGrid width="600" id="datagrid" dataProvider="{fullXML}">
<mx:columns>
<mx:DataGridColumn headerText="Select">
<mx:itemRenderer>
<mx:Component>
<mx:HBox horizontalAlign="center">
<mx:CheckBox id="check"/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn dataField="release_version" headerText="Release"/>
<mx:DataGridColumn dataField="build" headerText="build"/>
<mx:DataGridColumn dataField="time_login" headerText="time_login"/>
<mx:DataGridColumn dataField="time_tunnel" headerText="time_tunnel"/>
<mx:DataGridColumn dataField="rate_login" headerText="time_tunnel"/>
<mx:DataGridColumn dataField="rate_tunnel" headerText="rate_tunnel"/>
</mx:columns>
</mx:DataGrid>
</mx:VBox>
<mx:HTTPService url="http://localhost/php_genxml.php" id="siteData" result="contentHandler(event)" resultFormat="e4x"/>
</mx:Applicaton>
i want to select some check box and want to get the values of all the fields in data-grid correspond to that check-box, can some one help me how to get the selected values (selected values of check-box) in flex and action script.
<mx:itemRenderer>
<mx:Component>
<mx:HBox horizontalAlign="center" verticalAlign="middle">
<mx:Script>
<![CDATA[
var objTemp:Object = new Object();
override public function set data(value:Object):void
{
if(value != null)
{
var xml:XML = XML(value);
super.data = value;
objTemp = outerDocument.xmlToObject(xml.toString());
if(objTemp.story['quiz_score'] != null)
{
chkAssignment.visible = false;
}
else
{
chkAssignment.visible = true;
}
if(objTemp.story.is_selected == false)
{
chkAssignment.selected = false;
}
else
{
chkAssignment.selected = true;
}
}
}
private function deleteAssignment():void
{
if(chkAssignment.selected)
{
outerDocument.isChanged = true;
objTemp.story.is_selected = true;
var xml:XML = outerDocument.objectToXML(objTemp,"record");
var xmlList:XMLList = xml.children();
xml = xmlList[0] as XML;
outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml;
outerDocument.arrAssignment.push({"story_name": XML(outerDocument.dgListeningLog.selectedItem).story_title.toString() ,"student_assignmentId": XML(outerDocument.dgListeningLog.selectedItem).assignment_id.toString(),"session_key": XML(outerDocument.dgListeningLog.selectedItem).session_key.toString(),"selectedIndex": outerDocument.dgListeningLog.selectedIndex.toString()});
}
else
{
outerDocument.isChanged = true;
objTemp.story.is_selected = false;
var xml:XML = outerDocument.objectToXML(objTemp,"record");
var xmlList:XMLList = xml.children();
xml = xmlList[0] as XML;
outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml;
for(var i:int =0; i < outerDocument.arrAssignment.length; i++)
{
if(outerDocument.arrAssignment[i].selectedIndex == outerDocument.dgListeningLog.selectedIndex)
{
outerDocument.arrAssignment.splice(i,1);
break;
}
}
}
}
]]>
</mx:Script>
<mx:CheckBox id="chkAssignment" change="{deleteAssignment();}"/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
here i am storing the selected value or array in another array and when clicking on the remove button it will check and delete the value from the main array that is data provider of the dataGrid.
If you are facing the problem when scrolling the datagrid CheckBox shows wrong value than copy following method from code:
override public function set data(value:Object):void
there are mainly two functions used...
public function objectToXML(obj:Object, qname:String):XML
{
var qName:QName = new QName(qname);
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
public function xmlToObject(value:String):Object
{
var xmlStr:String = value.toString();
var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var resultObj:Object = decoder.decodeXML(xmlDoc);
return resultObj;
}

Flex checkbox control in datagrid

In my flex application im using a datagrid which has
checkbox itemrenderer column
im using the following code
<mx:DataGridColumn headerText="Select" dataField="isSelect" editable="false" textAlign="center" >
<mx:itemRenderer >
<mx:Component>
<mx:CheckBox selected="{data.isSelect}" change="outerDocument.addDetail(data)" >
</mx:CheckBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="FirstName" dataField="firstName" color="black" editable="false" width="125" />
<mx:DataGridColumn headerText="SecondName" dataField="lsecondName" color="black" editable="false" width="125"/>
after selecting the checkbox and im saving the details in my DB.
now,
when i come for fetching data the selected details from the db should be selected in checkbox.
how can i differentiate selected record and unselected Record.?? thanxx in advance
<mx:itemRenderer>
<mx:Component>
<mx:HBox horizontalAlign="center" verticalAlign="middle">
<mx:Script>
<![CDATA[
var objTemp:Object = new Object();
override public function set data(value:Object):void
{
if(value != null)
{
var xml:XML = XML(value);
super.data = value;
objTemp = outerDocument.xmlToObject(xml.toString());
if(objTemp.story['quiz_score'] != null)
{
chkAssignment.visible = false;
}
else
{
chkAssignment.visible = true;
}
if(objTemp.story.is_selected == false)
{
chkAssignment.selected = false;
}
else
{
chkAssignment.selected = true;
}
}
}
private function deleteAssignment():void
{
if(chkAssignment.selected)
{
outerDocument.isChanged = true;
objTemp.story.is_selected = true;
var xml:XML = outerDocument.objectToXML(objTemp,"record");
var xmlList:XMLList = xml.children();
xml = xmlList[0] as XML;
outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml;
outerDocument.arrAssignment.push({"story_name": XML(outerDocument.dgListeningLog.selectedItem).story_title.toString() ,"student_assignmentId": XML(outerDocument.dgListeningLog.selectedItem).assignment_id.toString(),"session_key": XML(outerDocument.dgListeningLog.selectedItem).session_key.toString(),"selectedIndex": outerDocument.dgListeningLog.selectedIndex.toString()});
}
else
{
outerDocument.isChanged = true;
objTemp.story.is_selected = false;
var xml:XML = outerDocument.objectToXML(objTemp,"record");
var xmlList:XMLList = xml.children();
xml = xmlList[0] as XML;
outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml;
for(var i:int =0; i < outerDocument.arrAssignment.length; i++)
{
if(outerDocument.arrAssignment[i].selectedIndex == outerDocument.dgListeningLog.selectedIndex)
{
outerDocument.arrAssignment.splice(i,1);
break;
}
}
}
}
]]>
</mx:Script>
<mx:CheckBox id="chkAssignment" change="{deleteAssignment();}"/>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
here i am storing the selected value or array in another array and when clicking on the remove button it will check and delete the value from the main array that is data provider of the dataGrid.
If you are facing the problem when scrolling the datagrid CheckBox shows wrong value than copy following method from code:
override public function set data(value:Object):void
Are you sure, that data provided to row itemRenderer has correct isSelect property? Try to trace it.

Add Array data to variable

I have a app that currently gets event data from facebook and displays this into a list
<s:List id="eventsDataGrid" width="100%" height="100%" dataProvider="{facebookEvents}">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<fx:Script>
<![CDATA[
import com.facebook.graph.utils.FacebookDataUtils;
private function time2DateStr(time:String):String {
return FacebookDataUtils.stringToDate(time).toLocaleString();
}
]]>
</fx:Script>
<s:HGroup paddingBottom="10" paddingTop="10">
<s:Image source="{data.pic}"/>
<s:VGroup width="100%">
<s:Label text="{data.name}" fontWeight="bold" width="100%"/>
<s:Label text="Where: {data.location}" id="locationtxt"/>
<s:Label text="When: {time2DateStr(data.start_time)}"/>
</s:VGroup>
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
I am trying to add a button that will display the location in a webView but i can not get the app to use the data.location from the array to use it with Google maps (i am just adding the data.location details to the end of a Google maps URL which will then use maps to search for the location
I can get the button to work but it will not search for the location it just either displays an error in flash builder (which i assume is down to me not adding the data to the string correctly) or searches for "data.location"
Here is the current code that will load the URL but with no location
<s:List id="eventsDataGrid" width="100%" height="100%" dataProvider="{facebookEvents}">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<fx:Script>
<![CDATA[
import com.facebook.graph.utils.FacebookDataUtils;
var webView:StageWebView = new StageWebView
var mapURL:String = "http://maps.google.com/maps?f=d&hl=en&saddr="
private function time2DateStr(time:String):String {
return FacebookDataUtils.stringToDate(time).toLocaleString();
}
protected function getDirections_clickHandler(event:MouseEvent):void
{
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, 100, stage.stageWidth, stage.stageHeight);
webView.loadURL(mapURL);
}
]]>
</fx:Script>
<s:HGroup paddingBottom="10" paddingTop="10">
<s:Image source="{data.pic}"/>
<s:VGroup width="100%">
<s:Label text="{data.name}" fontWeight="bold" width="100%"/>
<s:Label text="Where: {data.location}" id="locationtxt"/>
<s:Label text="When: {time2DateStr(data.start_time)}"/>
<s:Label text="Get Directions" click="getDirections_clickHandler(event)" id="getDirections"/>
</s:VGroup>
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
FULL CODE
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable] private var facebookEvents:ArrayCollection;
import com.facebook.graph.FacebookMobile;
private function onLoad():void
{
if(FacebookMobile.getSession() != null) {
getEvents()
} else {
eventsDataGrid.visible=false
NotLogin.visible=true
}
}
private function getEvents():void {
var fql:String = "select name, location, pic, start_time from event where creator = 148839887036 and eid in (select eid from event_member where uid=148839887036)";
FacebookMobile.fqlQuery(fql, handleGetEventsResponse);
}
private function handleGetEventsResponse(events:Object, fail:Object):void {
if (events != null)
facebookEvents = new ArrayCollection(events as Array);
//else
//status = "Error";
}
]]>
<s:List id="eventsDataGrid" width="100%" height="100%" dataProvider="{facebookEvents}">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<fx:Script>
<![CDATA[
import com.facebook.graph.utils.FacebookDataUtils;
var webView:StageWebView = new StageWebView
var mapURL:String = "http://maps.google.com/maps?f=d&hl=en&saddr="
private function time2DateStr(time:String):String {
return FacebookDataUtils.stringToDate(time).toLocaleString();
}
protected function getDirections_clickHandler(event:MouseEvent):void
{
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, 100, stage.stageWidth, stage.stageHeight);
webView.loadURL(mapURL);
}
]]>
</fx:Script>
<s:HGroup paddingBottom="10" paddingTop="10">
<s:Image source="{data.pic}"/>
<s:VGroup width="100%">
<s:Label text="{data.name}" fontWeight="bold" width="100%"/>
<s:Label text="Where: {data.location}" id="locationtxt"/>
<s:Label text="When: {time2DateStr(data.start_time)}"/>
<s:Label text="Get Directions" click="getDirections_clickHandler(event)" id="getDirections"/>
</s:VGroup>
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
all i needed to do was to add the data into the brackets (dont know the offical name) here it is
protected function directions_click (location:String):void
{
navigateToURL(new URLRequest('http://maps.google.co.uk/maps?q=' + location));
}
and for the button/ label
<s:Label text="Get Directions" fontFamily="lucida grande" fontSize="14" color="#3b5998" click="directions_click(data.location)"/>

Flex/Actionscript - capturing canvas bitmap with dynamically placed elements

I'm attempting to find overlap between elements on a flex canvas, an adaptation of
http://www.gskinner.com/blog/archives/2005/08/flash_8_shape_b.html
The attempt here is to place some text and figure overlap with previously placed text.
The simple example below illustrates the problem.
Both
ImageSnapshot.captureBitmapData(canvas);
or
BitmapData.draw(canvas);
do not seem to capture the elements placed on the canvas dynamically.
Any clues on how I can accomplish this?
Thanks in advance for any help.
-vivek
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Image;
import mx.controls.Text;
import mx.graphics.ImageSnapshot;
public function init():void {
var l:Label = new Label();
l.text = 'Dynamic Label!';
l.x = text.x+50;
l.y = text.y;
canvas1.addElement(l);
var bounds:Rectangle = text.getBounds(this);
trace(bounds.top + ',' + bounds.left + ',' + bounds.width + ',' + bounds.height);
var bmd:BitmapData = new BitmapData(text.width, text.height);
bmd.draw(text);
var bm:Bitmap = new Bitmap(bmd);
var img:Image = new Image();
img.source = bm;
img.x = 0;
img.y = 20;
canvas2.addChild(img);
var c2:BitmapData = ImageSnapshot.captureBitmapData(canvas1);
var bmd2:BitmapData = new BitmapData(text.width,text.height);
bmd2.copyPixels(c2,bounds,new Point(0,0));
var bm2:Bitmap = new Bitmap(bmd2);
var img2:Image = new Image();
img2.source = bm2;
img2.x = 0;
img2.y = 50;
canvas2.addChild(img2);
var c3:BitmapData = new BitmapData(canvas1.width, canvas1.height);
c3.draw(canvas1);
var bmd3:BitmapData = new BitmapData(text.width,text.height);
bmd3.copyPixels(c3,bounds,new Point(0,0));
var bm3:Bitmap = new Bitmap(bmd2);
var img3:Image = new Image();
img3.source = bm3;
img3.x = 0;
img3.y = 50;
canvas3.addChild(img3);
}
]]>
</fx:Script>
<mx:Canvas id="canvas1" width="400" height="100" backgroundColor="#FF0000">
<s:Label id="text" x="200" y="50" text="This is a test"/>
</mx:Canvas>
<mx:Canvas id="canvas2" y="100" width="400" height="100" backgroundColor="#00FF00"/>
<mx:Canvas id="canvas3" y="200" width="400" height="100" backgroundColor="#0000FF"/>
</s:Application>
Calling addChild doesn't make the component immediately visible/available within its parent. You need the creation process to complete before you grab the bitmap, which involves multiple phases/events. Put your grabbing code into a separate method and call it AFTER the creation process completes for your dynamically created component. Do that by using the callLater method, which will put your method call at the end of the event queue. Here's your code with callLater added (not that pretty, but hopefully it gets the point across):
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Image;
import mx.controls.Text;
import mx.graphics.ImageSnapshot;
public function init():void {
var l:Label = new Label();
l.text = 'Dynamic Label!';
l.x = text.x+50;
l.y = text.y;
canvas1.addElement(l);
this.callLater(addRect);
}
private function addRect():void {
var bounds:Rectangle = text.getBounds(this);
trace(bounds.top + ',' + bounds.left + ',' + bounds.width + ',' + bounds.height);
var bmd:BitmapData = new BitmapData(text.width, text.height);
bmd.draw(text);
var bm:Bitmap = new Bitmap(bmd);
var img:Image = new Image();
img.source = bm;
img.x = 0;
img.y = 20;
canvas2.addChild(img);
var c2:BitmapData = ImageSnapshot.captureBitmapData(canvas1);
var bmd2:BitmapData = new BitmapData(text.width,text.height);
bmd2.copyPixels(c2,bounds,new Point(0,0));
var bm2:Bitmap = new Bitmap(bmd2);
var img2:Image = new Image();
img2.source = bm2;
img2.x = 0;
img2.y = 50;
canvas2.addChild(img2);
var c3:BitmapData = new BitmapData(canvas1.width, canvas1.height);
c3.draw(canvas1);
var bmd3:BitmapData = new BitmapData(text.width,text.height);
bmd3.copyPixels(c3,bounds,new Point(0,0));
var bm3:Bitmap = new Bitmap(bmd2);
var img3:Image = new Image();
img3.source = bm3;
img3.x = 0;
img3.y = 50;
canvas3.addChild(img3);
}
]]>
</fx:Script>
<mx:Canvas id="canvas1" width="400" height="100" backgroundColor="#FF0000">
<s:Label id="text" x="200" y="50" text="This is a test"/>
</mx:Canvas>
<mx:Canvas id="canvas2" y="100" width="400" height="100" backgroundColor="#00FF00"/>
<mx:Canvas id="canvas3" y="200" width="400" height="100" backgroundColor="#0000FF"/>
</s:Application>

Resources