Adobe Flex: Error#1034 in trying to validate datagrid input - apache-flex

Trying to validate the input in my data grid, I am using a function (taken from an Adobe example).
This is how the grid goes:
<mx:DataGrid id="CashGrid" dataProvider="{cash}" editable="true" itemEditBeginning="allowForEdit(event)" itemEditEnd="formatData(event);" sortableColumns="false">
<mx:columns>
<mx:DataGridColumn textAlign="left" dataField="curName" headerText="Currency" />
<mx:DataGridColumn textAlign="right" dataField="value" headerText="Value" width="150">
</mx:columns>
</mx:DataGrid>
And here is the function from the AS part
public function formatData(event:DataGridEvent):void
{
if (event.reason == DataGridEventReason.CANCELLED)
{
// Do not update cell.
return;
}
var newData:String= TextInput(event.currentTarget.itemEditorInstance).text;
// Determine if the new value is an empty String.
if(newData == "")
{
event.preventDefault();
TextInput(cashGrid.itemEditorInstance).errorString=
"Enter a valid string.";
return;
}
}
Although it works in the source example, in my example, on editing said grid, an error pops up saying
TypeError: Error #1034: Type Coercion failed: cannot convert mx.controls::TextInput#f093c29 to spark.components.TextInput.
Trying to
import mx.controls.TextInput;
tells me
Can not resolve a multiname reference unambiguously. spark.components.TextInput.
I guess there is some confusion with the namespaces, but I have no idea how to make this work.
Help!
Thanks

I cannot reproduce the error with the given code. It's working fine on my end. Please send me the code for allowForEdit(event) as well.
Which SDK version are you using?
What is the root application namespace? Please paste the entire application namespace.

Related

How to pass column value to dataTipFunction in AdvancedDataGridColumn Flex

Flex Experts,
I am a newbie here. I am trying to create a conditional tool tip for my contact detail screen. If the contact has 'special-roles' like 'ABC, XYZ' then the tool tip should appear otherwise not. We have AdvancedDataGridColumn and using dataTipFunction to display the tool tip.
I am passing, values of roles & 'special-roles' in dataProvider for all contact objects.
I need to write tool-tip condition for every row(every contact). I am unable to write the condition in dataTipFunction as it has access to only 'item' object. I tried trace and Alert on 'item', but screen goes blank.
I have also tried "showDataTips" which accepts boolean but to no avail. I need a way to pass current row values (all columns) to a function from AdvancedDataGridColumn.
Here is the snapshot of my code. Any help is very much appreciated :)
<view:CopyPasteDataGrid id="contactsHolder"
width="100%"
height="100%"
displayDisclosureIcon="true"
variableRowHeight="true"
defaultLeafIcon="{null}"
folderClosedIcon="{null}"
folderOpenIcon="{null}"
useRollOver="true"
selectable="true"
styleName="PortfolioAdvanced"
designViewDataType="tree"
horizontalCenter="true"
verticalGridLines="false"
horizontalScrollPolicy="off"
dataProvider="{contactDetails}"
headerRelease="onHeaderRelease(event)">
<view:columns>
<mx:AdvancedDataGridColumn dataField="lastName"
sortCompareFunction="lastNameCompare"
headerText="{ApplicationConstants.LAST_NAME_HEADER}"
showDataTips="true" dataTipFunction="buildToolTipForContact"
width="100"/>
<view:rendererProviders>
<mx:AdvancedDataGridRendererProvider dataField="projectContacts"
renderer="com.ihg.hom.common.view.components.render.ContactsRenderer"
columnIndex="0"
columnSpan="0"/>
</view:rendererProviders>
</view:CopyPasteDataGrid>
private function buildToolTipForContact(item:Object):String
{
var myString:String="";
return "hello";
}
I have resolved it using different approach. I used label function. It has access to column object and based current object roles I am setting tool tip. Here is the code for reference.
<mx:AdvancedDataGridColumn dataField="role" sortable="{isRoleSortable}"
labelFunction="getRole"
headerText="Role" sortCompareFunction="sortRole" showDataTips="true" dataTipFunction="buildToolTipForContact"
width="120"/>
private var hasRole:Boolean=false;
private function getRole(item:Object):String
{
// extra code
if(currentRoles.indexOf(specialRole)!=-1)
{
hasRole=true;
}
}
private function buildToolTipForContact(item:Object):String
{
var myStr:String="";
if(hasRole){
myStr="Special Role";
}
return myStr;
}

Gettinf Property **text** not found error in DataGrid

I am using a custom itemEditor for my DataGrid.
The itemEditor has a simple TextField as a component.
But when ever i am clicking the cell i am getting an error as :
ReferenceError: Error #1069: Property text not found on editors.customItemEditor and there is no default value.
at mx.controls::DataGrid/itemEditorItemEditEndHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\controls\DataGrid.as:4827]
Plz help me to resolve this issue.
My Opinion is that the error is coming for the "text" field. But i havn't accessed "text" field or used it anywhere in my code.
Regards,
Ravi
i solved the problem with commiting incluting "return data["selected"].toString()" into get text:
<mx:DataGridColumn dataField="selected" rendererIsEditor="true" >
<mx:itemRenderer>
<fx:Component>
<mx:Box styleName="" width="100%" height="100%" backgroundAlpha="0.0"
horizontalAlign="center" verticalAlign="middle">
<fx:Script>
<![CDATA[
public function get text():String
{
return data["selected"].toString();
}
public function set text(value:String):void
{
}
protected function checkbox1_clickHandler(event:MouseEvent):void
{
data["selected"]=event.target["selected"];
}
]]>
</fx:Script>
<mx:CheckBox selected="{data.selected}" click="checkbox1_clickHandler(event)"/>
</mx:Box>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
Taken from http://livedocs.adobe.com/flex/3/html/help.html?content=celleditor_8.html
By default, Flex expects an item editor to return a single value to the list-based control. You use the editorDataField property of the list-based control to specify the property of the item editor that contains the new data. Flex converts the value to the appropriate data type for the cell.
The default item editor is a TextInput control. Therefore, the default value of the editorDataField property is "text", which corresponds to the text property of the TextInput control. If you specify a custom item editor, you also set the editorDataField property to the appropriate property of the item editor
I am still Struck in this error but i think there is a hope of coming out of it.... :)
If we take TextInput as the itemEditor, like:
dataGridColumn.itemEditor = new ClassFactory(TextInput);
Then there was no problem as "text" is being internally defined inside TextInput.as.
On a similar note if i copy set text and get text inside our custom Editor, then its working fine.. only problem that is
coming is regarding commiting the changed values.
I am working on it, hope i'll be able to sort it out.
P.S.: I am a newbie here, so plz pardon me of any idiotic RnD... :p

Flex Combobox and "Access of undefined property ..." error

I am kind of puzzled and not sure how to tackle this issue: Got two comboxes with dataproviders. Also, I created a separate index.as in /as folder. I have these two functions:
In my .mxml page, I have :
<mx:FormItem label="Property Code:" id="fi_propertyCode" width="100%">
<mx:ComboBox id="propertyCode" width="50%" dataProvider="{qry_communites}" change="showPropertyCode()" />
</mx:FormItem>
<mx:FormItem label="Utility Type:" id="fi_utilityType" width="100%">
<mx:ComboBox id="utilityType" width="50%" dataProvider="{qry_utilityTypes}" change="showUtilityType()" />
</mx:FormItem>
and in my index.as, I have:
private function showPropertyCode():void {
mx.controls.Alert.show(propertyCode.selectedItem.label, propertyCode.selectedItem.data);
}
private function showUtilityType():void {
mx.controls.Alert.show(utilityType.selectedItem.label, utilityType.selectedItem.data);
}
I get an error saying
Access of undefined property utilityType.
The funny thing is I don't get this error for "propertyCode". Moreover, If I copy pasyte showUtilityType() function and put inside my .mxml page, I don't get this error either. Btw. I have :
....
in my .mxml page. What should I do? How can I get rid of this error? Why does Flex see propertyCode combobox but not utilityType combobox? I would rather keep index.as as it is. Thanks in advance.
That's strange. Did you try specifying the script using the source attribute?
<mx:Script source="../as/index.as"/>

How do I dynamically populate the options in a combobox inside of a grid based on another row in Flex?

I'm trying to setup a DataGrid that contains a column of combo boxes. The values of the combo boxes are defined by data specific to that row. I cannot get this to work though, I'm asking for a solution to this, either fixing what I have below or a recommendation on a different way.
One of the columns of my DataGrid has an object derived from a ComboBox for an ItemEditor. The itemEditor is set like this:
<mx:DataGridColumn editorDataField="selectedItem" dataField="type" editable="true" >
<mx:itemEditor>
<mx:Component>
<FilterCell:SelectCellBase initialize="set_combo()" grid="{this}"/>
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>
So that when the itemEditor is generated (when the user double clicks on the cell) the data is populated.
Then, the SelectCellBase set_combo() function is defined as such:
public function set_combo( ) : void
{
var type : String = grid.dataProvider[grid.selectedIndex]['type'];
if( 'text' == type )
{
this.dataProvider = text;
}
else
{
this.dataProvider = number;
}
}
This implementation isn't working though because when I try to call grid.selectedIndex this always seems to return -1.
What am I doing wrong, or what better way is there to do this?
Your problem is that when you're inside of an <mx:Component> the scope is local, and no longer set to the outside MXML file. So when you have:
<mx:Component>
<FilterCell:SelectCellBase initialize="set_combo()" grid="{this}"/>
</mx:Component>
the "this" you're referring to is the inline component you've defined, not the base MXML component you're working on. The easy fix is to change it to
<mx:Component>
<FilterCell:SelectCellBase initialize="set_combo()" grid="{outerDocument}"/>
</mx:Component>
the outerDocument variable is automatically set when you're inside of an <mx:Component> tag, and can be used to access anything needed from the parent scope.

Flex CheckBox Question

<mx:DataGrid x="10" y="10" width="180" height="302" id="dgActions" dataProvider="{actionCollection}">
<mx:columns>
<mx:DataGridColumn headerText="Action" dataField="name"/>
<mx:DataGridColumn headerText="" dataField="setting" width="30" rendererIsEditor="true">
<mx:itemRenderer >
<mx:Component>
<mx:Box width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:CheckBox selected="{data.setting}" click="setActionSetting()">
<mx:Script>
<![CDATA[
private function setActionSetting(){
data.setting = String(this.selected);
}
]]>
</mx:Script>
</mx:CheckBox>
</mx:Box>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
For some reason I'm getting an error at the data.setting= String(this.selected) line which says "Access to possibly indefined property selected through a reference with static type".
[edit] The solution to the above problem (albeit not the entire mess) was that once you're inside a <mx:Component> tag you are within the scope of said component. To get access to the script and nodes outside this component you have to use the outerDocument object.
[end edit]
I'm not sure what it's expecting, I'm assuming that it's going to pass the true/false value of the selected(ness) of the checkbox into the method, but it appears not to understand what "this" is, in this context.
Am I doing something obviously wrong? All I want is for the data source to reflect the change in the status that it initially fed into the checkbox.
EDIT:
I just noticed that when I add trace('foo') to the function, it never writes anything back to the console. Is the checkbox's native behavior (and event capture) preventing it from bubbling past to my function?
Additionally, when I add references to objects outside in the rest of the document, it tells me it doesn't recognize them. I'm totally confused as to how Flex scopes things... any additional guidance or links to reference would be really handy.
this in this (ha) case is referring to the component renderer and not the surrounding class (or the checkbox, datagridcolumn, datagrid, etc). You are really better off breaking the renderer out into a real component so you won't be obfuscating the scope as much as when the inline component approach is used.
Peter Ent's series on itemRenderers is extremely useful and should explain everything you want to know on the subject.
If I had to guess "this" is the mx:Script element, try "parent.selected".
CheckBox.selected requires a Boolean value. The fact that you're setting data.setting to a String value tells me that data.setting is NOT a Boolean.
So, after a great deal of agony I have finally figured out how this all works....
Joel is on the right track, this doesn't refer to what you would hope it would refer to (namely the checkbox). Additionally, even if you pass this into the method FROM the checkbox node, it refers to the parent wrapper class and not the checkbox itself. So, the solution is to pass in the event, and then access its target, which FINALLY is the checkbox. And then you're home.
In other words...
<mx:CheckBox selected="{data.setting}" click="setActionSetting(event)">
<mx:Script>
<![CDATA[
private function setActionSetting(e:Event):void{
data.setting = e.target.selected;
trace("n=" + data.name + " set to " + data.setting);
//the name is the other piece of the data that I omitted for clarity
}
]]>
</mx:Script>
</mx:CheckBox>

Resources