View Full Version : V2 ComboBox: Default value based on selectedItem not selectedIndex
JoeyClams
03-16-2005, 10:48 PM
Ok I can't find any doco on this and it's driving me crazy. Anyone know how you specify the default value for the V2 ComboBox component based on the selectedItem when you don't know the selectedIndex? Here's my scenario:
I have a hardcoded combobox called "dd_names" with 50 names in it. I'm returning a dataset into Flash via a SQL query, and onload I need the dd_names dropdown to default to the value of the name returned in the dataset. So if the query returns "Tom" I need the dropdown to default to "Tom."
I tried setting dd_names.selectedItem = "Tom" and dd_names.value = "Tom" and these don't work. The ComboBox just defaults to the first item in the list.
However, if I use the selectedIndex property and set dd_names.selectedIndex = 3, the ComboxBox doesn't default to the first item in the list but shows the appropriate item. This doesn't help because I don't know the selectedIndex being returned from the query - I only know the selectedItem. Anyone know how to do this???
thxInAdv
FlashBulb
06-12-2006, 07:46 PM
Here's what I did:
function findIndex(whichArray:Array, whichItem:String)
{
var ret_value = "value not found";
for(var i in whichArray)
{
if(whichArray[i] == whichItem)
{
ret_value = i;
}
}
return ret_value;
};
my_cb.selectedIndex = findIndex(my_cb.data, "Tom");
I realize you posted over a year ago, but I was just having the same problem. :rolleyes: Hopefully this will help someone else!
Shaka
06-18-2006, 11:48 PM
It helped for me almost 3 years after
Thanks from Argentina
dogstar
04-12-2007, 12:05 AM
Helped me now :) Thanks from UK
YosiDov
10-30-2007, 11:31 AM
This works well when the options of the Combobox are inserted in the component parameters window (Data and Label), but I have an #include with a external text file that have lines and lines of "addItem", in this case the "my_cb.data" is undefined, do someone can help me with this?
Thanks from Jerusalem!
FlashBulb
10-30-2007, 02:04 PM
You can use the my_cb.labels array instead of the my_cb.data one.
Array.prototype.findIndex = function(whichItem:String):Number
{
var ret_value = "value not found";
for(var i in this)
{
if(this[i] == whichItem)
{
ret_value = i;
}
}
return ret_value;
};
trace("Index: " + my_cb.labels.findIndex("Tom"));
I also made it a prototype, I like this way better.
YosiDov
10-30-2007, 02:14 PM
Index: undefined :eek:
FlashBulb
10-30-2007, 02:16 PM
Hmm, you're loading in the labels from a text file - maybe they're not loaded yet? Is your ComboBox given the instance name my_cb?
YosiDov
10-30-2007, 02:32 PM
1) I load the variables from an external as file, the variables are loaded with an #include line, I just try making a delay to see if maybe variables are not loading yet, nothing change.
2) My Combobox name is "product", I change the name in the relevant parts in your script
Array.prototype.findIndex = function(whichItem:String):Number
{
var ret_value = "value not found";
for(var i in this)
{
if(this[i] == whichItem)
{
ret_value = i;
trace(i)
}
}
return ret_value;
};
trace("Index: " + product.labels.findIndex(MiValor));
function EscribeEsto(){
MiValor = MiValor + chr(Key.getCode());
product.selectedIndex = findIndex(product.label, MiValor);
product.open();
product.text = MiValor;
}
product.addEventListener("keyDown",EscribeEsto);
I'm trying the following: when the Combobox (product) is in focus and the client types in his keyboard I want the text from the Combobox (MiValor) to show the typed text and my options scrolls automatic until the option that is the most similar (by A-B-C) to the typed text.
FlashBulb
10-30-2007, 03:47 PM
You only need one argument in the new 'prototype' way - the Array is known already, because you call it as a function of that Array.
product.selectedIndex = product.labels.findIndex(MiValor);
EDIT: I just saw the bottom part of your post - this way doesn't search for the closest match based on the starting letters.
Are you still getting 'Index: undefined' traced?
YosiDov
10-30-2007, 04:39 PM
1) Do you know how to do that, to receive the closest match? :confused:
2) Yes, still the Index: undefined
Thanks ;)
EDIT: in your first script: the value of whichArray inside the function is undefined, is something wrong with my_cb.data, I tryed using instead dataProvider and I received [object Object],[object Object],[object Object],[object Object],[object Object], it is a way to read the data or label value from this?
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.