When working with Flex one component almost always shows up, the DataGrid. Now it may not just be the DataGrid but any List based control. It is rare that I create any type of data driven application and not use one of these controls. Along with displaying information it is also useful to be able to add data. Today, I am going to go over a method that can be used for this task. The way we are going achieve data insertion is by using a popup that will hold a simple form to fill out.
If you are not familiar with the DataGrid I suggest you check out one of our other tutorials on it. Those tutorials should be able to bring you up to speed as I am not going to spend much time going into the nitty gritty detail of the DataGrid or basic interface setup.
Below we have the demo application we are going to build today. It is a very simple note taking application. The part that interests us is the "Add Note" button and the corresponding actions. You can also right click the application to view the source.
The first thing we need to do is put together a very basic interface to hold our data. This is going to include a panel with our title of "Notes", a DataGrid to show the notes, and a button to add a note.
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="500" height="300">
<mx:Panel title="Notes"
width="100%" height="100%"
layout="vertical" horizontalAlign="right"
paddingTop="3" paddingLeft="3" paddingRight="3" paddingBottom="3">
<mx:DataGrid width="100%" height="100%">
<mx:columns>
<mx:DataGridColumn headerText="Author" dataField="author" width="80"/>
<mx:DataGridColumn headerText="Topic" dataField="topic" width="100"/>
<mx:DataGridColumn headerText="Description" dataField="description"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Add Note"/>
</mx:Panel>
</mx:Application>
None of the code above should look foreign, it is the same basic UI elements that are used in many of the DataGrid tutorials. The three columns associate with the variables on our Note class. What Note class you ask? Well the one we are adding right now.
{
public class Note
{
public var author:String;
public var topic:String;
public var description:String;
}
}
To really start getting things moving we need a Script block. In the script area we are going to add a variable to hold our notes. I added an ArrayCollection named notes to hold the notes. This collection can be bound to the data provider of our DataGrid. Below we see our new script block which can be added directly under the Application tag. The second snippet is our updated DataGrid tag.
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var notes:ArrayCollection = new ArrayCollection();
]]>
</mx:Script>
<mx:columns>
<mx:DataGridColumn headerText="Author" dataField="author" width="80"/>
<mx:DataGridColumn headerText="Topic" dataField="topic" width="100"/>
<mx:DataGridColumn headerText="Description" dataField="description"/>
</mx:columns>
</mx:DataGrid>
The next piece we are going to build is our popup, I created a new MXML component and named it AddNote. This is going have the fields to fill out for our new note. It also has two buttons, Cancel and Save. I based the component off a TitleWindow.
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="348" height="218"
title="Add A Note">
<mx:Label text="Author" x="35" y="10"/>
<mx:TextInput id="author" width="150" x="84" y="8"/>
<mx:Label text="Topic" y="36" x="42"/>
<mx:TextInput id="topic" width="150" x="84" y="34"/>
<mx:Label text="Description" y="62" x="10"/>
<mx:TextArea id="description" width="234" height="77" x="84" y="61"/>
<mx:Button label="Cancel" x="193" y="146"/>
<mx:Button label="Save" x="264" y="146"/>
</mx:TitleWindow>
Okay, so we now have a popup to hold all our information. We need to add some code back into our main application to create the popup and show it when appropriate. The first step is to add a creation complete event handler with some initialization code.
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="500" height="300"
creationComplete="init()">
The handling function init will create an instance of the popup to be used. We also need to create the private variable that is the popup.
private function init():void
{
addNoteScreen = new AddNote();
addNoteScreen.addEventListener("SaveNote", saveNote);
}
The second step is to add a click event handler for our "Add Note" button.
We now need to create the addNote function which will show the popup. The main worker in this function is a class called PopUpManager which simplifies showing and removing of popups. After the following code I will explain a couple of the function calls in detail.
{
PopUpManager.addPopUp(addNoteScreen, this, true);
PopUpManager.centerPopUp(addNoteScreen);
addNoteScreen.author.text = "";
addNoteScreen.topic.text = "";
addNoteScreen.description.text = "";
}
There are two function calls to look at. The first addPopUp shows the popup, we pass in the component to show as the popup (our AddNote class), the parent of the popup, and whether to make it modal (means it is only thing that can be clicked on). The second function call, centerPopUp, centers the popup in its parent. We also reset all the fields on our popup.
Once the component is popped up we can do two things - save the note or cancel. Cancel is easy as we simply need to add a function to our AddNote component to close itself and hook that into the Cancel button. The function is added in a script block and we update our button to call our close function when clicked.
<![CDATA[
import mx.managers.PopUpManager;
private function close():void
{
PopUpManager.removePopUp(this);
}
]]>
</mx:Script>
The close function utilizes the PopUpManager again to remove itself from the screen. To save a note we are going dispatch a custom event from our component. We can add a custom event using the
Event metadata tag. This is wrapped in the Metadata tag and added right beneath the main TitleWindow tag. I named the event "SaveNote".
[Event(name="SaveNote")]
</mx:Metadata>
To dispatch the event we hook into the click event handler of our Save button.
The function to save is added to the script of the component. The function simply dispatches a new event of type "SaveNote".
{
this.dispatchEvent(new Event("SaveNote"));
}
This completes all the code for our custom component. Below you can see all the code for the AddNote component.
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" width="348" height="218"
title="Add A Note">
<mx:Metadata>
[Event(name="SaveNote")]
</mx:Metadata>
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
private function close():void
{
PopUpManager.removePopUp(this);
}
private function save():void
{
this.dispatchEvent(new Event("SaveNote"));
}
]]>
</mx:Script>
<mx:Label text="Author" x="35" y="10"/>
<mx:TextInput id="author" width="150" x="84" y="8"/>
<mx:Label text="Topic" y="36" x="42"/>
<mx:TextInput id="topic" width="150" x="84" y="34"/>
<mx:Label text="Description" y="62" x="10"/>
<mx:TextArea id="description" width="234" height="77" x="84" y="61"/>
<mx:Button label="Cancel" click="close()" x="193" y="146"/>
<mx:Button label="Save" click="save()" x="264" y="146"/>
</mx:TitleWindow>
Handling the event on the side of the main application is also quite easy. We first create the function to handle the event. I called the function saveNote. It will create a new Note object and set the properties on it from the values in the text fields from the popup. It then adds the new note to our notes collection so that the DataGrid will update and show the note. Finally, it removes the popup from the screen.
{
var note:Note = new Note();
note.author = addNoteScreen.author.text;
note.topic = addNoteScreen.topic.text;
note.description = addNoteScreen.description.text;
notes.addItem(note);
PopUpManager.removePopUp(addNoteScreen);
}
The final thing we need to do to make this all work is actually listen for our "SaveNote" event from our popup window. This can be added to our init function. This completes the code for our main application which is in its entirety below.
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="500" height="300"
creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import mx.collections.ArrayCollection;
[Bindable]
private var notes:ArrayCollection = new ArrayCollection();
private var addNoteScreen:AddNote;
private function init():void
{
addNoteScreen = new AddNote();
addNoteScreen.addEventListener("SaveNote", saveNote);
}
private function addNote():void
{
PopUpManager.addPopUp(addNoteScreen, this, true);
PopUpManager.centerPopUp(addNoteScreen);
addNoteScreen.author.text = "";
addNoteScreen.topic.text = "";
addNoteScreen.description.text = "";
}
private function saveNote(e:Event):void
{
var note:Note = new Note();
note.author = addNoteScreen.author.text;
note.topic = addNoteScreen.topic.text;
note.description = addNoteScreen.description.text;
notes.addItem(note);
PopUpManager.removePopUp(addNoteScreen);
}
]]>
</mx:Script>
<mx:Panel title="Notes"
width="100%" height="100%"
layout="vertical" horizontalAlign="right"
paddingTop="3" paddingLeft="3" paddingRight="3" paddingBottom="3">
<mx:DataGrid dataProvider="{notes}" width="100%" height="100%">
<mx:columns>
<mx:DataGridColumn headerText="Author" dataField="author" width="80"/>
<mx:DataGridColumn headerText="Topic" dataField="topic" width="100"/>
<mx:DataGridColumn headerText="Description" dataField="description"/>
</mx:columns>
</mx:DataGrid>
<mx:Button label="Add Note" click="addNote()"/>
</mx:Panel>
</mx:Application>
Well, that wraps up this tutorial. If anything doesn't look right or you simply have a Flex related question feel free to leave a comment. As always keep coding and keep checking out our new tutorials here at Switch on the Code.
09/28/2009 - 07:26
very good,
10/05/2009 - 22:46
so useful !!
10/13/2009 - 06:24
:-)Amazing work man, i was finding to do this for last two days .,.. here you come u done it... thanks a lot for this example..Bookmarking you my Fatty
04/07/2010 - 08:33
Hi
Thank you for this wonderful example. It saved me tons of time. Everything seems to be working for my scenario.
But I have a problem. I hope you can resolve it.
I have a form with 2 datagrids in it. Upon doubleclick of any row, popup shows up with all the data for that record. The data for datagrid is coming from sql server via coldfusion. In that popup users should be able to enter a new record or update an existing record. i defined my remoteobject in Main.mxml and defined all the functions in the same. But when referring to these functions in AddNote.mxml add/save button click event, nothing seems to happen.
Here are the functions
private function insertValve1():void
{
DBFIL.insertValve(addNoteScreen1.filid_valve1.text,addNoteScreen1.valve.text,addNoteScreen1.cylinder.text,addNoteScreen1.date.text);
}
private function updateValve1():void
{
DBFIL.updateValve(addNoteScreen1.filid_valve1.text,addNoteScreen1.valve.text,addNoteScreen1.cylinder.text,addNoteScreen1.date.text);
}
private function editValve():void
{
if (addNoteScreen1.valve_bt.label == 'Add')
{
insertValve1();
}
else
if (addNoteScreen1.valve_bt.label == 'Save')
{
updateValve1();
}
}
private function saveNote1(e:Event):void
{
editValve();PopUpManager.removePopUp(addNoteScreen1);
}
Hope I provided you enough information.
Should I define the remoteobject in the AddNote.mxml?
Thanks,
Kevin
08/23/2010 - 04:24
Tried this with flash builder 4, and had to change a few things, but it seems to not like the add note declaration. finds no type.
1046: Type was not found or was not a compile-time constant: AddNote. line 17 Flex Problem
please advise.
05/14/2010 - 03:45
Thanks for the clear and simple example
06/03/2010 - 00:30
give me how to pass data between main page to popup window in Flex with an example
06/03/2010 - 09:27
Are you talking the the webpage or just the main window of the flex application. If the latter you can simply set properties or pass in an object when the popup is created.
07/01/2010 - 15:14
Thanks a lot for the example.
09/01/2010 - 04:39
hi. its good example . thanks for helping.
09/05/2010 - 05:32
Hi your website is really cool. I always come here to get my doubts cleared. Thanks a lot.
I have got one task related to inserting a new row in the datagrid which is already containing a data. In the columns I want to have components like combo box, numeric stepper as item editors. For the rows that are already containing the data editing works. But how can I allow user to insert a new row? When user clicks on a row thts not containing the data item editor doesnt work. Kindly help me with this. Thanks
10/17/2010 - 08:09
it is great example and i am really in my first apps. and i wish i could add Print button to the add Note as well so i could print whatever i saved
11/19/2010 - 14:25
Hello - great example!
I am creating a datagrid in the same way, but I have an additional column that totals up the values of each product using a label function. Since the other data is in the array (ex: your data in the notes array), how do I get the values from the last column that uses a label function and is not included in the data provider array?
Is there actionscript that can loop through each row of a column in a datagrid?
Thanks!
11/23/2010 - 00:38
its nice but it some mistakes
there is no validation between rows if i am not entered any values its updated the row and it will show the new row to entered values.
but its doesnt matter to change the code
thanks
02/08/2011 - 00:46
the information which u had provided is not sufficient...display more examples on data grid with a titlewindow.....how can we update data from a title window to the data grid....this is what we need....
02/23/2011 - 12:15
When i click on addnote it gives me with this error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.managers::PopUpManagerImpl/addPopUp()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\PopUpManagerImpl.as:250]
at mx.managers::PopUpManager$/addPopUp()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\managers\PopUpManager.as:169]
at main/addNote()[C:\Documents and Settings\muhammadsul\workspace\ShippingApplication\src\main.mxml:21]
at main/___main_Button1_click()[C:\Documents and Settings\muhammadsul\workspace\ShippingApplication\src\main.mxml:44]
plz help me out with this
05/01/2011 - 11:58
Hi.. This is a really good example but could you let us know how to do the same using an XML Collection as dataprovider
05/20/2011 - 12:31
I'm really grateful for this example - I'm a newbie flex developer.
Any chance you could update this for Spark architecture? I've tried and am getting the following error on the "Add Note" button click:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Thank you very much!
Mark
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.