2011-02-03 57 views
0

我正在設計一個數據錄入應用程序,允許多個條目到其主題。例如,一個人可能已經接受了來自多個機構的教育。每個教育項目是一個表單,應用程序用戶可以點擊一個按鈕添加另一個條目,這是一個空白但相同的形式。動態Flex表單複製

我想它涉及狀態和自定義窗體組件,但不知道如何將所有東西都放在一起。有人可以闡明如何做到這一點?一些示例代碼將不勝感激。

在此先感謝,

回答

0

如果有要求,是否需要flex版本?或者只是使用最新的Flex 4代碼?一般來說,在Flex中這是一項非常簡單的任務,您可以在MXML中創建AnEntry.mxml中的類定義,它僅僅是一個TextInput(或者標籤和TextInput或任何您需要的每個條目)。在Flex 3中,您在按鈕的點擊處理程序中調用this.addChild(new AnEntry())或在Flex 4中調用this.addElement(new AnEntry());.要提交,你需要做一個從0開始的循環,並轉到this.numChildren,並將每個TextInput作爲參數傳遞給HTTPService。下面

兩個文件對編譯的Flex 3.4

[AnEntry.mxml]

<?xml version="1.0" encoding="utf-8"?> 
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml"> 
    <mx:Label text="Link:"/> 
    <mx:TextInput id="theTextInput"/> 
</mx:Box> 

[MainApplication.mxml]

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
       layout="vertical" 
       minWidth="955" 
       minHeight="600"> 

    <mx:Script> 
     <![CDATA[ 
      import mx.rpc.http.HTTPService; 
      protected function button1_clickHandler(event:MouseEvent):void 
      { 
       var params:Object = {}; 
       var paramCount:Number=0; 
       // TODO Auto-generated method stub 
       for(var i:Number = 0; i<numChildren; i++) 
       { 
        var currentChild:DisplayObject = getChildAt(i); 
        if(currentChild is AnEntry) 
        { 
         params[paramCount++] = (currentChild as AnEntry).theTextInput.text; 
        } 
       } 
       var httpService:HTTPService = new HTTPService(); 
       httpService.method = "POST" 
       httpService.url = "http://www.shaunhusain.com/somewhere.php"; 
       httpService.send(params); 
      } 
     ]]> 
    </mx:Script> 


    <mx:Button label="Add Entry" click="this.addChild(new AnEntry())"/> 
    <mx:Button label="Submit Info" click="button1_clickHandler(event)"/> 
</mx:Application> 

請讓我知道,如果這能讓你在去正確的方向,如果你是Flex的一名初學者,在一週的視頻中搜索Flex,他們是一個很好的教程,讓你開始使用Flex開發。

修訂針對Flex 4:

[MainApplication.mxml]

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> 
    <s:layout> 
     <s:VerticalLayout/> 
    </s:layout> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <fx:Script> 
     <![CDATA[ 
     import mx.rpc.http.HTTPService; 
     protected function button1_clickHandler(event:MouseEvent):void 
     { 
     var params:Object = {}; 
     var paramCount:Number=0; 
     // TODO Auto-generated method stub 
     for(var i:Number = 0; i<numChildren; i++) 
     { 
     var currentChild:DisplayObject = getChildAt(i); 
     if(currentChild is AnEntry) 
     { 
     params[paramCount++] = (currentChild as AnEntry).theTextInput.text; 
     } 
     } 
     var httpService:HTTPService = new HTTPService(); 
     httpService.method = "POST" 
     httpService.url = "http://www.shaunhusain.com/somewhere.php"; 
     httpService.send(params); 
     } 
     ]]> 
    </fx:Script> 


    <mx:Button label="Add Entry" click="this.addElement(new AnEntry())"/> 
    <mx:Button label="Submit Info" click="button1_clickHandler(event)"/> 

</s:Application> 

[AnEntry.mxml]

<?xml version="1.0" encoding="utf-8"?> 
<mx:Box xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <mx:Label text="Link:"/> 
    <mx:TextInput id="theTextInput"/> 
</mx:Box> 
+0

我也被複雜化的東西。無論如何,你的代碼明確指出了我的正確方向。我在Flex 4中運行代碼時做了小的更改以反映Spark,但未按預期方式向容器添加可視元素。我錯過了什麼嗎?再次感謝分享智慧。 – Jay 2011-02-03 18:57:39