Flash Kid Logo  
Home - Tutorials - Applications - Games - Books - Directory - Site Map - Contact Us  
 
 


 

 

Binding data service to Flex DataGrid component
Author: Salman Awan
Difficulty Level: Advanced
Flash Version: Flash Builder 4 or later
Sources to Download: binding_data_service_to_Flex_DataGrid_component.zip
Prerequisites: Flash Builder, Php, Local Web Servers

Introduction:

One of the main reasons why Flash Builder (Flex) was introduced is to support flash applications that deal with large amount of data presentation on the web. For that purpose Flash Builder 4 now provides great support for dealing with data services in an easy and intuitive way.

In this tutorial we will use a PHP Service and bind it to a DataGrid component provided by Flex Builder. Creation of PHP service is not within the scope of this tutorial so we’ll use one already created.
 
 

Configuring our Project:

Create a new Flex Project and drag-drop a DataGrid component on the stage. Don’t bother configuring the DataGrid as of now since we’ll need to add columns manually once we bind it to a data service.

New Flex Project with DataGrid component on stage
Fig. 1 - New Flex Project with DataGrid component on stage


Setting-up a PHP Service:
Create a new folder under project root and name it ‘services’. Download the php service file provided with the source of this tutorial and place it in services folder.

Note: Make sure to edit the database server settings and credentials specified in CustomerService.php file.


Setting-up a Database:


If you would examine the CustomerService.php file you can reverse engineer the database schema from it. However you can also get the schema creation script from this tutorial’s source files in db folder. Fill Customers table with some dummy data.

Note: At this point verify your database connection using a simple test php file and see if data is retrieved fine outside flash builder. It will also ensure that your local web server supports php files, otherwise you’ll have to install php support.

Setting Project’s Server Technology
Before connecting to any data service, we will need to set the project’s server technology appropriately, in our case PHP. So after selecting project in Package Explorer, select:
Project > Properties

In list of project properties groups select Flex Server option. In Flex Server properties choose PHP to be the Application Server Type, fields specific to php server configuration will appear.

Configuring Server type for a Flash Builder Project
Fig. 2 - Configuring Server type for a Flash Builder Project


You will be required to Validate Configuration before pressing Ok button. So make sure your Web Root and Root URL settings are correct before pressing Ok button.

Connect to Data/Service:

Once Flex Server is configured, we can connect to the data service we just created. Select from menu:

Data > Connect to Data/Service…

Connect to Data Service - selecting a service type
Fig. 3 - Connect to Data Service - selecting a service type


Pressing Next button will show Configure PHP Service dialogue. Browser to the CustomerService.php file we added to our project and Flex will automatically fill the Service details.

Configuring php data service
Fig. 4 - Configuring php data service


Note: At this point Flex will install the Zend Framework for data processing. In case Zend Framework is not installed properly or Flex is unable to access it, you won’t be able to move forward with this tutorial so you would need to troubleshoot the Zend Installation.

Pressing next button will make Flex to introspect the service and present all the operations available through this service.

Service Operations listing
Fig. 5 - Service Operations listing


Pressing Finish will make this service and its operations available to UI Components of this project. You can view this service through
Window > Data/Services panel.

Bind DataGrid to Data:

Now we are ready to bind our DataGrid to service we have just registered. Right click  on the DataGrid component placed on the stage and select Bind to Data option from context menu.


Bind to data properties dialogue
Fig. 6 - Bind to data properties dialogue


We need to configure a valid return type for this service’s operation. Click on Configure Return Type button.

Configure return type of data service operation
Fig. 7 - Configure return type of data service operation

Flex will again help you with automatically suggesting a return type, Object in our case. Press Finish and then Ok to close the ‘Bind to Data’ pop-up.

You would now see the DataGrid component is reset to zero columns. So our next step would be to configure these columns.

Configuring the Columns:

Right click on the DataGrid component and select Configure Columns option from context menu. Configure Columns wizard will appear. Press Add button and add three columns having dataField and headerText properties set to same as their column names.

Tip: Use the Advanced View in Configure Columns wizard to configure these columns.

Configure Columns for a DataGrid component
Fig. 8 - Configure Columns for a DataGrid component


Pressing ok will update the DataGrid component on the stage. Save the project and we are good to run our application Run > Run Main (Ctrl + F11).

Flex DataGrid component populated with data using a php data service
Fig. 9 - Flex DataGrid component populated with data using a php data service

 

 
 

Exercises:

  1. Configure this project to add another column 'id' in the DataGrid that is already maintained in database.
  1. Add a new column homephone in database and update project all the way till DataGrid to display data in DataGrid.

  2. Configure DataGrid columns further to have different background colors and text settings etc.

  3. Locate in your filesy stem where Flex has installed Zend Framework and explore it's folder structure.


References:

http://www.adobe.com/devnet/flex/testdrive/articles/1_build_a_flex_app4.html

 
 
 
 
 
 
Related Links
Discussion Forums for Tutorials
 
 
 
Feedbacks :
 
 
 
Send Feedback
Your Name:
Email:
Comments:
 
Code:
 


 
 
 
© 2011 flashkid.org! All Rights Reserved.
 
   
 
a project of: salmanawan, other projects: xonsolutions.com