Manipulate data object in a itemrenderer - Adobe Flex 4 - actionscript-3

I have a simple Itemrenderer with the following code:
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true" width="90" height="90">
<s:VGroup horizontalAlign="center">
<mx:Image source="{data.photo}" toolTip="{data.name}" />
</s:VGroup>
</s:ItemRenderer>
I would like to manipulate the data object before that it's binded to the image attributes (source and tooltip). To do this, I modified the code in this way:
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true" width="90" height="90"
initialize="itemrenderer1_initializeHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function itemrenderer1_initializeHandler(event:FlexEvent):void
{
var obj:Object = this.data;
//here the manipulation
}
]]>
</fx:Script>
<s:VGroup horizontalAlign="center">
<mx:Image source="{data.photo}" toolTip="{data.name}" />
</s:VGroup>
</s:ItemRenderer>
When I try to access to the this.data object, it's always empty! Is there a way to manipulate the data object before binding? Probably i don't have to use this.data, but i cannot find any other object to edit

Another solution would be to override the set data function, like this:
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true" width="90" height="90"
creationComplete="itemrenderer1_creationCompleteHandler(event)"
>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import valueObjects.Product;
[Bindable]
private var prd:Product;
override public function set data(value:Object):void
{
super.data = value;
//here i can access to the this.data object!
this.prd = this.data as Product;
}
]]>
</fx:Script>
<s:VGroup horizontalAlign="center">
<mx:Image source="{prd.photo}" toolTip="{prd.name}" width="70" />
<mx:Label text="{prd.name}"/>
</s:VGroup>

I found it! I can access to the this.data object only when the creation of the ItemRenderer is completed! to this I have to manipulate the object after the creationComplete event!
Here the code:
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true" width="90" height="90"
creationComplete="itemrenderer1_creationCompleteHandler(event)"
>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import valueObjects.Product;
[Bindable]
private var prd:Product;
protected function itemrenderer1_creationCompleteHandler(event:FlexEvent):void
{
//here i can access to the this.data object!
this.prd = this.data as Product;
}
]]>
</fx:Script>
<s:VGroup horizontalAlign="center">
<mx:Image source="{prd.photo}" toolTip="{prd.name}" width="70" />
<mx:Label text="{prd.name}"/>
</s:VGroup>
</s:ItemRenderer>

Although not a new thread, do NOT go with CreationComplete. The event is invoked only once. The data will be set whenever the hosting component decides that the itemRenderer should be re-used. E.g. useVirtualLayout=true for a list.

Related

How to access a public function outside custom component in flex?

I have an application width a mx:ViewStack component with differents view components under each s:NavigatorContent as it follows.
<mx:ViewStack id="vsOne" resizeToContent="true">
<s:NavigatorContent label="First">
<package:MyFirstComponent id="myFirstComponent"/>
</s:NavigatorContent>
<s:NavigatorContent label="Second">
<package:MySecondComponent id="mySecondComponent"/>
</s:NavigatorContent>
</mx:ViewStack>
And this is package.MyFirstComponent's important part..
<s:Button label="Next" click="somethingToGoForward()"/>
What I've tried:
Calling somethingToGoForward() in the view component and triyng to access to a parent vsOne: Don't work.
Calling parent.somethingToGoForward() (when this method is in the same mxml as the ViewStack): Don't work
How can I change my ViewStack's selectedIndex from anywhere outside the mxml file containing it?
Thanks.
This is an example using simple events:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" initialize="application1_initializeHandler(event)"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function application1_initializeHandler(event:FlexEvent):void
{
viewStack.selectedChild=one;
}
protected function one_changeViewHandler(event:FlexEvent):void
{
viewStack.selectedChild=two;
}
protected function two_changeViewHandler(event:FlexEvent):void
{
viewStack.selectedChild=one;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ViewStack id="viewStack" >
<local:ccomp id="one" changeView="one_changeViewHandler(event)" text="LAbel ONE">
</local:ccomp>
<local:ccomp id="two" changeView="two_changeViewHandler(event)" text="LAbel TWO" >
</local:ccomp>
</mx:ViewStack>
</s:Application>
ccomp is a custom component
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Metadata>
[Event(name="changeView", type="mx.events.FlexEvent")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private var _text:String;
public function get text():String
{
return _text;
}
[Bindable]
public function set text(value:String):void
{
_text = value;
}
protected function button1_clickHandler(event:MouseEvent):void
{
this.dispatchEvent(new FlexEvent("changeView",true));
}
]]>
</fx:Script>
<fx:Declarations>
</fx:Declarations>
<s:Button label="Change" click="button1_clickHandler(event)">
</s:Button>
<s:Label fontSize="24" text="{text}">
</s:Label>
</mx:HBox>
Davide

datefield itemrenderer

I try to create an editable datefield itemrenderer.
This itemrenderer works fine if I click on calendar button. But if I put data with keyboard, data isn't updating, if field is focus out data become empty.
See below my code
Datagrid column is like that
<mx:DataGridColumn dataField="echDate"
headerText="Date" headerStyleName="dgHeader"
itemEditor="ui.itemRenderer.irDateD" editorDataField="dateModif"/>
and item editor is
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.events.CalendarLayoutChangeEvent;
import mx.events.FlexEvent;
[Bindable] public var dateModif:String;
override public function set data(value:Object):void{
if(listData){
var newDate:Date;
var value1 = value.echDate;
if (value1 is String){
newDate = new DateUtility().dateStringToObject2(value1);
super.data = newDate;
dfDG.selectedDate = newDate;
dfDG.text = value1;
}
else if (value1 is Date){
super.data = value as Date;
dfDG.selectedDate = value1 as Date;
dfDG.text = new DateUtility().DateAsToString(value1);
}
}
}
protected function dfDG_changeHandler(event:CalendarLayoutChangeEvent):void
{
dateModif=dfDG.text;
}
]]>
</fx:Script>
<mx:DateField id="dfDG"
editable="true"
formatString="DD/MM/YYYY"
yearNavigationEnabled="true"
width="95"
change="dfDG_changeHandler(event)"
>
</mx:DateField>
</s:MXDataGridItemRenderer>
I'll happy if someone could help me to solve that
You can try this solution.
I have inserted the second DG to let you see, if data is updated or not.
I don't have your DateUtility().dateStringToObject2(value1), that's why I use a Validator instead.
The "backward connection" between Renderer and App is implemented by means of Binding.
When you use your DateUtility() to correct the input you can insert result to the text property of the DateField.
<?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">
<fx:Declarations>
<fx:Model id="cells">
<dataset>
<data>
<name>a</name>
<echDate>14/01/2013</echDate>
</data>
<data>
<name>b</name>
<echDate>15/01/2013</echDate>
</data>
</dataset>
</fx:Model>
</fx:Declarations>
<s:VGroup>
<mx:DataGrid
width="386" height="100" dataProvider="{cells.data}"
alternatingItemColors="[0xffffff]"
horizontalGridLineColor="0x999999"
horizontalGridLines="true"
verticalGridLineColor="0x999999"
sortableColumns="false"
resizableColumns="false" selectable="false">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="echDate" headerText="Date"
itemRenderer="com.dgdatefield.irDateD"/>
</mx:columns>
</mx:DataGrid>
<mx:DataGrid
width="386" height="100"
dataProvider="{cells.data}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="echDate" headerText="Date"/>
</mx:columns>
</mx:DataGrid>
</s:VGroup>
</s:Application>
//Renderer
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Declarations>
<mx:DateValidator
id="dateValidator"
source="{dfDG}"
property="text"
inputFormat="DD/MM/YYYY"
wrongLengthError="The date format is not correct"
allowedFormatChars="/ - ."/>
</fx:Declarations>
<fx:Binding source="dfDG.text" destination="data.echDate"/>
<s:HGroup width="100%" horizontalAlign="center">
<mx:DateField
id="dfDG" text="{data.echDate}"
editable="true"
formatString="DD/MM/YYYY"
yearNavigationEnabled="true"
width="120"/>
</s:HGroup>
</s:MXDataGridItemRenderer>
I hope it can help you

FLEX DataGrid Column Modification

I am new in Flex. I want to display data in attached grid format. I found best way to display in DataGrid. But the CIs column has multiple entries so because of that other columns will duplicate. I want to avoid duplicate data in other columns. Attached screenshot is of excel, i want to achieve same format in Flex. I am using Flex 4.5
Best way I see to do what you want is to create a custom item renderer for the CLS column and have it render as a list control. That way, the CLS item in each row will only take up one row instead of multiple rows.
You can get some idea by following code... you can build logic using below concept for your implementation. Below example is sample not complete according to your requirement: -
<?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">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.core.UIComponent;
import mx.events.AdvancedDataGridEvent;
import mx.events.ListEvent;
[Bindable]
private var dpHierarchy:ArrayCollection= new ArrayCollection
([
{name:"Barbara Jennings", address: "Arizona", contact:999970, orders:["1"]},
{name:"Dana Binn", address: "Arizona", contact:9999130, orders:["2"]},
{name:"Joe Smith", address: "California", contact:9992929, orders:["3"]},
{name:"Alice Treu", address: "California", contact:9999230, orders:["4"]}
]);
private function addOrder():void
{
for(var i:int = 0 ; i < dpHierarchy.length; i++)
{
if(dropDownID.selectedItem.name == dpHierarchy[i].name)
{
var itemUpdated:Array = dpHierarchy[i].orders;
itemUpdated.push(orderNumber.text);
dpHierarchy[i].orders = itemUpdated;
dpHierarchy.itemUpdated(dpHierarchy[i]);
break;
}
}
}
]]>
</fx:Script>
<mx:VBox x="30" y="30" width="450" height="400">
<s:HGroup>
<s:Button label="Add Order" click="addOrder()"/>
<s:DropDownList id="dropDownID" dataProvider="{dpHierarchy}" labelField="name" selectedIndex="0"/>
<s:TextInput id="orderNumber" width="100"/>
</s:HGroup>
<mx:AdvancedDataGrid id="myADG"
dataProvider="{dpHierarchy}"
variableRowHeight="true" wordWrap="true">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="name" headerText="Name"/>
<mx:AdvancedDataGridColumn dataField="address" headerText="Address"/>
<mx:AdvancedDataGridColumn dataField="contact" headerText="Contact"/>
<mx:AdvancedDataGridColumn dataField="orders" headerText="Orders" itemRenderer="OrderItemRenderer"/>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:VBox>
</s:Application>
Item Reenderer Name: - OrderItemRenderer
<?xml version="1.0" encoding="utf-8"?>
<s:MXAdvancedDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true" width="90%" height="90%">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var initDG:ArrayCollection = new ArrayCollection();
override public function set data(value:Object):void
{
super.data = value;
initDG = new ArrayCollection(value.orders)
}
]]>
</fx:Script>
<mx:DataGrid id="dataGrid" dataProvider="{initDG}" headerHeight="0" rowCount="{initDG.length}"/>
</s:MXAdvancedDataGridItemRenderer>

ActionScript 3 and mxml

My problem is this:
I want to use mxml for visual element... i want to graphically set components and in an as class to program because it easy to me... How to do this??? I have two classes: one as and one mxml...This is my code:
public class chat extends Application{
private var nc:NetConnection = null;
public var connect:Button;
public var status:Text;
public function VideoChat(){
addEventListener(FlexEvent.APPLICATION_COMPLETE, mainInit);
}
private function mainInit(event:FlexEvent):void{
status.text = "Status quo";
connect.addEventListener(MouseEvent.CLICK, doConnect);
}
and mxml:
<?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" backgroundColor="#FBF8F8"
preloaderChromeColor="#CC3535"
>
<mx:Button x="77" y="547" height="19" label="Connect" id="connect"/>
<mx:UIComponent id="videoReceiveContainer" x="77" y="52" width="500" height="400"/>
<mx:Button x="507" y="547" label="Play" id="play"/>
<mx:Text id="status" x="77" y="460" width="501" height="58"/>
<mx:Button x="297" y="547" label="Publish" id="publish"/>
</s:Application>
First of all, your AS3 code is a liiiittle bit funky.
public class VideoChat extends Application{
private var nc:NetConnection = null;
public var connect:Button;
public var status:Text;
public function VideoChat(){
addEventListener(FlexEvent.APPLICATION_COMPLETE, mainInit);
}
private function mainInit(event:FlexEvent):void{
status.text = "Status quo";
connect.addEventListener(MouseEvent.CLICK, doConnect);
}
}
The name of the class has to match the name of the constructor.
Second, the MXML:
<?xml version="1.0" encoding="utf-8"?>
<local:VideoChat xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:local="*"
minWidth="955" minHeight="600" backgroundColor="#FBF8F8"
preloaderChromeColor="#CC3535"
>
<mx:Button x="77" y="547" height="19" label="Connect" id="connect"/>
<mx:UIComponent id="videoReceiveContainer" x="77" y="52" width="500" height="400"/>
<mx:Button x="507" y="547" label="Play" id="play"/>
<mx:Text id="status" x="77" y="460" width="501" height="58"/>
<mx:Button x="297" y="547" label="Publish" id="publish"/>
</local:VideoChat>
If you notice, I defined the XML namespace 'local' xmlns:local="*". And the s:Application tag has been replaced with local:VideoChat.
Lastly, you asked in a comment what the path was to load the page. It should be ...bin-debug/name.html. This HTML file is a wrapper that displays the compiled SWF of your project.

pass variable from a component to TitleWindow in Flex4

I am trying to access a variable defined in an mxml component from a TitleWindow but I cannot get it. I also declared a variable in my titleWindow which references to the component. And I also tried using parentDocument to access the variable but in vain.
Any precious help on this. Thanks.
See my codes below:
This is my component (Comp.xml) where I have declared the variable testVar.
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%">
<fx:Script>
<![CDATA[
public var testVar:String = "Testing";
]]>
</fx:Script>
</s:Group>
This is my titleWindow code:
<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" left="10"
creationComplete="titlewindow1_initializeHandler(event)"
width="100%" height="100%"
>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import com.Comp;
public var varComp:Comp;
public function titlewindow1_initializeHandler(event:FlexEvent):void
{
//Alert.show(new String(Application));
Alert.show(new String(varComp.testVar));
}
]]>
</fx:Script>
</s:TitleWindow>