0
votes

On a project I've inherited I need to switch from using a spark List component to using an mx Tree component in order to be able to group items into directories. I'm pretty rusty with Flex/XML so I'm wondering if I can get a nudge in the right direction for how to handle this.

My questions (details/data specifics below):

  • How do I detect a 'studentGroup' node from a 'student' node?

  • The Tree Component needs a field to use for the display name. Do I have to have a common name between 'studentGroup' nodes and 'student' nodes?

  • Am I completely doing this wrong?

Previously my XML data was flat (I've stripped out all the detail for clarity):

<studentList>
  <student>
      <studentName>Sam</studentName>
  </student>
   <student>
       <studentName>Ruby</studentName>
   </student>
</studentList>

New format is a mix of groups and individual students:

<studentList>
    <studentGroup>
       <studentGroupName>Chess</studentGroupName>
          <student>
              <studentName>Betty</studentName>
          </student>
        </studentGroup>
    <student>
        <studentName>Sam</studentName>
    </student>
    <student>
        <studentName>Ruby</studentName>
    </student>
</studentList>

The XML is currently being parsed with this (again simplified) code:

for each (var prop:XML in studentsXML.file){
    tempArray = new ArrayCollection();
    for each(var studentProp:XML in prop.studentList.student){
       tempStudent = new Student(studentProp.studentName);
       tempArray.addItem(tempStudent);
    }
}

I need to change it so that for 'studentGroups' I do one thing and for 'students' I handle it like above. In pseudo code it would look like below but I am tripping on syntax (or maybe I am completely off track?).

for each (var prop:XML in studentsXML.file){
    tempArray = new ArrayCollection();
    for each(var studentProp:XML in prop.studentList){

       //HOW DO I DETECT A StudentGroup FROM A Student NODE?

       if (studentList.studentGroup){
          //student group
           tempStudentGroup = new StudentGroup(studentProp.studentGroupName);
             for each(var student:XML in studentList.studentGroup){
               tempStudent = new Student(studentProp.studentName);
               tempStudentGroup.add(tempStudent);
             }

            tempArray.addItem(tempStudentGroup);
       }else{
          //single student
          tempStudent = new Student(studentProp.studentName);
          tempArray.addItem(tempStudent);
       }
    }
}
2

2 Answers

0
votes

I would try something like this:

for each(var studentGroup:XML in prop.studentGroup)
{
    //student group
    for each(var student:XML in studentGroup.student) {
        tempStudent = new Student(studentProp.studentName);
        tempStudentGroup.add(tempStudent);
    }
    tempArray.addItem(tempStudentGroup);
}
for each(var student:XML in prop.student)
{
    //single student
    tempStudent = new Student(studentProp.studentName);
    tempArray.addItem(tempStudent);
}
0
votes

if want to be used in mx:Tree, your xml will like this:

<studentList>
  <student label="Chess">
    <student label="Bett"/>
  </student>
  <student label="Sam"/>
  <student label="Ruby"/>
</studentList>

this means:you should always use unique string to include content in any depth, and mx:Tree will show these as node parent or node, depends on which has children or not.

first question: you can check "hasChildren" to diff student group and student in your itemRenderer. like this:

 override public function set data(value:Object):void {
   if( value != null ) { 
     super.data = value;

     if( TreeListData(super.listData).hasChildren ) {
       ...

your second question:yes, they all use "label" for the display name.