2011-09-21 82 views
0

我有一个循环,通过书中的数据并显示它。这本书在布局上并不一致,所以我试图用两种不同的方式展示它。第一种方法(正常工作)是将该部分的文本加载到面板并显示它。第二种方法是创建一个新面板(面板创建正常),然后将可折叠面板(嵌套)添加到该面板。这是来自else循环的代码。动态地将容器添加到动态容器中

else if (newPanel == false){ 
        // simpleData is just for the title bar of the new panel 
        // otherwise the panel has no content 
        var simpleData:Section = new Section; 
        simpleData.section_letter = item.section_letter; 
        simpleData.letter_title = item.letter_title; 
        simpleData.section_id = item.section_id; 
        simpleData.title = item.title; 
        simpleData.bookmark = item.bookmark; 
        simpleData.read_section = item.read_section; 

        var display2:readPanel02 = new readPanel02; 
        //item is all the data for the new text 
        display2.id = "panel"+item.section_id; 
        //trace(display2.name);//trace works fine 
        // set vars is how I pass in the data to the panel 
        display2.setVars(simpleData); 
        studyArea.addElement(display2); // displays fine 
        newPanel = true; 

        //this is where it fails 
        var ssPanel:subSectionPanel = new subSectionPanel; 
        //function to pass in the vars to the new panel 
        ssPanel.setSSVars(item); 
        //this.studyArea[newPanelName].addElement(ssPanel); 
        this["panel"+item.section_id].addElement(ssPanel); 

我得到的错误是:在components.readTest没有找到物业panel4.4并没有默认值:的ReferenceError:错误#1069。

我曾尝试设置“名称”属性而不是“id”属性。任何帮助将不胜感激。我很难过。谢谢。

+0

我不明白你要完成的任务。你只是想显示书籍数据?不要专注于如何,但什么。从一瞥你的代码我可以告诉你,你在做什么远非最佳。 –

+0

@J_A_X我不知道你是否看到我自己回答。你对那里的代码有什么看法?我对优化知之甚少,所以我想知道你的意见。 – ChristopherMarcel

+0

再一次,我不能评论,因为我不知道你想用这些完成什么。你试图解决什么问题? –

回答

0

所以这里是我想出的解决方案。我使新的readPanel创建了子面板。我添加了else语句以帮助它更有意义。 readPanel创建第一个子面板,并且对于子面板的每个后续需要,它都会按名称引用其他面板,并在创建新子面板的面板中调用公共功能。下面是创建主面板代码:

else if (newPanel == false){ 
        var display2:readPanel02 = new readPanel02; 
        studyArea.addElement(display2); 
        display2.name = "panel"+item.section_id; 
        display2.setVars(item); 

        newPanel = true; 

       } 
       else{ 
        var myPanel:readPanel02 = studyArea.getChildByName("panel"+item.section_id) as readPanel02; 
        myPanel.addSubSection(item); 

       } 

这里是功能面板:

public function setVars(data:Section):void 
     { 
      myS = data; 
      textLetter = myS.section_letter; 
      textLetterTitle = myS.letter_title; 
      textSection = myS.section_id; 
      textTitle = myS.title; 
      myS.bookmark == 0 ? bookmarked = false : bookmarked = true; 
      myS.read_section == 0 ? doneRead = false : doneRead = true; 
      showTagIcon(); 
      showReadIcon(); 
      addSubSection(myS); 

     } 

     public function addSubSection(data:Section):void{ 
      var ssPanel:subSectionPanel = new subSectionPanel; 
      ssPanel.setSSVars(data); 
      myContentGroup.addElementAt(ssPanel, i); 
      i++; 
     }