How to handle a change in a custom GridItemRenderer - actionscript-3

I'm using a DataGrid in one of my project, with a custom ItemRenderer for one of the column to represent a boolean value as a CheckBox
<s:DataGrid id="clients" resizableColumns="false">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="fullName" headerText="Client name" />
<s:GridColumn dataField="active" headerText="Active?" width="90"
itemRenderer="CheckBoxGridItemRenderer"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
And here is the code of CheckBoxGridItemRenderer:
<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
clipAndEnableScrolling="true">
<fx:Script>
<![CDATA[
override public function prepare(hasBeenRecycled:Boolean):void {
checkBox.selected = Boolean(data[column.dataField]);
}
]]>
</fx:Script>
<s:CheckBox id="checkBox" horizontalCenter="0" />
</s:GridItemRenderer>
Now here is my question: What is the best way to handle the change in data from the class I declared the DataGrid ?
I tried to change the data value from within my custom item renderer class:
private function valueChange():void
{
data.active = checkBox.selected;
}
[...]
<s:CheckBox id="checkBox" change="valueChange()" />
and then listen to CollectionEvent.COLLECTION_CHANGE event on the dataProvider of the DataGrid but I never handle any change. Any thoughts or advices?

Where do you change the data. When an element of data collection changed, the DataGrid can detect nothing unless you dispatch CollectionEvent.COLLECTION_CHANGE for the data collection.
In addition, the data collection should be something like ArrayCollection, not Array.

As I found a solution to my problem, I will post it here, but I leave the question open for some time in case someone come up with a better one.
First I created a CustomEvent (I could use the CollectionEvent class but this way I'm sure I don't mess up with the internal logic of the DataGrid)
package
{
import flash.events.Event;
public class CustomEvent extends Event
{
public static const CHANGE:String = "change";
public var column:String;
public var data:Object;
public function CustomEvent(column:String, data:Object)
{
super(CHANGE, true);
this.column = column;
this.data = data;
}
}
}
Notice the call to super() with bubbles set to true. This is actually needed for the event to be handle here.
Then I modified my CheckBoxGridItemRenderer to dispatch this event when the checkbox is selected/deselected:
<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" clipAndEnableScrolling="true">
<fx:Script>
<![CDATA[
import CustomEvent;
override public function prepare(hasBeenRecycled:Boolean):void {
if (data)
{
checkBox.selected = Boolean(data[column.dataField]);
}
}
private function valueChange():void
{
data[column.dataField] = checkBox.selected;
dispatchEvent(new CustomEvent(column.dataField, data));
}
]]>
</fx:Script>
<s:CheckBox id="checkBox" horizontalCenter="0" change="valueChange()" />
</s:GridItemRenderer>
Finally I only have to listen to my custom event dispatched by the DataGrid instance:
clients.addEventListener(CustomEvent.CHANGE, handleCustomEvent);

Related

call a function when component visibility changes

Pretty simple thing but I am stuck.
Question: I want to call a function when the visibility of my component changes so I can do some clean up.
I have tried using the FlexEvents FlexEvent.HIDE and FlexEvent.SHOW and specified the function to call in my mxml header
show="onShow(event)"
private function onShow(event:FlexEvent):void
{
trace("********** onShow***********");
}
That didn't work. I then tried adding various eventListeners but that didn't work, Am I using listening for the wrong events?
addEventListener(FlexEvent.SHOW, onShow, false, 0, true);
addEventListener(FlexEvent.STATE_CHANGE_COMPLETE, onHide, false, 0, true);
And finally I tried overriding the setter for visible but that threw errors
override public function set visible(value:Boolean):void
{
super.visible = value;
visible = value;
}
You are right when using FlexEvent.HIDE and FlexEvent.SHOW but I don't know why you didn't get what you want. Take this simple example where a button hide a second one and when this last is hidden it disable the first one. A third button show the second one which when it is visible enable the first button :
<?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(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function button_02_hideHandler(event:FlexEvent):void
{
button_01.enabled = false;
}
protected function button_01_clickHandler(event:MouseEvent):void
{
button_02.visible = false;
}
protected function button_03_clickHandler(event:MouseEvent):void
{
button_02.visible = true;
}
protected function init(event:FlexEvent):void
{
button_02.addEventListener(FlexEvent.SHOW, function(e:FlexEvent){
button_01.enabled = true;
})
}
]]>
</fx:Script>
<s:Button id="button_01" x="68" y="93" label="Button 01" click="button_01_clickHandler(event)"/>
<s:Button id="button_02" x="170" y="93" label="Button 02" hide="button_02_hideHandler(event)"/>
<s:Button id="button_03" x="276" y="93" label="Button 03" click="button_03_clickHandler(event)"/>
</s:Application>

Is it possible to refresh a single itemRenderer without refreshing whole dataProvider?

Here's the issue: I've got the list with some dataProvider and itemRendererFunction. I had to decide to use itemRenderFunction because we need to have different renderers for different items in dataProvider. The problem is that when single item in dataProvider changes, I have to refresh whole dataProvider, which is quite problematic in case of big amount of data. Is there any way to refresh a single item in this case?
Example code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application name="list_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
click="onApplicationClick(event)"
>
<fx:Script>
<![CDATA[
import mx.core.ClassFactory;
import renderers.*;
import spark.skins.spark.DefaultItemRenderer;
private function itemRendererFunction(item:Object):ClassFactory
{
var rendererClass:Class = DefaultItemRenderer;
switch (item.type)
{
case "typeA":
rendererClass = ItemRendererTypeA;
break;
case "typeB":
rendererClass = ItemRendererTypeB;
break;
default:
break;
}
return new ClassFactory(rendererClass);
}
protected function onApplicationClick(event:MouseEvent):void
{
var item:Object = dp.getItemAt(0);
item.type = "typeB";
dp.itemUpdated(item);
dp.refresh();
}
]]>
</fx:Script>
<fx:Declarations>
<s:ArrayCollection id="dp">
<fx:Object name="Item 1"
type="typeA"
/>
<fx:Object name="Item 2"
type="typeA"
/>
<fx:Object name="Item 3"
type="typeB"
/>
</s:ArrayCollection>
</fx:Declarations>
<s:List id="list"
labelField="name"
itemRendererFunction="itemRendererFunction"
horizontalCenter="0"
verticalCenter="0"
useVirtualLayout="true"
height="300"
dataProvider="{dp}"
>
</s:List>
</s:Application>
I've placed some traces on addToStage event handlers in item renderers classes. All of them are called at every single click.
As discussed in this SO post, you'll want to avoid returning a new instance of ClassFactory for every invocation of your itemRendererFunction:
private var itemRendererTypeA:ClassFactory = new ClassFactory(ItemRendererTypeA);
private var itemRendererTypeB:ClassFactory = new ClassFactory(ItemRendererTypeB);
private function itemRendererFunction(item:Object):ClassFactory
{
switch (item.type)
{
case "typeA":
return itemRendererTypeA;
break;
case "typeB":
return itemRendererTypeB;
break;
default:
break;
}
}

Main application can't capturing custom event Flex

I have a problem when despatching an custom event from my "ItemRendered" to "Main application",the function "avatarSelectedHandler()" is not called when I click in the "CheckBox" (ItemRendered) located in the "DataGrid", I followed the official doc adobe but unfortunately I have not led to a solution.
below my code :
Try.mxml(Main application)
<?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="initApps()">
<fx:Script>
<![CDATA[
import entity.Avatar;
import events.AvatarSelected;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
[Bindable]
private var listAvatar:ArrayCollection=new ArrayCollection();
public function initApps():void{
getAvatar();
avatarColumn.addEventListener(AvatarSelected.SELECTED_AVATAR,avatarSelectedHandler);
//avatarGrid.addEventListener(AvatarSelected.SELECTED_AVATAR,avatarSelectedHandler);
if(avatarColumn.hasEventListener(AvatarSelected.SELECTED_AVATAR))
Alert.show("EventListener OK ");
}
public function avatarSelectedHandler(event:AvatarSelected):void
{
Alert.show("L'evenement est bien traité sur main");
}
public function getAvatar():void{
var date:Date=new Date();
var avatar1:Avatar=new Avatar();
avatar1.idAvatar=4562;
avatar1.pseudo="X";
var avatar2:Avatar=new Avatar();
avatar2.idAvatar=659;
avatar2.pseudo="Y";
listAvatar.addItem(avatar1);
listAvatar.addItem(avatar2);
}
]]>
</fx:Script>
<s:DataGrid x="158" y="177" width="649" height="194" fontFamily="Times New Roman" id="avatarGrid"
fontSize="15" requestedRowCount="4" textAlign="center" dataProvider="{listAvatar}">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="isAvatar" headerText="Id" ></s:GridColumn>
<s:GridColumn dataField="pseudo" headerText="Pseudo"></s:GridColumn>
<s:GridColumn headerText="Selectionner" width="100" itemRenderer="rendered.CheckRendered" id="avatarColumn"></s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
</s:Application>
My ItemRendered "CheckRendered.mxml"
<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer 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="50" height="27" clipAndEnableScrolling="true">
<fx:Metadata>
[Event(name="avatarSelected", type="events.AvatarSelected")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import entity.Avatar;
import events.AvatarSelected;
import mx.controls.Alert;
protected function cBox_clickHandler(event:MouseEvent):void
{
var eventAvatar:AvatarSelected=new AvatarSelected("avatarSelected",Avatar (data));
dispatchEvent(eventAvatar);
}
]]>
</fx:Script>
<s:CheckBox id="cBox" horizontalCenter="0" click="cBox_clickHandler(event)"/>
</s:GridItemRenderer>
My custom event "AvatarSelected.as"
package events
{
import entity.Avatar;
import flash.events.Event;
import mx.states.OverrideBase;
public class AvatarSelected extends Event
{
public static const SELECTED_AVATAR:String = "avatarSelected";
public var avatar:Avatar;
public function AvatarSelected(type:String, avatar:Avatar)
{
super(type);
this.avatar =avatar;
}
}
}
My VO "Avatar.as"
package entity
{
//[Bindable]
//[RemoteClass(alias="entity.Avatar")]
[Bindable]
public class Avatar
{
public var idAvatar:Number;
public var pseudo:String;
}
}
I opted for another way to capture the event, I created a class SkinDataGrid that extends GridColumn to accept the event as an argument, but it does not work yet!
DataGrid on the Main Application:
<s:DataGrid x="171" y="333" width="649" height="194" fontFamily="Times New Roman" id="avatarGridII"
fontSize="15" requestedRowCount="4" textAlign="center" dataProvider="{listAvatar}">
<s:columns>
<s:ArrayList>
<skin:SkinDataGrid dataField="isAvatar" headerText="Id" ></skin:SkinDataGrid>
<skin:SkinDataGrid dataField="pseudo" headerText="Pseudo"></skin:SkinDataGrid>
<skin:SkinDataGrid headerText="Selectionner" width="100" itemRenderer="rendered.CheckRendered" id="avatarColumnII" avatarSelected="avatarSelectedHandler(event)"></skin:SkinDataGrid>
</s:ArrayList>
</s:columns>
</s:DataGrid>
SkinDataGrid.as
package skin
{
import spark.components.DataGrid;
import spark.components.gridClasses.GridColumn;
[Event(name="avatarSelected", type="events.AvatarSelected")]
public class SkinDataGrid extends GridColumn
{
public function SkinDataGrid()
{
super();
}
}
}
any idea ?
Thank you in advance.
You don't specify what kind of problem do you have (type of error), but from looking at the code I find suspicious, that function avatarSelectedHandler has as argument of MouseEvent type, when you assign it as handler of AvatarSelected type.
You also can try to dispatch this event with bubbling enabled (by default it's set to false in the Event class constructor).
In your case, the AvatarSelected constructor can be changed to smth like that:
public function AvatarSelected(type:String, avatar:Avatar, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.avatar =avatar;
}
And event creating would be like this (attention on the third parameter in the AvatarSelected()):
var eventAvatar:AvatarSelected = new AvatarSelected("avatarSelected", Avatar (data), true);

Simple flex popup with dispatch event in child and listener in parent not working

Parent.mxml
This file is calling the popup class below when the button is clicked.
<?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:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
var popup:pop = null ;
protected function clickHandler(event:MouseEvent):void
{
popup = PopUpManager.createPopUp(this,pop,true) as pop;
popup.ok.addEventListener(MouseEvent.CLICK, captureComments);
popup.close();
}
var str:String="";
private function captureComments():void{
Alert.show("Invoked Event Listener", "Alert");
str=popup.comments;
}
]]>
</fx:Script>
<mx:Button id="id1" label="Click" click="clickHandler(event)"/>
<s:Label id="lbl" height="18" width="282" text={str} />
</s:Application>
Child.mxml
This file is the popup reference, creating popup.
I have configured the close action and cancel buttons to work as needed, but unable to propagate the text entered in comment are back to the parent.
<?xml version="1.0" encoding="utf-8"?>
<mx: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"
layout="vertical" width="524" height="322"
showCloseButton="true"
close="close()" chromeColor="#CCCCCC"
horizontalAlign="center" verticalAlign="middle" color="#000000">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
[Bindable]public var comments:String;
public function close(): void{
PopUpManager.removePopUp(this);
}
private function save(): void{
if(comments == null){
comments = "";
}
dispatchEvent(new ResultEvent(comments));
}
]]>
</fx:Script>
<mx:HBox width="100%" height="70%">
<mx:VBox width="100%" height="100%">
<mx:TextArea id="comment" text="{comments}" width="100%" height="80%" change="comments = event.target.text">
</mx:TextArea>
<mx:HBox horizontalAlign="center" width="100%" height="20%">
<s:Button id="ok" label="OK" chromeColor="#CCCCCC" click="save()" fontWeight="bold"/>
<s:Button id="cancel" label="Cancel" chromeColor="#CCCCCC" click="close()" fontWeight="bold"/>
</mx:HBox>
</mx:VBox>
</mx:HBox>
</mx:TitleWindow>
You are dispatching a ResultEvent, like this:
dispatchEvent(new ResultEvent(comments));
However, the event type is the user entered input. Check out the ResultEvent constructor. The first argument is the event type. I have no idea how you'll be able to consistently listen for an event type that is unknown at compile time.
I think you are trying to pass your custom data back to your parent class. My preference would be to use a custom event for this; but if you wanted to use a ResultEvent you could pass your comments in the result property:
dispatchEvent(new ResultEvent(ResultEvent.RESULT,false,true,comments));
In your parent file, add an event listener like this:
popup.addEventListener(ResultEvent.RESULT, processResult);
You should be able to get at your comments value as a property on the event:
protected function processResult(event:ResultEvent):void{
str=event.result
}
Another thing I've done as well is in the Child Container Create a public variable to pass the function from the parent ie:
in parent:
protected function captureComments(comments:String):void{
str=comments;
}
in child :
declare a public var and add the function to the save() function
public var parentFunction:Function;
private function save(): void{
if(comments == null){
comments = "";
}
parentFunction(comments);
Then when Creating your popup container in the parent:
protected function clickHandler(event:MouseEvent):void
{
popup = PopUpManager.createPopUp(this,pop,true) as pop;
popup.parentFunction = captureComments;
popup.close();
}

Flex 4 component on top of TitleWindow

I have Flex s:Group with some ui elements and I want it to be always (!) on top of any component inside my app, including s:TitleWindow (dialog).
How can I achieve it with Flex 4?
Thanks in advance!
You should use the mx.managers.PopUpManager class. It provides a simple interface to handle displaying content above any other.
You can write your own interface that encapsulates the PopUpManager interface to give you a chance to bring all your "always-on-top" components on top again, even when you add another popup that should appear behind.
Simple example:
public final class AlwaysOnTopPopUpManager {
private static var alwaysOnTopDisplayObjects:Array;
public static function addAlwaysOnTopObject(displayObject:IFlexDisplayObject):void
{
alwaysOnTopDisplayObjects = alwaysOnTopDisplayObjects || [];
if (alwaysOnTopDisplayObjects.indexOf(displayObject) == -1)
{
alwaysOnTopDisplayObjects.push(displayObject);
arrange();
}
}
public static function removeAlwaysOnTopObject(displayObject:IFlexDisplayObject):void
{
if (alwaysOnTopDisplayObjects)
{
var index:int = alwaysOnTopDisplayObjects.indexOf(displayObject);
if (index != -1)
{
alwaysOnTopDisplayObjects.splice(index, 1);
}
}
}
// uses the same interface as mx.managers.PopUpManager
public static function addPopUp(window:IFlexDisplayObject, parent:DisplayObject, modal:Boolean = false, childList:String = null, moduleFactory:IFlexModuleFactory = null):void
{
mx.managers.PopUpManager.addPopUp(window, parent, modal, childList, moduleFactory);
arrange();
}
private static function arrange():void
{
for each (var popup:IFlexDisplayObject in alwaysOnTopDisplayObjects)
{
mx.managers.PopUpManager.bringToFront(popup);
}
}
The class holds a list of objects to appear above any other content. Each time an object is added to that list or to the popups the arrange() method is called and bring all your registered objects to front again.
Add you top component in systemManager.rawChildren after application creationComplete event. See down:
<?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"
creationComplete="application_creationCompleteHandler(event)"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.core.UIComponent;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
protected function application_creationCompleteHandler(event:FlexEvent):void
{
panel.width = width;
panel.height = 300;
systemManager.rawChildren.addChild(panel);
}
protected function addPopup():void
{
PopUpManager.addPopUp(titleWin, this, true);
PopUpManager.centerPopUp(titleWin);
}
protected function button_clickHandler(event:MouseEvent):void
{
addPopup();
}
protected function titleWin_closeHandler(evt:CloseEvent):void
{
PopUpManager.removePopUp(evt.currentTarget as UIComponent);
}
]]>
</fx:Script>
<fx:Declarations>
<s:Panel id="panel" backgroundColor="0x00ff00" alpha="0.9" title="Panel">
<s:layout>
<s:VerticalLayout paddingLeft="50" horizontalAlign="left" verticalAlign="middle" />
</s:layout>
<s:Button id="btn" width="200" height="100" label="create popup"
click="button_clickHandler(event)" />
</s:Panel>
<s:TitleWindow id="titleWin"
title="Spark TitleWindow"
close="titleWin_closeHandler(event);"
width="300">
<s:layout>
<s:VerticalLayout paddingLeft="10" paddingRight="10"
paddingTop="10" paddingBottom="10" />
</s:layout>
<s:Label text="Popup window"
fontSize="24"
width="100%"/>
</s:TitleWindow>
</fx:Declarations>
</s:Application>
If all your elements are in the same MXML file, you simply have to put your s:Group element at the bottom of the file as the last element of it.
If your views are wired more dynamically, then you can force its index with something like this:
var topIndex:int = myParentView.numChildren - 1;
myParentView.setChildIndex(myGroupElement, topIndex);
Where myGroupElement if the reference to your s:Group or its ID.
myGroupElement must already be a child of myParentView when this code is executed.
If your s:Group element is only displayed occasionally, you might be interested in Popups.