Here's the sample of code for the DG, DGColumns and Renderer.  I'm
dynamically adding columns to a DG, so I don't have mxml to show.  The
addColumn method is used to dynamically add.  I don't have a code test
case to provide, since it would be too large.  Here is how I call the
addColumn routine:

LPA_businessRiskName.addColumn("Business Risk Name",
"businessRiskName", 200, true, new ClassFactory(TextInEmbedded), obj,
svco);

Any help would be greatly appreciated.  Thanks a bunch.

John

Code snippets follow:


/* method used to dynamically add columns to data grid */

public function addColumn(header:String, fieldName:String,
width:int,                      visible:Boolean, itemRenderer:IFactory=null,
defaultValue:Object=null,                               
vco:ValidatorConfigObject=null):void {
                                
                var newColumn:DataGridColumnValidated = new 
DataGridColumnValidated();
                        if (vco != null)
                                newColumn.validatorConfigType = vco;
                                                                        
                                newColumn.visible = visible;
                                // get current columns
                                var gridColumns:Array = dg.columns;
                                newColumn.headerText = header;
                                newColumn.dataField = fieldName;
if (vco != null) {
        newColumn.deepDataField = fieldName + ".value";                         
        newColumn.dataField = fieldName + ".value";
}
else
        newColumn.deepDataField = null;
        newColumn.width = width;
        newColumn.editable=true;
        if (itemRenderer != null) {
                                        
        var rendererItem:Object = itemRenderer.newInstance();

if (rendererItem is TextInEmbedded) {
        var textIn:ClassFactory = new ClassFactory(TextInEmbedded);
        if (vco != null) // assign properties
        textIn.properties = {maxLength:
StringValidatorConfigObject(vco).maxLength, minLength:
StringValidatorConfigObject(vco).minLength, required: vco.required,
vEnabled: vco.enabled};
// test to see if can edit and then display as label with ... to
truncate - seems to work
// this uses a different widget as the editor, but renderers it as a
label, to allow for truncation
        var labelIn:ClassFactory = new ClassFactory(Lbl);
        labelIn.properties = {truncateToFit: true, maxLength:
StringValidatorConfigObject(vco).maxLength, minLength:
StringValidatorConfigObject(vco).minLength, required: vco.required,
vEnabled: vco.enabled};                                                 
//TEST FOR VALIDATIO    newColumn.itemRenderer = labelIn;
//newColumn.itemRenderer = new ClassFactory(ValidatedCellRenderer);
newColumn.itemEditor = textIn;
newColumn.itemRenderer = textIn;                                                
newColumn.rendererIsEditor = false; // TEST FOR VALIDATION
        }else if (rendererItem is NumericInEmbedded) {
        var numIn:ClassFactory = new ClassFactory(NumericInEmbedded);
                if (vco != null) // assign properties
                numIn.properties = {maxValue:
NumberValidatorConfigObject(vco).maxValue, minValue:
NumberValidatorConfigObject(vco).minValue, required: vco.required,
vEnabled: vco.enabled};
// test to see if can edit and then display as label with ... to
truncate - seems to work
// this uses a different widget as the editor, but renderers it as a
label, to allow for truncation
        var nlabelIn:ClassFactory = new ClassFactory(Lbl);
        nlabelIn.properties = {truncateToFit: true};                            
        
newColumn.itemRenderer = numIn;
// TEST FOR VALIDATION  newColumn.itemRenderer = nlabelIn;
        newColumn.itemEditor = numIn;
        newColumn.rendererIsEditor = true;; // TEST FOR VALIDATION
        }
                                        
        }
        gridColumns.push(newColumn);
        dg.columns = gridColumns;
        }



/* extended DataGridColumn */

        public class DataGridColumnValidated extends DataGridColumn
        {
                private var _validatorConfigType:ValidatorConfigObject=null;
                private var _isRequired:Boolean = false;
                private var _hasValidator:Boolean = false;
                
                public function DataGridColumnValidated(columnName:String=null)
                {
                        super(columnName);
                }
                
                public var deepDataField:String;
                                
                
                public function get validatorConfigType():ValidatorConfigObject 
{
                        return _validatorConfigType;
                }
                
                public function set
validatorConfigType(value:ValidatorConfigObject):void {
                        _validatorConfigType = value;
                        _isRequired = value.required;
                }
                
                public function get isRequired():Boolean {
                        return _isRequired;
                }
                
                public function get hasValidator():Boolean {
                        return (_validatorConfigType != null)
                }
                
        }


/* itemRenderer - base class follows this section */

        public class TextInEmbedded extends TextIn
        {
                
        
//--------------------------------------------------------------------------
        //
        //  Constructor
    //
   
//--------------------------------------------------------------------------

    /**
     *  Constructor.
     */
                public function TextInEmbedded()
                {
                    super();    
                    this.setStyle("backgroundAlpha", 0);
                    this.setStyle("borderStyle","none");
                }

                override protected function setField(event:Event,
fieldValue:String):void {
                        this.text = fieldValue;
                        // for the embedded numeric type, you must use the data 
Object
                        this.data[event.currentTarget.listData.dataField] = 
fieldValue;
                }
                
        }
}


/* base class for Renderer */

        public class TextIn extends TextInput 
        {
        
                private var _val:StringValidator;
                private var _maxLength:Number;
                private var _minLength:Number;
                private var _required:Boolean = false;
                private var _requiredFieldError:String;
                private var _tooLongError:String;
                private var _tooShortError:String;
                private var _vEnabled:Boolean = true;
                private var _locked:Boolean = false;
        
        
//--------------------------------------------------------------------------
        //
        //  Constructor
    //
   
//--------------------------------------------------------------------------

    /**
     *  Constructor.
     */
                public function TextIn()
                {
                    super();    
                    
                   _val = new StringValidator();
                   _val.source = this;
                   _val.property = "text";
                   _maxLength = _val.maxLength;
                   _minLength = _val.minLength;
//                 _required = _val.required; // don't use default for
StringValidator - let app set
                   _requiredFieldError =_val.requiredFieldError;
                   _tooLongError =_val.tooLongError;
                   _tooShortError =_val.tooShortError;
                   _val.enabled = false;
//              _val.triggerEvent="change";
                        
                        if (!isNaN(_val.maxLength))                 
                            this.maxChars = _val.maxLength;

                   this.addEventListener(Event.CHANGE, textChanged);
                            
               
_val.addEventListener(mx.events.ValidationResultEvent.INVALID,
validationFailedHandler);
                _val.addEventListener(mx.events.ValidationResultEvent.VALID,
validationPassedHandler);
                // register with screen level class to notify this when
validation event is fired
//              
mx.core.Application.application.addEventListener(ValidateWidgetEvent.VALIDATE,
validateThis, false);
                }

                private function validateThis(event:ValidateWidgetEvent):void {
                        var failed:Boolean;
                        this.validateNow();
                        failed = performValidation();
                        if (failed)
                                dispatchEvent(new
ValidateWidgetEvent(ValidateWidgetEvent.VALIDATION_FAILED));
                        else
                                dispatchEvent(new
ValidateWidgetEvent(ValidateWidgetEvent.VALIDATION_PASSED));
                }
        

                private function textChanged(event:Event):void {
                        var fieldValue:String = 
assets.Util.removeLeadingSpaces(this.text);
                        setField(event, fieldValue);
                        this.validateNow();
                        performValidation();
                }
                
                protected function setField(event:Event, 
fieldValue:String):void {
                        this.text = fieldValue;
                }
                
                public function performValidation(value:String=null):Boolean {
                        if (value == null)
                                var fieldValue:String = this.text;
                        else
                                fieldValue = value;
                        
                        if (required) {
                                if (fieldValue.length < 1) {
                                        this.errorString = 
this.requiredFieldError;
                                        return(false);
                                }
                                else
                                        this.errorString = '';
                        }else if (!isNaN(_val.maxLength)) {
                                if (fieldValue.length > _val.maxLength) {
                                        this.errorString = this._tooLongError;
                                        return(false);
                                }
                                else
                                        this.errorString = '';
                        }else if (!isNaN(_val.minLength)) {
                                if (fieldValue.length < _val.minLength) {
                                        this.errorString = this._tooShortError;
                                        return(false);
                                }
                                else
                                        this.errorString = '';
                        }
                        
                        return true; // passed
                }
                
                protected function
validationFailedHandler(event:ValidationResultEvent):void {
                    this.setStyle("borderStyle","solid");
                        trace('validation Failed');                 
                        trace('Fail value:' + this.text);    
//                      dispatchEvent(new
ValidationResultEvent(mx.events.ValidationResultEvent.INVALID, true,
false));
                }               
                
                protected function
validationPassedHandler(event:ValidationResultEvent):void {
                    this.setStyle("borderStyle","none");
                        trace('validation Passed');                 
                        trace('Pass value:' + this.text);    
//                      dispatchEvent(new
ValidationResultEvent(mx.events.ValidationResultEvent.VALID, true,
false));
                }               
                
                
        
        override public function set data(value:Object):void
        {
                super.data = value;

                var dgListData:DataGridListData = listData as DataGridListData;
                var dataGrid:DataGrid = dgListData.owner as DataGrid;
        var column:DataGridColumnValidated =
dataGrid.columns[dgListData.columnIndex];
if (column.deepDataField != null) {
                var fields:Array = column.deepDataField.split('.');
                var lastField:String = fields.pop();
                var o:Object = value; // was data
                var n:int = fields.length;
                for (var i:int = 0; i < n; i++)
                        o = o[fields[i]];
                if ("value" in o)
                        listData.label = o[lastField].toString();
                else
                        listData.label = o.toString();
}
                
                this.text = listData.label;
                this.toolTip = listData.label;
                
                
//              var t1:String = value.businessRiskName.value;
//              var b1:Boolean = value.businessRiskName.passedValidation;

        }

        }





--- In [email protected], "Alex Harui" <[EMAIL PROTECTED]> wrote:
>
> Post the relevant code: The DG, the DGColumns, the Renderers.  A small
> test case if you can create one.
> 
>  
> 
> Do you have floating hit targets or is the mouse hitting something that
> isn't parented by the renderer?
> 
>  
> 
> ________________________________
> 
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of j_lentzz
> Sent: Wednesday, September 19, 2007 12:09 PM
> To: [email protected]
> Subject: [flexcoders] Re: DataGrid editor/renderer MouseDownHandler
> error
> 
>  
> 
> That is what I can't figure out. I don't know what I could do to make
> it null. I'm trying to use the ideas from your sub-object example, so
> I can pass in an object to the editor/renderer so it can then validate
> the value and set a pass/fail validation value - Ex: I 'm passing in
> an object to businessRiskName where value="high" and
> passValidation=true. So I'm trying to use the sub-objects
> businessRiskName.value and businessRiskName.passValidation. I've been
> able to get them to display properly, but when I click on the datagrid
> I get the error I've described. I'm wondering if I'm not getting the
> data from the grid/provider correctly, but don't know where to look.
> 
> Any guidance would be greatly appreciated.
> 
> Thanks,
> 
> John
> --- In [email protected] <mailto:flexcoders%40yahoogroups.com>
> , "Alex Harui" <aharui@> wrote:
> >
> > How did itemEditorInstance get to be null?
> > 
> > 
> > 
> > ________________________________
> > 
> > From: [email protected] <mailto:flexcoders%40yahoogroups.com>
> [mailto:[email protected] <mailto:flexcoders%40yahoogroups.com>
> ] On
> > Behalf Of j_lentzz
> > Sent: Wednesday, September 19, 2007 11:23 AM
> > To: [email protected] <mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] Re: DataGrid editor/renderer MouseDownHandler
> > error
> > 
> > 
> > 
> > For what it is worth, this is the error I get at runtime when I click
> > on the cell:
> > 
> > TypeError: Error #1009: Cannot access a property or method of a null
> > object reference.
> > at
> >
> mx.controls::DataGrid/mx.controls:DataGrid::mouseDownHandler()[E:\dev\fl
> > ex_201_borneo\sdk\frameworks\mx\controls\DataGrid.as:4239]
> > --- In [email protected]
> <mailto:flexcoders%40yahoogroups.com>
> <mailto:flexcoders%40yahoogroups.com>
> > , "j_lentzz" <jelentz@> wrote:
> > >
> > > I've been working on getting a datagrid to validate and I am close. 
> > > However, whenever I click on a cell in the datagrid, I get a runtime
> > > error in the DataGrid.as file, because my itemEditorInstance is
> null.
> > > However, I am providing an extended TextInput to the datagrid column
> > > the itemEditor and the itemRenderer. I've tried setting the
> > > rendererIsEditor to both true and false, but nothing changes. Has
> > > anyone seen this? Or can tell me what I'm doing wrong? I'm trying to
> > > follow examples, but obviously I'm doing something wrong.
> > > 
> > > Thanks,
> > > John
> > >
> >
>


Reply via email to