Flex advanceddatagrid 验证处理
Flex advanceddatagrid 验证处理
2010年08月28日
Validator,是指flex验证器,用以验证输入是否满足条件,比如验证输入内容是否为数字,是否非空等。通常要了解Validator,应该了解Validator的工作原理,flex 默认Validator有哪写验证器,如何开发自定义验证器?
关于上述内容,flex官方文档Using_Flex_4.pdf validating data章节已有详细描述,这里不再赘述。本文重点讨论Flex的表格控件如何验证,以AdvancedDataGrid为例。
思路如下:
1)自定义Column,继承AdvancedDataGridColumn,自定义的Column具有诸多额外的属性及验证功能;
2)循环遍历表格的Column,判断其是否为自定义的Column,如果是则调用自定义的Column进行验证,返回验证结果。
实现如下:
1)创建自定义Column:SelfColumn, SelfColumn有字符串、数字2种验证类型代码如下: package controls.datagrid { import mx.controls.AdvancedDataGrid; import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn; import mx.core.mx_internal; import mx.events.ValidationResultEvent; import mx.validators.StringValidator; import mx.validators.ValidationResult; publicclass SelfColumn extendsAdvancedDataGridColumn{ privatevar _numberValidator:NumberValidator = new NumberValidator(); privatevar _stringValidator:StringValidator = new StringValidator(); publicfunction SelfColumn(columnName:String = null) { super(columnName); } privatevar _required:Boolean = true; privatevar _needValidate:Boolean = true; privatevar _minLength:Object; privatevar _maxLength:Object; /** * 该列的值是否必须输入,默认可以为空
*/
publicfunctionget required():Boolean{
return _required;
}
publicfunctionset required(value:Boolean):void{
this._required = value;
}
publicfunctionset minLength(value:Object):void{
this._minLength = value;
this._stringValidator.minLength = value;
}
/**
* 字符串最小长度
*/
publicfunctionget minLength():Object{
return _minLength;
}
publicfunctionset maxLength(value:Object):void{
this._maxLength = value;
this._stringValidator.maxLength = value;
}
/**
* 字符串最大长度
*/
publicfunctionget maxLength():Object{
returnthis._maxLength;
}
privatevar _minValue:Object;
publicfunctionget minValue():Object{
return _minValue;
}
publicfunctionset minValue(value:Object):void{
_minValue = value;
_numberValidator.minValue = value;
}
privatevar _maxValue:Object;
publicfunctionget maxValue():Object{
return _maxValue;
}
publicfunctionset maxValue(value:Object):void{
_maxValue = value;
_numberValidator.maxValue = value;
}
privatevar _validType:String = "none";
privatestaticconst VALIDTYPE_TEXT:String = "text";
privatestaticconst VALIDTYPE_NUMBER:String = "number";
privatestaticconst VALIDTYPE_NONE:String = "none";
[Inspectable(category="General",enumeration="none,text,number ",defaultValue="none")]
/**
* 验证类型
*/
publicfunctionget validType():String{ return _validType; } publicfunctionset validType(value:String):void{ this._validType = value; // if(value!=VALIDTYPE_NONE) _needValidate = true; else _needValidate = false; } publicfunction validate():ValidationResult{ var result:ValidationResult = null; if(_needValidate){//auto code switch(_validType){ case VALIDTYPE_NUMBER: result = validateNumber(); break; case VALIDTYPE_TEXT: break; } } if(result==null) result = new ValidationResult(false); return result; } privatefunction validateNumber():ValidationResult{ _numberValidator.required = _required; var datagrid:AdvancedDataGrid = mx_internal::owner as AdvancedDataGrid; var dataProvider:Object = datagrid.dataProvider; if(dataProvider!=null && dataProvider.length>0){ for(var i:int = 0; iAdvancedDataGrid = mx_internal::owner as AdvancedDataGrid;
var dataProvider:Object = datagrid.dataProvider;
if(dataProvider!=null && dataProvider.length>0){
for(var i:int = 0; i使用SelfColumn,请看下述简单的Demo AdvancedDataGrid id="adg1" width="100%" height="100%" dataProvider="{dpFlat}" editable="true">