一:先說說我寫這個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{
//應該在這里查詢數(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ù)在達不到*/
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;
}
}
}
以上代碼是渲染器部分。
您可能感興趣的文章:- Flex3 DataGrid拖拽到ClumnChart動態(tài)顯示圖表實現(xiàn)代碼
- Flex DataGrid DataGridColumn數(shù)據(jù)顏色多樣化-類型替換
- Flex中讓鼠標移至AdvancedDataGrid的行上不自動修改顯示效果