First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Ben Johnson

Fine Tuning Your Output

Our employee list box display showed us an example of a simple data transformation. However, there are many instances when we need to really do something to our data. Perhaps you need to convert your data from feet to miles or days to weeks. For the Employee Management application, we decide that we want to display how many years our employees have been with the company. Unfortunately, the data is stored in months and the bindFormatStrings() cannot process complex data transformations, such as addition and multiplication.

Fortunately there’s another data binding function of DataGlue, bindFormatFunction(). This function will allow us to have more granular control as to how our data is formatted. Instead of passing two formatting strings, we pass a reference to a user-defined function that will control all the formatting. The function definition is shown below:

bindFormatFunction( component, recordset, referenceToFunction )

In Listing 3, our callback function now calls bindFormatFunction() and passes the function reference for a user-defined function called myFormattingFunction().

Listing 3

// getAllEmployees_result (callback function)
//
function getAllEmployees_Result (result) {

	DataGlue.bindFormatFunction( employeesChart,
					  result,
					  myFormattingFunction )

}




// myFormattingFunction (user-defined formatting function)
//
// This function is called for each individual row of data
// in the recordset.  
//
function myFormattingFunction (record) {
	// create an object to return to the component for each
	// row of data	
var returnObj = new Object();

	// Note that you must return an object with Label &
	// Value attributes, but UI components require an object
	// with Label & Data attributes.
	returnObj.label = record.firstName
	returnObj.value = Int(record.monthsWithCompany/12 * 100) / 100;
	
	return returnObj;
}

Behind the scenes, DataGlue loops through the recordset and calls the myFormattingFunction for each row. The record argument contains all the data for the current row being processed. Each column can be accessed by its column name so the data in FirstName column for that row can be accessed as record.firstName.

Also note that the return object can vary. The UI components (ListBox, Tree, etc) accept a return object with a Label attribute for the label field and a Data attribute for the data field. The Charting components, on the other hand, accept an object with a Label attribute for the label field and a Value attribute for the value field. You can change what field the value is assigned to by using the setValueSource() method of the Chart components. This is required when using bindFormatStrings() with Chart components. See Listing 4 shows an example of using bindFormatStrings() with Charting Components.

Listing 4

// getAllEmployees_result (callback function)
//
function getAllEmployees_Result (result) {
	// set Value field to ‘data’
	employeesChart.setValueSource(“data”);
	
	DataGlue.bindFormatStrings( employeesChart,
					 result,
					 “#FirstName# #LastName#”,
 “#MonthsWithCompany#” )

}
» Level Intermediate

Added: 2002-08-16
Rating: 7 Votes: 20
(10 being the highest)
» Author
Ben Johnson has been programming for seven years and creating web applications for the past two years. He is currently an information architect for Architekture.com, creating web applications using Flash and ColdFusion.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.