Flex動態(tài)生成可編輯的DataGrid具體實現(xiàn)代碼
更新時間:2013年04月25日 14:52:27 作者:
DataGrid具有以下功能:表頭是動態(tài)生成的、每行都是有序號的、每行都是可以編輯、插入、刪除、修改,接下來為大家分享下Flex如何動態(tài)生成可編輯的DataGrid
一:先說說我寫這個DataGrid具有的功能
1、表頭是動態(tài)生成的。
2、每行都是有序號的。
3、每行都是可以編輯、插入、刪除、修改的。
4、每個單元格都是加驗證的。
5、單元格有些是經(jīng)過渲染生成的比如:Combobox,DateField...
二、說一些實現(xiàn)這些功能的困難
寫這個的時候感覺都是困難不知道,走過來了也就木有神馬啦,最讓我費勁的就是渲染例如:Combobox在渲染的時候不能用ItemRenderer因為他不能綁定值,只能用ItemEditor但是怎樣獲得這個一個經(jīng)過渲染的對象,通過百度不斷地百度,終于發(fā)現(xiàn)了ClassFactory這個工廠可以生產(chǎn)各種想要的組件。各種困難現(xiàn)在都記不起來了,三天時間終于完成啦。為什嗎要這樣寫呢?因為要做數(shù)據(jù)更新系統(tǒng),每年數(shù)據(jù)都會有變化,這樣頁面也需要變化,總不能每年都去改源碼吧,不如想個法子全給他整成動態(tài)的。這就是寫這個的初衷。
三、關(guān)鍵源碼
package datagridview
{
import com.adobe.serialization.json.JSON;
import com.jzh.test.ComboxColumn;
import com.jzh.test.ComboxItem;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.ComboBase;
import mx.controls.ComboBox;
import mx.controls.DateField;
import mx.controls.RadioButtonGroup;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.ClassFactory;
import mx.validators.RegExpValidator;
import spark.components.DropDownList;
public class RendererUtil
{
public function RendererUtil()
{
}
public static function getButtonRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(OperateButtons);
f.properties={};
return f;
}
public static function getNumRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(numLabel);
f.properties={};
return f;
}
public static function getComboxRenderer(arr:ArrayCollection,label:String):ClassFactory{
//應(yīng)該在這里查詢數(shù)據(jù)庫
var f:ClassFactory=new ClassFactory(ComboBox);
f.properties={dataProvider: arr,labelField:label,selectedIndex:'0',selectedItem:'石質(zhì)路面'};//添加屬性,綁定選擇狀態(tài)
return f;
}
public static function getRadioRenderer(label:String):ClassFactory{
var f:ClassFactory=new ClassFactory(ComboBox);
var arr:ArrayCollection=new ArrayCollection();
arr.addItem("是");
arr.addItem("否");
f.properties={dataProvider: arr,labelField:label,selectedIndex:'0',selectedItem:'否'};//添加屬性,綁定選擇狀態(tài)
return f;
}
public static function getDateRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(DateField);
f.properties={formatString:"YYYY-MM-DD",showToday:true};//添加屬性,綁定選擇狀態(tài)
return f;
}
/*本來想在這渲染生成驗證器的,無奈技術(shù)在達(dá)不到*/
public static function getValidateRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(RegExpValidator);
f.properties={ source:"roadcode",
property:"text" ,
expression:"^[0-9]*$",
noMatchError:"填寫驗證不通過時顯示他提示信息" };//添加屬性,綁定選擇狀態(tài)
return f;
}
/*測試用*/
public static function getRenderer(label:String,callback:Function=null):ClassFactory{
var f:ClassFactory=new ClassFactory(numLabel);
f.properties={lab:label,callback:callback};
return f;
}
}
}
以上代碼是渲染器部分。
1、表頭是動態(tài)生成的。
2、每行都是有序號的。
3、每行都是可以編輯、插入、刪除、修改的。
4、每個單元格都是加驗證的。
5、單元格有些是經(jīng)過渲染生成的比如:Combobox,DateField...
二、說一些實現(xiàn)這些功能的困難
寫這個的時候感覺都是困難不知道,走過來了也就木有神馬啦,最讓我費勁的就是渲染例如:Combobox在渲染的時候不能用ItemRenderer因為他不能綁定值,只能用ItemEditor但是怎樣獲得這個一個經(jīng)過渲染的對象,通過百度不斷地百度,終于發(fā)現(xiàn)了ClassFactory這個工廠可以生產(chǎn)各種想要的組件。各種困難現(xiàn)在都記不起來了,三天時間終于完成啦。為什嗎要這樣寫呢?因為要做數(shù)據(jù)更新系統(tǒng),每年數(shù)據(jù)都會有變化,這樣頁面也需要變化,總不能每年都去改源碼吧,不如想個法子全給他整成動態(tài)的。這就是寫這個的初衷。
三、關(guān)鍵源碼
復(fù)制代碼 代碼如下:
package datagridview
{
import com.adobe.serialization.json.JSON;
import com.jzh.test.ComboxColumn;
import com.jzh.test.ComboxItem;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.ComboBase;
import mx.controls.ComboBox;
import mx.controls.DateField;
import mx.controls.RadioButtonGroup;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.core.ClassFactory;
import mx.validators.RegExpValidator;
import spark.components.DropDownList;
public class RendererUtil
{
public function RendererUtil()
{
}
public static function getButtonRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(OperateButtons);
f.properties={};
return f;
}
public static function getNumRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(numLabel);
f.properties={};
return f;
}
public static function getComboxRenderer(arr:ArrayCollection,label:String):ClassFactory{
//應(yīng)該在這里查詢數(shù)據(jù)庫
var f:ClassFactory=new ClassFactory(ComboBox);
f.properties={dataProvider: arr,labelField:label,selectedIndex:'0',selectedItem:'石質(zhì)路面'};//添加屬性,綁定選擇狀態(tài)
return f;
}
public static function getRadioRenderer(label:String):ClassFactory{
var f:ClassFactory=new ClassFactory(ComboBox);
var arr:ArrayCollection=new ArrayCollection();
arr.addItem("是");
arr.addItem("否");
f.properties={dataProvider: arr,labelField:label,selectedIndex:'0',selectedItem:'否'};//添加屬性,綁定選擇狀態(tài)
return f;
}
public static function getDateRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(DateField);
f.properties={formatString:"YYYY-MM-DD",showToday:true};//添加屬性,綁定選擇狀態(tài)
return f;
}
/*本來想在這渲染生成驗證器的,無奈技術(shù)在達(dá)不到*/
public static function getValidateRenderer():ClassFactory{
var f:ClassFactory=new ClassFactory(RegExpValidator);
f.properties={ source:"roadcode",
property:"text" ,
expression:"^[0-9]*$",
noMatchError:"填寫驗證不通過時顯示他提示信息" };//添加屬性,綁定選擇狀態(tài)
return f;
}
/*測試用*/
public static function getRenderer(label:String,callback:Function=null):ClassFactory{
var f:ClassFactory=new ClassFactory(numLabel);
f.properties={lab:label,callback:callback};
return f;
}
}
}
以上代碼是渲染器部分。
相關(guān)文章
如何在Renderer中設(shè)置屬性 Renderer中設(shè)置屬性的方法實例
如何在Renderer中設(shè)置屬性 Renderer中設(shè)置屬性的方法實例,需要的朋友可以參考一下2013-06-06
Flex 基于數(shù)據(jù)源的Menu Tree實現(xiàn)代碼
由外部參數(shù)flashvars指定數(shù)據(jù)源的文件位置或render鏈接,在源數(shù)據(jù)上加href和target屬性來控制打開窗口,可自定義父節(jié)點和子節(jié)點圖標(biāo),不設(shè)置采用系統(tǒng)默認(rèn),感興趣的你可以了解下啊,或許對你有所幫助2013-01-01
flex中使用css樣式修改TextArea滾動條的皮膚代碼
使用css樣式修改TextArea滾動條的皮膚,具體示例代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助2013-08-08
flex中validateall()方法實現(xiàn)多Item驗證且結(jié)果統(tǒng)一提示
在本文將為大家介紹下flex中validateall()方法如何實現(xiàn)多Item驗證且結(jié)果統(tǒng)一提示,具體如下,感興趣的朋友可以參考下2013-09-09
Flex中TabNavigator設(shè)置Tabs樣式思路及源碼
這篇文章主要介紹了Flex中TabNavigator如何設(shè)置Tabs樣式有哪些思路,感興趣的朋友可以看看下面的源碼2014-05-05
Flex中在Tree綁定數(shù)據(jù)后自動展開樹節(jié)點的方法
使用Tree組件在綁定數(shù)據(jù)后自動展開所有樹型節(jié)點(不需要用戶再自己點擊展開節(jié)點,會方 便許多),接下來為大家介紹下具體的實現(xiàn)2014-01-01

