Using strings to access custom methods dynamically - apache-flex

I am creating a two-dimensional list that has consecutive numbers at the end of "day", for use as dataProvider for a DataGrid
i have been accessing them via the command
dg1.selectedItem.day1
dg1.selectedItem.day2
dg1.selectedItem.day3
etc...
is there any way to take the string ("day"+i) and convert it into a (what is it? variable name?)
so that i can do something along the lines of:
for(var i:Number=1; i<numFields; i++)
{
dg1.selectedIndex = i-1;
dg1.selectedItem.(mysteryFunction("day"+i)) = 42;
}
if there's a function that i could use for mysteryFunction, or what data type to use, any info would be very helpful
this is what i've been using (so tedious):
<mx:XMLList id="sched">
<field>
<day1></day1>
<day2></day2>
<day3></day3>
</field>
<field>
<day1></day1>
<day2></day2>
<day3></day3>
</field>
...
</mx:XMLList>

The "mystery function" you are looking for is as simple as indexing with brackets:
for(var i:Number=1; i<numFields; i++)
{
dg1.selectedIndex = i-1;
dg1.selectedItem["day"+i] = 42;
}
And it is called, surprisingly, an attribute.

Use an Array or if you are going to bind it (which I am kind of betting on) use ArrayCollection instead of naming these variables individually.
If the members are generated by some program, you better put all of these in one of the collection classes I mentioned above and then start the processing. It makes life easier in the long run.
E4X is the way to go when dealing with XML. The Mozilla guys have an arguably better explanation of that technology. So, if your XML is stored in a variable as:
var tree:XML = <field>
<day1></day1>
<day2></day2>
<day3></day3>
You can simply do:
tree.day1 = 42;
Why would you want this mysteryFunction()? A dataProvider object is just a collection of some Type. You know the type already, right? Read this.
Anyway, there is no such mystery function. Note, however, string concatenation with a number converts the number to a string. Try
trace("str " + 42);

Related

Changing values to combobox from an array

I need to load values from an array to a combobox, with this code it loads only the last value of the array? Can anyone help me please.
for(int i =0; i<lines.size(); i++) {
resultArray[i] = lines.get(i).split("\t");
Laptops[i] = resultArray[i][0];
ObservableList<String> option = FXCollections.observableArrayList(Laptops[i].toString());
cbx1.setValue("");
cbx1.setItems(option);
cbx2.setValue("");
cbx2.setItems(option);
cbx3.setValue("");
cbx3.setItems(option);
}
In your loop, you are creating a brand new List on each iteration. So when you call setItems() on your ComboBox, the option list only has that one item in it.
There are several other issues with your loop, but once you have a valid array, populating a ComboBox with it is quite simple using the Arrays.asList() method:
ObservableList<String> option = FXCollections.observableList(Arrays.asList(resultArray));
cbx1.setItems(option);
That being said, I doubt you're getting a proper array with your line:
resultArray[i] = lines.get(i).split("\t");
Without knowing what lines actually is in your code, it's difficult to address this specifically, but it looks like you could skip the whole resultArray altogether and just use lines.
Calling the split() method returns an array of Strings anyway. Perhaps you could remove the loop altogether and just try:
ObservableList<String> options =
FXCollections.observableArrayList(Arrays.asList(lines.split("\t"));

How to remove double qoutes in Objective-C

Let me introduce myself.
My name is Vladimir, C++ programmer, I am from Serbia. two weeks ago I have started to learn objective-C and it was fine until tonight.
Problem:
I cant remove double quotes from my NSLog output.
NSLog(#"The best singers:%#", list.best);
Strings are joined with componentsJoinedByString:#" and "
I would like to get something like this:
The best singers: Mickey and John.
But I get this:
The best singers: ("Mickey", and "John").
I cant remove comma (,) and parentheses either.
I have tried with "replaceOccurencesOfString" but with no success. It can remove any character except qoute and comma.
Also I have used -(NSString *)description method to return string.
You are getting the raw output from your list (which I assume is an array). You will have to do your own formatting to get this to display in the format that you want. You can achieve this by building your string by iterating through your array. Note that this probably isn't the most efficient nor the most robust way to achieve this.
NSMutableString *finalString = [NSMutableString string];
BOOL first = YES;
for (NSString *nameString in list) {
if (first) {
[finalString appendString:nameString];
first = NO;
} else {
[finalString appendString:[NSString stringWithFormat:#" and %#", nameString]];
}
}

Flex - sorting a datagrid column by the row's label

I'm creating a table that displays information from a MySQL database, I'm using foreignkeys all over the place to cross-reference data.
Basically I have a datagrid with a column named 'system.' The system is an int that represents the id of an object in another table. I've used lableFunction to cross-reference the two and rename the column. But now sorting doesn't work, I understand that you have to create a custom sorting function. I have tried cross-referencing the two tables again, but that takes ~30sec to sort 1200 rows. Now I'm just clueless as to what I should try next.
Is there any way to access the columns field label inside the sort function?
public function order(a:Object,b:Object):int
{
var v1:String = a.sys;
var v2:String = b.sys;
if ( v1 < v2 ){
trace(-1);
return -1;
}else if ( v1 > v2 ){
trace(1);
return 1;
}else {
trace(0);
return 0;
}
}
One way to handle this is to go through the objects you received and add the label as a property on each of them based on the cross-referenced id. Then you can specify your label property to display in your data grid column instead of using a label function. That way you would get sorting as you'd expect rather than having to create your own sort function.
The way that DataGrids, and other list based classes work is by using itemRenderers. Renderers are only created for the data that is shown on screen. In most cases there is a lot more data in your dataProvider than what is seen on screen.
Trying to sort your data based on something displayed by the dataGrid will most likely not give you the results you want.
But, there is no reason you can't call the same label function on your data objects in the sortFunction.
One way is to use the itemToLabel function of the dataGrid:
var v1:String = dataGrid.itemToLabel(a);
var v2:String = dataGrid.itemToLabel(b);
A second way is to just call the labelFunction explicitly:
var v1:String = labelFunction(a);
var v2:String = = labelFunction(b);
In my experience I have found sorting to be extremely quick, however you're recordset is slightly larger than what I usually load in memory at a single time.

ArrayCollection reverses order of my objects

I'll show you the function first.
private var areaCollection:ArrayCollection;
private function generateAreaCollection():void
{
areaCollection = new ArrayCollection();
areaCollection.addItem({Areal: "Totalareal:", Verdi: int(totalArea * 100) / 100 + " kvm"});
areaCollection.addItem({Areal: "Hovedtakets areal:", Verdi: int(result.area* 100) / 100 + " kvm"});
//Lots of other stuff omitted (just more addItems).
}
As you see, the order i put the items in the ArrayCollection is Areal, then Verdi (area, value)
When i loop through the collection later, the order changes. Now it is Verdi then Areal (value, area).
Does anyone have an idea of what might be the problem?
It really ruins things when I pass it over to a PHP-script for table-making in html later.
(I have several different dynamic DataGrids that differs in size and "values", so I can't really point directly to f.ex "Areal" in my PHP-script)
And by the way, does anyone know how i can remove that pesky mx_internal_uid?
Raw objects in AS3 (Object class, or things defined by {} ), have no sorting. That is, the order is unpredictable. You're confusing two ideas here I think. The Areal and Verdi strings are keys within an object map. The array collection is a list composed of two such objects. So any sorting applied to the array collection will sort those objects, but not within the object.
So you need to refer to areaCollection.source[0].Areal to get "Totalareal". And areaCollection.source[1].Verdi to get int(result.area* 100) / 100 + " kvm"
If you do for(var s:String in areaCollection.source[0]) { } you will iterate twice, with the value of "s" being "Areal" and "Verdi" or vice-versa (e.g, order not guaranteed).
Make sense? If you need order, you can make the object an array instead ["Totalareal", (int(totalArea * 100) / 100 + " kvm")], and then access "Verdi" using [1].
P.s. not sure how to remove the mx_internal_id.

How can I tell if E4X expression has a match or not?

I am trying to access an XMLList item and convert it to am XML object.
I am using this expression:
masonicXML.item.(#style_number == styleNum)
For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:
var defaultItem:XMLList = DataModel.instance.masonicXML.item.(#style_number == styleNum);
if(defaultItem.text())
{
DataModel.instance.selectedItem = XML(defaultItem);
}
But it still give me an error if theres no match. It works fine if there is a match.
THANKS!
In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.
Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.
var defaultItem:XML =
DataModel.instance.masonicXML.item.(#style_number == styleNum)[0];
if( defaultItem != null )
{
DataModel.instance.selectedItem = defaultItem;
}
OK I got it to work with this:
if(String(defaultItem.#style_number).length)
Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)
You can also check for the length of the XMLList without casting it to a String:
if (defaultItem.#style_number.length() > 0)
The difference to String and Array is that with an XMLList, length() is a method instead of a property.

Resources