Get Object using id in flex? - apache-flex

Acctually
I want to remove Child from VBox , i have id of child , but i don't to have real object that i want to remove using removeChild function of VBox

var elem:Type_of_E = this["constructed_id_of_E"];

If you have the id of the child to be removed, you have the real object. id attribute in mxml creates a public variable by it's value and store a reference to the object in that variable.
if(childId != null)
vbox.removeChild(childId);
else
trace("Normally this shouldn't happen in flex");
//or if you don't have VBox's id but you are sure that
//the child is in fact is parented by a container:
childId.parent.removeChild(childId);

Assuming you know the name of your VBOX before runtime:
yourVBOX.removeChild( yourVBOX.getChildByName('yourChildID') );
Read more on:
LiveDocs - Container - getChildByName

You might also want to set the "name" property on your component, such as :
myLabel.name = "LabelX";
myLabel.id = "LabelX"; // eventually
Then proceed to doing as the first answer said,
yourVBOX.removeChild( yourVBOX.getChildByName('LabelX') );
The thing to remember is to set the name as well as the ID, there is no such method as "getChildByID" :-)

getChildByID:
this.getChildren()[id]

Related

how to set datagrid numberbox prefix onchange combobox

I am using datagrid with inline editor which have validatebox, numberbox, and combobox. What I am try to do is when I select the combobox, I need to set numberbox prefix as currency symbol as I want. If I want to set the value to the numberbox, usually write like this:
var ed8 = $("#dg").datagrid("getEditor", {index:idx, field:'cost'});
$(ed8.target).numberbox('setValue',value);
but I am stack on case how to change/set the property "prefix" of numberbox.
One more thing that I want to ask, Is that possible to set multiple prefix in one field (one row one prefix) of datagrid?
I just found the solution by put the code like this.
var ed8 = $("#dg").datagrid("getEditor", {index:idx, field:'cost'});
var opts = $(ed8.target).numberbox('options');
opts.prefix = '$';
It is possible to set multiple prefix on a field
hope this can help others.

App Engine emulated datastore - strange observation

I was experimenting with 'datastore' on my machine. Using this code.
Key parentKey = KeyFactory.createKey("parent", "parentId");
try {
// Entity parent = new Entity("parent", "parentId");
// parent.setUnindexedProperty("property1", "value1");
// ds.put(parent);
Entity savedParent = ds.get(parentKey);
// savedParent.setUnindexedProperty("property1", "value3");
// ds.put(savedParent);
// Entity child = new Entity("child", "childId", savedParent.getKey());
Entity child = ds.get(KeyFactory.createKey(savedParent.getKey(), "child", "childId"));
child.setUnindexedProperty("property1", "val2");
ds.put(child);
// logger.info("Saved child.");
} catch (EntityNotFoundException e) {
throw new RuntimeException(e);
}
First I saved parent entity and set property on it to "value2" then I added a child entity with property val1. Then I updated parent property to value3. Then I updated property on child to val2.
Then I found out in the admin console, that value of the property on the parent is back to value2. I repeated it again with the same result. Am I missing something? Or is this some kind of bug?
I suppose, this is manifestation of eventual consistency, right? I run each step in a new JVM instance, thinking that values must have been stored at the time I switched it off:-. Contradicting common sense, but correct in terms of emulation of eventual consistency....

Converting a string into a CheckBox

I have a string which is ultimately the id of a CheckBox.
What I need to be able to do is to access the CheckBox's properties from the string
var myCheckBox:Object;
var myString:String;
myString = "checkbox_1"
myCheckBox = Object(myString); ?!?!
... and then I'd need to get to myCheckBox.selected, and myCheckBox.label etc
easier answer:
if(this.hasOwnProperty(myString) && this[myString] is CheckBox) {
myCheckBox = this[myString] as CheckBox
}
It's a bit of overcoding (since the as keyword will return a null if it's not a checkbox and you could better handle it that way with potentially less code), but that should do ya.
Best of luck.
If you know what DisplayObjectContainer (e.g. Sprite, MovieClip) the CheckBox is inside you can use getChildByName.
Unfortunately if you are using Flex containers (like Group) there is no function getElementByName(). There is getElementAt so you could write a loop that iterates over all of a Groups elements until it encounters one that matches the name you have.

Flex DataGridColumn id missing?

I am creating a Datagrid dynamically in AS3. In MXML it is valid to write:
mx:DataGridColumn dataField="abc" id="testColumn"
in AS3 however, I can not use the property "id", it is not found. Any ideas?
var dgc : DataGridColumn = new DataGridColumn();
dgc.id = "testColumn"; (NOT WORKING)
Thanks,
martin
The id is not a real property, but only a way to define the variable name in mxml. Therefore in your case the ID is "dgc".
What are you trying to achieve, by the way?

How do I programmatically associate a RadioButton with a RadioButtonGroup in ActionScript3?

I have a UI component that, for various reasons, I have to construct programatically. The component is a table of radio buttons grouped by column.
Right now, I'm constructing the column groups like so:
private function createGroupsForItemList(items: XMLList): void {
for each (var item: XML in items) {
var rbGroup: RadioButtonGroup = new RadioButtonGroup();
groups[item.#level.toString()] = rbGroup;
}
}
I'm trying to associate the RadioButton instances with the column groups like so:
private function createValueControl(item: XML): UIComponent {
var control: RadioButton = new RadioButton();
control.label = "";
control.group = groups[item.#level.toString()];
control.addEventListener(Event.SELECT, updateSelection);
return control;
}
I can see in the debugger that the control has an association to the group:
control.group == groups[item.#level.toString()]
However, I can see equally that the group does not know anything about the control:
group.radioButtons.length == 0
I imagine that this is because the setter for group in RadioButton is a dumb setter; all it does is copy to the variable, which doesn't do the magic that groupName does. However, I can't seem to find the value I should use to set the RadioButton.groupName property correctly.
So, in short, I'm stumped on how to get these bits to talk to each other. How do I do this?
-- EDIT --
It turns out that I can have the groups created and associated simply by setting the groupName property, but I can't get at the group to set up a selection listener; the group is NULL immediately after the setting process, which means that the second line below throws the Flex equivalent of an NPE:
control.groupName = groupNameForLevel(item);
control.group.addEventListener(Event.SELECT, updateSelection);
First instinct is that this issue has to do with invalidateDisplayList and when and how that is called. Of course, since issues related to that function are behind a number of Flex's quirks, I may just be scapegoating.
This is not the answer to your question per se, but it seems like it might actually work as an alternate solution.
RadioButtonGroups will initialize based on a IFlexDisplayObject. This means that you can do something like:
var c:HBox = new HBox();
var rbg:RadioButtonGroup = new RadioButtonGroup( c );
// do stuff with rbg.
c.addChild( new RadioButton() );
The problem is that it may not be the most practical answer, but it has the decided benefit of being a workable solution.
Setting groupName should work.
All I can suggest is to step through the group() getter of the RadioButton component and see where exactly it is failing. Are you programmatically creating the group too? If that's the case, maybe it isn't initialized fully yet.

Resources