Flex advanceddatagrid 验证处理

  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; i0){
  for(var i:int = 0; i flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="724" height="363" xmlns:datagrid=" controls.datagrid.*"> height="100%" dataProvider="{dpFlat}" editable="true">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值