Advanced ColdFusion flash form Technology-use an object array to process Record Sets in ActionScript

Source: Internet
Author: User
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,.selectedItemProperty is a reference to the currently selected row object ("project"), such as the control'sdataProviderAttribute. If both the tag and data value are specified, the tag references objects with these two attributes.

In the preceding exampleBindIncfformitemLabel. The binding method is similaronchangeEvent 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 copiedcfformitemLabel. Therefore, whenuserSelectWhen the selected project changes, the new project data is displayed. Of course, you can bind it to the tag attributes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.