flex: drilling down into dataprovider arrays - apache-flex

I have an SQL Statement which I want to read the name fields from an SQL database. the code:
public function getAllGiberish():void {
var stmt:SQLStatement = new SQLStatement();
stmt.sqlConnection = sqlConnection;
stmt.text = "SELECT name FROM test3";
stmt.execute();
l.dataProvider = new ArrayCollection(stmt.getResult().data);
}
This will pull the data from the db. However, in the list item it shows everything up as [object Object]. Upon debugging I can see that the data provider shows:
data[0] > name
data[1] > name
data[2] > name
Where the info that I want is the name within each data object.
How do I easily access this? It is a problem I keep coming across and would like to work it out once and for all!
Cheers

You want to set the labelField property on the list (Assuming Flex 3 here). By default it's looking for a field called "label" not "name". Also look at the dataField and labelFunction properties of the list object for some more advanced options.

The result of the query returns an array of Object instances, while you might expect String, with their keys equal to the column names that you select. You will either need to alias the "name" column to "label" in your query (as the List control uses this as the default labelField as Chris metioned), or you need to set the labelField or a labelFunction on the List control.
Note that you can also return typed objects instead of plain Object instances by setting the itemClass property on the statement.

You could write a label function:
private function list_labelFunction(item:Object):String {
return item.columnName;
}
And make sure that your List has a label function set:
<s:List id="myList" labelFunction="list_labelFunction"/>
That's how I did it anyway!

Related

xpages session scope AND document data binding

I have a value that I get from a picklist. I set this value as a sessionScope variable.
I then want to use this value, do a lookup, and set the value of an input field - which is working.
However, I am doing the lookup code in the fields data binding section using SSJS, and as such am not too sure how to save this value (normally my data binding would just be document1.FIELDNAME)
I've tried setting the value as part of my code, but the change is not saved in the backend doc.
I've also tried doing the lookup code in the fields "Default value" property, but this always just returns nothing.
Does anyone know how I can display on the xpage the value from my lookup AND also save this value to the backend document?
I fear I am missing something simple and maybe getting tunnel vision!
The code I am using for my data binding value is below.
Thanks
try{
var key1 = sessionScope.PLProspectiveAssured;
var dbName:NotesDatabase = session.getDatabase(database.getServer(),"CIR2001.nsf");
if (key1==""){
returnVal = "Not found";
}else {
var vwOrgs:NotesView = dbName.getView("OrgDocID");
var doc:NotesDocument = vwOrgs.getDocumentByKey(key1);
returnVal = doc.getItemValueString("OrgCountry");
}
// set our field
var doc:NotesDocument = document1.getDocument();
doc.replaceItemValue("ProspectiveAssured", returnVal);
return returnVal;
}catch(e){
openLogBean.addError(e,this);
}
Use your datasource and set the value using .setValue(field, value). In your case:
// set our field
document1.setValue("ProspectiveAssured", returnVal);
Make sure that you save your datasource somewhere (else).

Cannot implicitly convert type 'string' to 'System.Collections.Generic.ICollection<WebApplication2.Entry>'

I used ado.net entity framework to connect database and have an .edmx file in project.. When I tried to reach objects in code side with object initializer I can see the object names but when I tried to enter a value into textarea in throws this error.Title is a table in database and entries is another tables data but because of both tables has relationship I can see Entries down of Title. What do I have to do? I do not understand anything.. thanks for helps here is the situation
Title a = new Title
{
Entries=textarea.InnerText,
};
Try below, you need to inititialize entry collection with your item by giving correct property value
Title a = new Title
{
Entries= new List<Entry>()
{
new Entry() {PropertyName =textarea.InnerText}
};
};
It's because your Entrires are of type ICollection<Entry> and you trying to store there string variable.

Get attribute from DropdownList SelectedItem

I have a dropdownlist where I need to store more data than the standard list item allows. The approach I've taken is to add an attribute to each of the listitems.
I monitor for changes and can return the SelectedIndex, but I'm not sure how to get the attribute back from there, or whether there are any easier ways of achieving this.
Any ideas?
Try this:
ddl.SelectedItem.Attributes["key"];
I did try this once before and i figured i could not really use the attribute's on the DropDownList attributes.
What i did was the following:
Create a list containing a KeyValuePair. The Key in the KeyValuePair is the same ID as you put in your DropDownList Item.
The value of the KeyValuePair, is the value (or values) that you would like to keep/connect with your item.
You can store the List in your viewState and read the data once you have selected an item in your DropDownList and find the right KeyValuePair using the ID.
So you can "store" the data like this:
var listKeyValuePair = new List<KeyValuePair<int, string>>();
listKeyValuePair.Add(new KeyValuePair<int, string>(1, "data"));
ViewState["DataList"] = listKeyValuePair;
And you can get your data like this:
var listKeyValuePair = (List<KeyValuePair<int, string>>)ViewState["DataList"];
var dataILikeToHave = listKeyValuePair.Find(k => k.Key == Convert.ToInt16(dropDownlist.SelectedItem.Value));

datagrid cannot be found

I create each datagrid to be added to the NavigatorConent(), however, how do I retrieve the datagrid by ID so that I can point the ArrayCollection to datagrid's dataprovider?
private var pdg:String;
private function stabAdd():void {
var dg1:DataGrid = new DataGrid();
var cn:NavigatorContent = new NavigatorContent();
stab.addElement(cn);
cn.name = "nc"+nu;
dg1.id = "nc"+nu;
pdg = dg1.id;
dg1.addEventListener(MouseEvent.CLICK,cc);
nu++;
This will throw an error which pdg cannot be found, I wonder why:
trace(DataGrid(pdg));
The purpose of nu++ is to assign a
unique name (dg1, dg2, etc) to each
datagrid so that I can assign AC to
that datagrid's dataprovider
I can respect the need to give every component a unique name. The appropriate way to do that in ActionScript is not to specify the id/name field of the component, but rather to create an instance of the component as a variable. Something like this:
protected var myGrid : DataGrid;
And you can now access myGrid anywhere in the component, or in it's children, without creating some complicated scheme. If you need multiple DataGrid's you can store them in an array:
protected var myGridArray : Array = new Array();
And somewhere later in your code--probably createChildren() do something like this:
loop
var newGrid : DataGrid = new DataGrid()
myGridArray.push(newGrid);
end loop
For the most part, this is how all the Flex list based components do it with itemRenderers. They have an array of visible renderers.
As stated in #J_A_X_ comments, you are trying to convert pdg--a string--into a DataGrid. I would expect that to return a null value, as Flex casts tend to fail quietly.
If you want more help, you'll have to tell us the explicit error that you're receiving, possibly with line numbers and more code.

dynamically generate ComboBox name

I have a script that parses some complex XML. When the XML element is of a certain type, it generates a comboBox using the XML element's children to populate the box. I then want to check all of the values of the all the generated ComboBoxes against their correct answers (which is also info stored in the XML file). When creating the ComboBoxes, I added an "id" property. However, it seems that I cannot them use:
dynamicQuestion.id.selectedItem.labelField
to check the answers. However, I am able to get the labelField if I know the variable name used to create the ComboBox.
dynamicQuestion.selectedItem.labelField
This indicates (to me) that I need to dynamically generate the variable name as I'm creating new instances of the ComboBox. But how do I dynamically generate a variable name? If I use
var thisBox:String = "box"+boxCount;
var newBox:ComboBox = thisBox as ComboBox;
I get an implicit coercion error. I also tried changing the creation statement to a function that accepted an argument, "thisBox," but this didn't work either. Conceptually, this seems quite simple, but I'm having a hard time putting it to practice. It seems that the comboBox's id is what is generated by created the box using script (e.g., var thisBox). How do I dynamically generate this name?
Use an array as Stefan suggested. If you must use string identifiers, you can create an object and use it as an associative array.
var combos:Object = {};
var boxCount:Number = 1;
var thisBox:String = "box"+boxCount;
//you can store comboboxes in the object using the following syntax
combos[thisBox] = new ComboBox();
//or
combos.box2 = new ComboBox();
//or
combos["box3"] = new ComboBox();
trace(combos.box1.selectedItem.labelField);
trace(combos.box2.selectedItem.labelField);
trace(combos.box3.selectedItem.labelField);
Why don't you store all your dynamically created combo boxes in an array? When you want to evaluate them you iterate over the array and access selectedItem.labelField.

Resources