Flex表单自动填充与表单数据自动收集

 如题,以下代码展示如何讲界面的表单域的值自动归集到指定对象,以及如何用指定对象自动填充表单域,思路很简单,但用起来挺方便的,代码结构不算好,但挺稳定的,所以也懒得去重构了。

简单的说下原理:

1 收集表单数据,通过指定容器对象,对容器进行递归遍历,当发现是表单域时,判断对象是否有表单域名称相同的属性,如果有则将表单域的值设置到对象中。

2 同理重置表单和自动填充表单对1做反向操作就行,只是对下拉、radio等会自动定位判断。

问题:

1 当界面的表单值对应到多个对象时要自动封装怎么办,很简单,调用多次wrapperFormData(container:Object,bo:Object)就行。

2 当界面的表单值对应到多个对象时要自动填充怎么办,同样,调用多次fillForm(rec:Object,container:Object)就行。

支持flex3和flex4版本。

 

As代码     收藏代码
  1. package xxx.service  
  2. {  
  3.     import mx.controls.CheckBox;  
  4.     import mx.controls.ComboBox;  
  5.     import mx.controls.DateField;  
  6.     import mx.controls.RadioButton;  
  7.     import mx.controls.RadioButtonGroup;  
  8.     import mx.controls.TextArea;  
  9.     import mx.controls.TextInput;  
  10.     import mx.core.Container;  
  11.     import mx.core.IVisualElementContainer;  
  12.       
  13.     import spark.components.BorderContainer;  
  14.     import spark.components.CheckBox;  
  15.     import spark.components.ComboBox;  
  16.     import spark.components.DropDownList;  
  17.     import spark.components.RadioButton;  
  18.     import spark.components.RadioButtonGroup;  
  19.     import spark.components.TextArea;  
  20.     import spark.components.TextInput;  
  21.   
  22.     /**  
  23.      * 表单数据收集和填充服务  
  24.      * 收集过程:通过遍历容器,提取容器种的表单控件,将表单控件的id与值作为对象的属性与值写入对象  
  25.      * 赋值过程:通过遍历容器,提取容器种的表单控件,如果数据源中包含表单控件的id,则对该控件赋值  
  26.      */  
  27.     public class FormParser  
  28.     {  
  29.         private static var _instance:FormParser=new FormParser();  
  30.         public function FormParser()  
  31.         {  
  32.         }  
  33.         public static function getInstance():FormParser  
  34.         {  
  35.             return _instance;  
  36.         }  
  37.         public function wrapperFormData(container:Object,bo:Object):void  
  38.         {  
  39.             if(bo==null)  
  40.             {  
  41.                 throw new Error("收集表单数据的对象不能为空!");  
  42.             }  
  43.             var count:Number=container.numChildren;  
  44.             for(var i:Number=0;i<count;i++)  
  45.             {  
  46.                 var obj:Object=container.getChildAt(i);  
  47.                 if(isTextField(obj))  
  48.                 {  
  49.                     appendProperty(bo,obj,obj.text);  
  50.                 }  
  51.                 else if(isCheckBox(obj))  
  52.                 {  
  53.                     appendProperty(bo,obj,obj.selected?"1":"0");  
  54.                 }  
  55.                 else if(isRadioButton(obj))  
  56.                 {  
  57.                     if(obj.groupName!=null)  
  58.                     {  
  59.                         bo[obj.groupName]=obj.group.selectedValue;  
  60.                         continue;  
  61.                     }  
  62.                     if(obj.selected)  
  63.                     {  
  64.                         appendProperty(bo,obj,obj.value);  
  65.                     }  
  66.                 }  
  67.                 else if(isComboBox(obj))  
  68.                 {  
  69.                     var result:Object=(obj.selectedItem==null||obj.selectedItem is String||!obj.selectedItem.hasOwnProperty("data"))?null:obj.selectedItem.data;  
  70.                     appendProperty(bo,obj,result);  
  71.                 }  
  72.                 else if(isDropDownList(obj))  
  73.                 {  
  74.                     var dr:DropDownList=obj as DropDownList;  
  75.                     appendProperty(bo,obj,dr.selectedItem.data);  
  76.                 }  
  77.                 else if(isRadioButtonGroup(obj))  
  78.                 {  
  79.                     //取不到  
  80.                 }  
  81.                 else if(obj is Container||obj is IVisualElementContainer)  
  82.                 {  
  83.                     wrapperFormData(obj,bo);  
  84.                 }  
  85.             }  
  86.         }  
  87.   
  88.         /***  
  89.         * 为对象增加指定属性和属性值  
  90.   
  91.         */  
  92.         private function appendProperty(bo:Object,obj:Object,propertyValue:Object):void  
  93.         {  
  94.             if(!obj.hasOwnProperty("id"))  
  95.             {  
  96.                 return;  
  97.             }  
  98.             bo[obj["id"]]=propertyValue;  
  99.         }  
  100.         /***  
  101.         * 重置form中的项  
  102.   
  103.         */  
  104.         public function resetForm(container:Object):void  
  105.         {  
  106.             var count:Number=container.numChildren;  
  107.             for(var i:Number=0;i<count;i++)  
  108.             {  
  109.                 var obj:Object=container.getChildAt(i);  
  110.                 if(isTextField(obj))  
  111.                 {  
  112.                     obj.text="";  
  113.                 }  
  114.                 else if(isCheckBox(obj))  
  115.                 {  
  116.                     obj.selected=false;  
  117.                 }  
  118.                 else if(isRadioButton(obj))  
  119.                 {  
  120.                     if(obj.groupName!=null)  
  121.                     {  
  122.                         obj.group.selectedValue=null;  
  123.                         continue;  
  124.                     }  
  125.                     if(obj.groupName==undefined||obj.groupName=="")  
  126.                     {  
  127.                         obj.selected=false;  
  128.                     }  
  129.                 }  
  130.                 else if(isComboBox(obj))  
  131.                 {  
  132.                     obj.selectedIndex=0;  
  133.                 }  
  134.                 else if(isRadioButtonGroup(obj))  
  135.                 {  
  136.                     obj.selectedValue=obj.getRadioButtonAt(1).value;  
  137.                 }  
  138.                 else if(isDropDownList(obj))  
  139.                 {  
  140.                     obj.selectedIndex=0;  
  141.                 }  
  142.                 else if(obj is Container|| obj is IVisualElementContainer)  
  143.                 {  
  144.                     resetForm(obj);  
  145.                 }  
  146.             }  
  147.         }  
  148.         /***  
  149.         * 根据对象填充表单  
  150.         */  
  151.         public function fillForm(rec:Object,container:Object):void  
  152.         {  
  153.             if(rec==null)  
  154.             {  
  155.                 throw new Error("指定的数据源为空!");  
  156.             }  
  157.             var count:Number=container.numChildren;  
  158.             for(var i:Number=0;i<count;i++)  
  159.             {  
  160.                 var obj:Object=container.getChildAt(i);  
  161.                 var name:String=getObjectName(rec,obj);  
  162.                 if(isFormControl(obj))  
  163.                 {  
  164.                     if(name==null)  
  165.                     {  
  166.                         continue;  
  167.                     }  
  168.                 }  
  169.                 var value:String=getValue(rec,name);  
  170.                 if(isTextField(obj))  
  171.                 {  
  172.                     obj.text=value;  
  173.                 }  
  174.                 else if(isCheckBox(obj))  
  175.                 {  
  176.                     if(value=="1")  
  177.                     {  
  178.                         obj.selected=true;  
  179.                     }  
  180.                 }  
  181.                 else if(isRadioButton(obj))  
  182.                 {  
  183.                     if(obj.groupName!=null)  
  184.                     {  
  185.                         obj.group.selectedValue=value;  
  186.                     }  
  187.                     else  
  188.                     {  
  189.                          if(obj.data==value)  
  190.                          {  
  191.                              obj.selected=true;  
  192.                          }  
  193.                     }  
  194.                 }  
  195.                 else if(isComboBox(obj)||isDropDownList(obj))  
  196.                 {  
  197.                     var source:Object=obj.dataProvider;  
  198.                     var sel:Object=getItemByData(source,value);  
  199.                     if(sel==null)  
  200.                     {  
  201.                         obj.selectedIndex=0;  
  202.                     }  
  203.                     else  
  204.                     {  
  205.                         obj.selectedItem=sel;  
  206.                     }  
  207.                 }  
  208.                 else if(obj is Container||obj is IVisualElementContainer)  
  209.                 {  
  210.                     fillForm(rec,obj);  
  211.                 }  
  212.             }  
  213.         }  
  214.         private function getValue(rec:Object,name:String):String  
  215.         {  
  216.             return name==null?"":rec[name];  
  217.         }  
  218.         private function getObjectName(rec:Object,obj:Object):String  
  219.         {  
  220.             var name:String=obj.hasOwnProperty("id")?obj.id:null;  
  221.             if(isRadioButton(obj)&&obj&&obj.groupName!=null)  
  222.             {  
  223.                 name=obj.groupName;  
  224.             }  
  225.             if(rec.hasOwnProperty(name))  
  226.             {  
  227.                 return name;  
  228.             }  
  229.             return null;  
  230.         }  
  231.         private function isFormControl(obj:Object):Boolean  
  232.         {  
  233.             if(isTextField(obj))  
  234.             {  
  235.                 return true;  
  236.             }  
  237.             if(isCheckBox(obj)||isRadioButton(obj)||isComboBox(obj))  
  238.             {  
  239.                 return true;  
  240.             }  
  241.             return false;  
  242.         }  
  243.         /**  
  244.          * source Combox下拉列表数据源  
  245.          * itemData 下拉数据源中某项的data属性值  
  246.          * return itemData对应的item  
  247.          */  
  248.         public function getItemByData(source:Object,itemData:Object):Object  
  249.         {  
  250.             if(source==null||itemData==null)  
  251.             {  
  252.                 return null;  
  253.             }  
  254.             for each(var obj:Object in source)  
  255.             {  
  256.                 if(obj.data==itemData)  
  257.                 {  
  258.                     return obj;  
  259.                 }  
  260.             }  
  261.             return null;  
  262.         }  
  263.           
  264.         private function isDropDownList(obj:Object):Boolean  
  265.         {  
  266.             return obj is DropDownList;  
  267.         }  
  268.           
  269.         private function isRadioButtonGroup(obj:Object):Boolean  
  270.         {  
  271.             return obj is mx.controls.RadioButtonGroup||obj is spark.components.RadioButtonGroup;  
  272.         }  
  273.         private function isRadioButton(obj:Object):Boolean  
  274.         {  
  275.             return obj is mx.controls.RadioButton||obj is spark.components.RadioButton;  
  276.         }  
  277.           
  278.         private function isComboBox(obj:Object):Boolean  
  279.         {  
  280.             return obj is mx.controls.ComboBox||obj is spark.components.ComboBox;  
  281.         }  
  282.           
  283.         private function isCheckBox(obj:Object):Boolean  
  284.         {  
  285.             return obj is mx.controls.CheckBox||obj is spark.components.CheckBox;  
  286.         }  
  287.           
  288.         private function isTextField(obj:Object):Boolean  
  289.         {  
  290.             if(obj is mx.controls.TextInput||obj is mx.controls.TextArea||obj is mx.controls.DateField||obj is spark.components.TextInput||obj is spark.components.TextArea)  
  291.             {  
  292.                 return true;  
  293.             }  
  294.             return false;  
  295.         }  
  296.     }  
  297. }  

你可能感兴趣的:(Flex表单自动填充与表单数据自动收集)