La récursion est la méthode à suivre lorsque vous travaillez sur des objets contenant un nombre inconnu d'éléments. Essayez cet exemple d'application:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="{init();}"
layout="vertical"
verticalAlign="middle">
<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import mx.collections.HierarchicalData;
private var paths:Array = ['one/two/three','one/two/four','five/six'];
private static const DELIMITER:String = "/";
private function init():void {
var test:Array = buildHierarchy(paths);
dg_test.dataProvider = new HierarchicalData(test);
trace(ObjectUtil.toString(test));
}
private function buildHierarchy(arr:Array):Array {
var ret:Array = new Array();
var o:Object = new Object();
/* Loop over the paths array */
for (var i:int = 0; i < arr.length; i++) {
/* Split the string block according to the delimiter */
var parts:Array = String(arr[i]).split(DELIMITER);
if (parts.length) {
/* Make a new object with a label equal to the first string element */
o = new Object();
o.label = parts[0];
/* Remove the first item in the string list */
parts.splice(0, 1);
/* Important - If the string has remaining members, call this
function again with the remaining members. Assign this to
the 'children' property of the newly created object */
if (parts.length > 0)
o.children = buildHierarchy([parts.join(DELIMITER)]);
/* Add the object to the new array */
ret.push(o);
}
}
return ret;
}
]]>
</mx:Script>
<mx:AdvancedDataGrid id="dg_test" height="200" width="400">
<mx:columns>
<mx:AdvancedDataGridColumn id="col_label" dataField="label"/>
</mx:columns>
</mx:AdvancedDataGrid>
Cette fonction s'appeler une fois pour chaque élément contenu dans le bloc 'string/string/string'. La clé pour obtenir cette structure affichée dans l'ADG consiste à définir adg.dataProvider = new HierarchicalData (myArray);
Espérons que cela fonctionne! Je ne peux pas obtenir le code au format 100% mais vous devriez avoir l'idée. N'oubliez pas d'ajouter la balise d'application de fermeture.
Regardez la récursivité. – sharvey
Merci sharvey, Récursivité? Êtes-vous capable de pointer dans la direction spécifique où il y a un exemple d'application de ceci à un arbre ou adg? Bravo Mitch – mitch