2009-12-28 4 views
1

Je suis un peu nouveau à fléchir et je n'arrive pas à résoudre ce problème. quelqu'un peut-il aider. Merci d'avance.Ajouter des enfants dynamiques à un tableau ou une collection de tableaux

J'ai un chemin de liste de chaînes.
chemin 1 - "un/deux/trois"
chemin 2 - "un/deux/quatre"
chemin 3 - "cinq/six"

je besoin d'un DataGrid avancé pour montrer une structure arborescente comme si
un/
... deux/
........ trois/
............ quatre
cinq/
...... .six
mais je veux réaliser ce dynamicall avec des tableaux, des objets, ou un rraycollection (le cas échéant)
Je dois parcourir chaque chemin de corde en utilisant des méthodes de cordes qui ne posent pas de problème mais comment puis-je créer des enfants "DYNAMIQUES" (profondeur)? S'il vous plaît aider comme je suis sur le point de me tirer les cheveux.

+0

Regardez la récursivité. – sharvey

+0

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

Répondre

0

Vous pouvez essayer quelque chose comme:

var paths:Array = ['one/two/three','one/two/four','five/six']; 
var pathsCollection:ArrayCollection = new ArrayCollection(); 

for(var i:int = 0 ; i < paths.length ; i++){ 
    var folderArr:Array = paths[i].split('/'); 
    var folderNum:int = folderArr.length; 
    var folderLabel:String = ''; 
    for(var j:int = 0 ; j < folderNum; j++){ 
     trace(folderLabel+folderArr[j]); 
     pathsCollection.addItem({label:folderLabel+folderArr[j],level:j,path:folderArr}); 
     folderLabel += '...'; 
    } 
} 

et comme dit sharvey, ont un regard sur la récursivité.

+0

Merci George - très apprécié. Je ne comprends pas comment puis-je obtenir ceci pour donner la profondeur ADG ou de l'arbre. 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? acclamations Mitch – mitch

0

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.

+0

merci zhaupin il a beaucoup aidé cependant, le Hierarchi je dois afficher ne doit pas contenir un nouveau chemin un/deux/quatre mais à la place il devrait seulement l'ajouter au chemin original créé pour ressembler: un/deux/trois /quatre cinq/six J'espère que cela a du sens. – mitch

Questions connexes