0

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);
       }
    }
}
4

2 に答える 2

0

mx:Treeで使用したい場合、xmlは次のようになります。

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

つまり、コンテンツを任意の深さに含めるには、常に一意の文字列を使用する必要があります。mx:Treeは、子があるかどうかに応じて、これらをノードの親またはノードとして表示します。

最初の質問:「hasChildren」をチェックして、itemRendererで学生グループと学生を比較できます。このような:

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

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

2番目の質問:はい、それらはすべて表示名に「ラベル」を使用します。

于 2012-10-24T08:19:46.927 に答える
0

私はこのようなことを試してみます:

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);
}
于 2012-10-23T17:20:54.293 に答える