How to get selected values (using checkBox) from DataGrid in flex - apache-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;
}

Related

Adobe Flex Error Trying To Render DropdownList in Grid

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.

Populating combobox with data from a datagrid, receiving data from a sqlite database

I'm still a newbie to Adobe Air/Flex, and still fairly new with SQL.
I've downloaded this (http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air…-part-1/) code and have been looking over it and I'm trying to implement a different idea.
I'm using a canvas for this form.
MaterialForm.mxml is called by a panel CadastroMaterial.mxml which also show a datagrid with the information saved from this form.
the sql statements are made by MaterialEvent.as using the lib flexlib.
I'm having a problem getting data from a datagrid to a combobox, I'm doing this way to get the data:
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" width="388" height="475" creationComplete="init()" label="{_material.material_id > 0 ? _material.tipo : 'Novo Material'}">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
import Eventos.MaterialEvent;
import Eventos.TMaterialEvent;
import Formularios.TMaterialForm;
import mx.collections.ArrayCollection;
[Bindable] private var tMateriais:ArrayCollection;
// Para criar a conexão com o banco de dados
private function openDatabase():void
{
var file:File = File.applicationDirectory.resolvePath("bd/SISC.db");
sqlConnection = new SQLConnection();
sqlConnection.open(file);
// Para verificar se o banco de dados já existe se sim utiliza-o senão cria um novo, não é útil se não cria a tabela
//var isNewDB:Boolean = !file.exists;
//if (isNewDB) createDatabase();
findAll();
}
// Para selecionar a tabela do banco de dados desejada
private function findAll():void
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = "SELECT tmaterial FROM TMATERIAL";
stmt.execute();
tMateriais = new ArrayCollection(stmt.getResult().data);
}
[Bindable] public var _material:Object;
public var sqlConnection:SQLConnection;
private var validators:Array;
private function init():void
{
validators = [tipoValidator, responsavelValidator, compartimentoValidator];
openDatabase();// colocado para abrir o banco de outro lugar
}
public function set material(material:Object):void
{
_material = material;
}
public function get material():Object
{
return _material;
}
private function save():void
{
if (Validator.validateAll(validators).length>0)
{
return;
}
_material.tipo = tipo.text;
_material.num = num.text;
_material.responsavel = responsavel.text;
_material.compartimento = compartimento.text;
_material.observacoes = observacoes.text;
if (_material.material_id > 0)
{
update();
}
else
{
insert();
}
}
private function insert():void
{
try
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text =
"INSERT INTO material (tipo, num, responsavel, compartimento, observacoes) " +
"VALUES (:tipo, :num, :responsavel, :compartimento, :observacoes)";
stmt.parameters[":tipo"] = _material.tipo;
stmt.parameters[":num"] = _material.num;
stmt.parameters[":responsavel"] = _material.responsavel;
stmt.parameters[":compartimento"] = _material.compartimento;
stmt.parameters[":observacoes"] = _material.observacoes;
stmt.execute();
_material.material_id = stmt.getResult().lastInsertRowID;
label = _material.tipo;
dispatchEvent(new MaterialEvent(MaterialEvent.CREATE, _material, true));
}
catch (error:SQLError)
{
Alert.show(error.details, "Erro");
}
}
private function update():void
{
try
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text =
"UPDATE material set " +
"tipo=:tipo, " +
"num=:num, " +
"responsavel=:responsavel, " +
"compartimento=:compartimento, " +
"observacoes=:observacoes " +
"WHERE material_id=:materialId";
stmt.parameters[":tipo"] = _material.tipo;
stmt.parameters[":num"] = _material.num;
stmt.parameters[":responsavel"] = _material.responsavel;
stmt.parameters[":compartimento"] = _material.compartimento;
stmt.parameters[":observacoes"] = _material.observacoes;
stmt.parameters[":materialId"] = _material.material_id;
stmt.execute();
label = _material.tipo;
dispatchEvent(new MaterialEvent(MaterialEvent.UPDATE, _material, true));
}
catch (error:SQLError)
{
Alert.show(error.details, "Error");
}
}
private function deleteItem():void
{
try
{
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = "DELETE FROM material WHERE material_id = :materialId";
stmt.parameters[":materialId"] = _material.material_id;
stmt.execute();
dispatchEvent(new MaterialEvent(MaterialEvent.DELETE, _material, true));
}
catch (error:SQLError)
{
Alert.show(error.details, "Erro");
}
}
]]>
</mx:Script>
<mx:Validator id="tipoValidator" required="true" source="{tipo}" property="text"/>
<mx:Validator id="responsavelValidator" required="true" source="{responsavel}" property="text"/>
<mx:Validator id="compartimentoValidator" required="true" source="{compartimento}" property="text"/>
<mx:Form width="381" height="466">
<mx:FormItem label="Tipo:" required="true">
<mx:ComboBox dataProvider="{tMateriais}" id="tipo" text="{_material.tipo}" width="200"/>
</mx:FormItem>
<mx:FormItem label="Número:">
<mx:TextInput id="num" text="{_material.num}" width="200"/>
</mx:FormItem>
<mx:FormItem label="Responsavel:" required="true">
<mx:TextInput id="responsavel" text="{_material.responsavel}" width="200"/>
</mx:FormItem>
<mx:FormItem label="Compartimento:" required="true">
<mx:TextInput id="compartimento" text="{_material.compartimento}" width="200"/>
</mx:FormItem>
<mx:FormItem label="Observações:">
<mx:TextInput id="observacoes" text="{_material.observacoes}" width="200"/>
</mx:FormItem>
</mx:Form>
<mx:Button label="Salvar" click="save()" left="16" bottom="20"/>
<mx:Button label="Deletar" click="deleteItem()" left="87" bottom="20"/>
but as a result appears in the combobox [object Object] and not the name of the type of material inserted in the table TMATERIAL, if I insert 30 values ​​in Table tmaterial in another form, the 30 appear in combobox as [object Object] I traced this and the error are in this form. Could anyone help me? Sorry the english (google translator). Thnx.
The problem is the ComboBox needs a hint as to how it should show the name for each item in the dataProvider. By default, many Flex components look for the elements in the dataProvider to have a property named label. If such a property exists, and it is a String, the ComboBox will display the value of that label property.
If the elements in the dataProvider don't have a label property, then the Flex component will call toString() on the dataProvider object which in this case results in the output "[object Object]".
If the elements in your dataProvider do not have a label property, then you can tell the ComboBox how to display the name by using either the labelField or labelFunction properties of the ComboBox.
Use the labelField property to specify the name of the property in your dataProvider that can be used as the label (in your case I believe this is tipo or :tipo)
<mx:ComboBox dataProvider="{tMateriais}" id="tipo" labelField="tipo" />
Or use the labelFunction property specify a function that will be used to generate the label for each item in the dataProvider.
<mx:ComboBox dataProvider="{tMateriais}" id="tipo" labelFunction="myLabelFunction" />
The labelFunction has the following method signature:
private function myLabelFunction(dataProviderItem : Object) : String

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>

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.

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