Enrico Foschi’s official blog

Something about me – .NET Team Leader with an entrepreneurial mind

Posts Tagged ‘Tutorials

Disabling tree nodes in Flex 2

without comments

[ENGLISH]

Well, today i had to disable some tree nodes in my tree view, but i didn’t find how to. In Flex 1 I could directly use the row property in this way:

my_tree.rows[node]._alpha = 20;
my_tree.rows[node].enabled = false;
my_tree.rows[node].onPress = null;

In Flex 2, the best way is to work with XMLListCollection object properties.
So, I have created an enabled property, that has the following value:
- 1: the node is enabled
- 0: the node is disabled

Now, for disabling the node, I make a strange operation. If the node clicked is disabled, I select manually the previous selected node (or nothing if there wasn’t any node selected).
So, I have to declare a current_node_selected variable.

Then, in the click event, I have added the following instruction:

if(tree_group.selectedItem.@enabled != "1") {
   tree_group.selectedItem = current_node_selected;
}
Here is the complete code:
<![CDATA[
	var current_node_selected:Object = null;
	private function treeClick(e:Event):void {
	if(tree_group.selectedItem.@enabled != "1") {
   		 tree_group.selectedItem = current_node_selected;
	} else {
		current_node_selected = tree_group.selectedItem;
		// my actions for enabled nodes
	}
}
]>

<mx:Tree id="tree_group" change="treeClick(event)" />

[ITALIANO]

Buongiorno a tutti. Il problema di oggi era il disabilitare alcuni nodi di un tree component in Flex 2. In Flex 1 non avrei avuto problemi… avrei utilizzato un codice simile al seguente sfruttando la proprietà row:

my_tree.rows[node]._alpha = 20;
my_tree.rows[node].enabled = false;
my_tree.rows[node].onPress = null;

In Flex 2 purtroppo non posso utilizzare la stessa sintassi. La soluzione migliore che ho trovato riguarda l’utilizzo di una proprietà di ogni nodo della sorgente di dati XMLListcollection.
Ho deciso a questo punto di utilizzare la proprietà enabled (inizializzata a 1 quando il nodo è attivo).

Ora, per disabilitare il nodo, eseguo delle strane operazioni. Se il nodo cliccato è disabilitato (enabled non è uguale a 1), seleziono manualmente il nodo precedentemente selezionato (o niente se non ve ne era alcuno). Utilizzo quindi una variabile current_node_selected che memorizza continuamente il nodo appena selezionato.

Al click eseguo queste istruzioni:

if(tree_group.selectedItem.@enabled != "1") {
	tree_group.selectedItem = current_node_selected;
}

Di seguito il codice completo

<![CDATA[

	var current_node_selected:Object = null;
	private function treeClick(e:Event):void {
		if(tree_group.selectedItem.@enabled != "1") {
			tree_group.selectedItem = current_node_selected;
		} else {
			current_node_selected = tree_group.selectedItem;
			// my actions for enabled nodes
		}
	}
]]>
<mx:Tree id="tree_group" change="treeClick(event)" />

Written by Enrico Foschi

February 8, 2007 at 2:11 pm

How to include an XML into a DataGrid using custom columns – come includere un XML all’interno di un DataGrid utilizzando colonne personalizzate

without comments

Screenshot

[ENGLISH]

Today I am working about registered user views in DataGrid and I have to view all of them in a DataGrid. I can get them by an XML generated by a back-end script.

The code i have used is this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas implements="RMGConnect.Services.RMGIService" xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#009900" width="100%" height="100%">
	<mx:Script>
		<![CDATA[
			import RMGConnect.Core.*;
			[Bindable]
			private var user_data:XML = <preferences>
				<item id="1" userid="admin" />
				<item id="2" userid="collaborator" />
				<item id="3" userid="editor" />
				<item id="4" userid="vice-admin" />
				<item id="5" userid="alphatester" />
				<item id="6" userid="betatester" />
				<item id="7" userid="omegatester" />
				<item id="8" userid="dummyuser" />
			</preferences>

			public function startService():void {
				RMGCommon.showDebug(dg_users.columns.toString());
			}
		]]>
	</mx:Script>
	<mx:DataGrid dataProvider="{user_data.item}"  id="dg_users" width="100%" height="100%">
		<mx:columns>
			<mx:DataGridColumn headerText="id" dataField="@id" />
			<mx:DataGridColumn headerText="user-id" dataField="@userid" />
		</mx:columns>
	</mx:DataGrid>
</mx:Canvas>

Binding the XML foo-variables with the DataGrid, automatically i get an XMLListCollection of all “item” nodes. Using mx:columns and mx:DataGridColumn can set header text and source data of every column of my DataGrid.

[ITALIANO]

Oggi sto sviluppando un servizio che consenta di visualizzare una lista completa di tutti gli utenti registrati in un DataGrid. La lista la ottengo da un XML generato in automatico dai servizi back-end. Il codice che ho utilizzato è il seguente:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas implements="RMGConnect.Services.RMGIService" xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#009900" width="100%" height="100%">
	<mx:Script>
		<![CDATA[
			import RMGConnect.Core.*;
			[Bindable]
			private var user_data:XML = <preferences>
				<item id="1" userid="admin" />
				<item id="2" userid="collaborator" />
				<item id="3" userid="editor" />
				<item id="4" userid="vice-admin" />
				<item id="5" userid="alphatester" />
				<item id="6" userid="betatester" />
				<item id="7" userid="omegatester" />
				<item id="8" userid="dummyuser" />
			</preferences>

			public function startService():void {
				RMGCommon.showDebug(dg_users.columns.toString());
			}
		]]>
	</mx:Script>
	<mx:DataGrid dataProvider="{user_data.item}"  id="dg_users" width="100%" height="100%">
		<mx:columns>
			<mx:DataGridColumn headerText="id" dataField="@id" />
			<mx:DataGridColumn headerText="user-id" dataField="@userid" />
		</mx:columns>
	</mx:DataGrid>
</mx:Canvas>

Effettuando un binding tra la variabile XML (contenente dati fasulli) ed il DataGrid, automaticamente ottengo una XMLListCollection di tutti i nodi “item”, che posso gestire comodamente nelle colonne tramite i tag mx:columns e mx:DataGridColumn con gli attributi headerText (titolo della colonna) e dataField (dato da visualizzare).

Written by Enrico Foschi

February 2, 2007 at 10:45 am