Use an array of objects to process records in ActionScript
If you stop and think about this, you can use an array of Structure Variables to include data, similar to the ColdFusion query (record set) variable. If I have data in the following form:
Row number |
Name |
Name |
Management Status |
1 |
Fred |
123 |
False |
2 |
Ginger |
234 |
True |
Using the cfquery tag, I can retrieve data from such a table:
<cfquery name="qUsers" datasource="thisData">
SELECTname, id, adminStatus
FROMUser
</cfquery>
Use the following content to reference the management status of Ginger:
qUsers.adminStatus[2]
[queryName].[columnName][rowNumber]
Unfortunately, only the ColdFusion application server knows about the ColdFusion query object. Another way to provide the same data structure is to serve as an array of structure variables. Using code similar to the following content, I can convert my query variables into arrays of Structure Variables:
<cfset aUsers = arrayNew(1)>
<cfloop from="1" to="#qUsers.recordCount#" index="i">
<cfset aUsers[i] = structNew()>
<cfloop list="#qUsers.columnList#" index="columnName">
<cfset aUsers[i][columnName] = qUsers[columnName][i]>
</cfloop>
</cfloop>
See demo_query2structarray.cfm.
Then I can reference the management status of Ginger:
aUsers[2].adminStatus
[arrayName][index].[struct key name]
The record set formatted as a structure array is very useful in all aspects. For example, they can be transmitted as Web Service requests or responses through soap, while ColdFusion query objects cannot.
When processing a dataset of data, the event Code uses a very similar data structure: an array of object variables (an array of objects "). For example, the data table shown above may be represented in ActionScript, as shown below:
var users = [ ];
var user1 = {name:'Fred', id:123, adminStatus:false};
users.push(user1);
var user2 = {name:'Ginger', id:234, adminStatus:true};
users.push(user2);
Use an object array as the data provider
The flash component used by the cfselect and cfgrid labels supports the attribute dataprovider. One way to populate the cfselect tag is to create an array of string values and set it to the data provider of your control. You use each string as the visible content of a tag or a row of controls. For example:
<cfsavecontent variable="loadData">
var users = ["Jan", "Joe", "Bob"];
userSelect.dataProvider = users;
</cfsavecontent>
<cfform format="flash" width="300" height="500">
<cfinput name="loadData" type="button" value="Load Data"
onclick="#loadData#">
<cfselect name="userSelect">
<option>default</option>
</cfselect>
</cfform>
See demo_cfselectdataprovider1.cfm for a complete sample code.
In this example, the initial tag "default" is replaced with a new group of tags. You can also use.addItem()
Instead of replacing them. For example:
<cfsavecontent variable="loadData">
var users = ["Jan", "Joe", "Bob"];
for(var i=0; i < users.length; i++)
{
userSelect.addItem(users[i]);
}
</cfsavecontent>
<cfform format="flash" width="300" height="500">
<cfinput name="loadData" type="button" value="Add Data"
onclick="#loadData#">
<cfselect name="userSelect">
<option>default</option>
</cfselect>
</cfform>
See demo_cfselectdataprovider2.cfm to obtain the complete code example.
Of course, you can also add user-supplied data to the control, as shown below:
<cfsavecontent variable="addData">
var newLabel = newData.text;
userSelect.addItem(newLabel);
</cfsavecontent>
<cfform format="flash" width="300" height="500">
<cfinput name="loadData" type="button" value="Add Data"
onclick="#addData#">
<cfinput name="newData" type="text" />
<cfselect name="userSelect">
<option>default</option>
</cfselect>
</cfform>
See demo_cfselectdataprovider3.cfm for details.
Alternatively, you can specify a data value different from the visible tag for each project in the control, just as you specify an option value, this value is different from the visible option text when default data is specified through tags. Therefore, you can specify an object array to dataprovider, which contains objects with two attributes: tags and data. For example:
<cfsavecontent variable="loadData">
var users = [];
var user1 = {label:'Fred', data:123};
users.push(user1);
var user2 = {label:'Ginger', data:456};
users.push(user2);
userSelect.dataProvider = users;
</cfsavecontent>
<cfform format="flash" width="300" height="500">
<cfinput name="loadData" type="button" value="Load Data"
onclick="#loadData#">
<cfformitem type="text" bind="{userSelect.selectedItem.data}" />
<cfselect name="userSelect">
<option value="default value">default</option>
</cfselect>
</cfform>
See demo_cfselectdataprovider4.cfm.
In the selection control,.selectedItem
Property is a reference to the currently selected row object ("project"), such as the control'sdataProvider
Attribute. If both the tag and data value are specified, the tag references objects with these two attributes.
In the preceding exampleBindIncfformitem
Label. The binding method is similaronchange
Event handle. In the BIND property, place the reference to the property in curly brackets. Once the value of the bound property is changed, the changed value is copiedcfformitem
Label. Therefore, whenuserSelect
When the selected project changes, the new project data is displayed. Of course, you can bind it to the tag attributes.