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>
from:http://www.switchonthecode.com/tutorials/flex-datagrid-adding-rows-using-a-popup
相关推荐
Flex DataGrid是一款强大的数据展示组件,常用于Adobe Flex或Flash Builder等开发环境中。它能够高效地展示大量数据,并提供丰富的用户交互功能。在本场景中,我们关注的是“前台分页”这一技术。 前台分页是指在...
Flex Datagrid 是Adobe Flex框架中用于展示数据集的组件,它允许开发者以表格形式展示数据,并提供多种交互功能。在Flex应用中,Datagrid经常被用于处理和展示大量的结构化数据。在标题“Flex Datagrid checkbox实现...
在Flex开发中,数据网格(DataGrid)是用于展示大量结构化数据的常用组件。它允许用户以表格的形式查看和操作数据。当我们处理的数据需要进行分类或者分组时,Flex DataGrid 提供了表头分组的功能,这使得数据的展示...
在Flex编程中,DataGrid组件是用于展示结构化数据的强大工具。它允许用户对数据进行排序、筛选和编辑。在实际应用中,我们经常需要实现全选和反选功能,以便用户可以一次性选择或取消选择所有条目。本文将详细讨论...
在Flex开发中,数据网格(DataGrid)是用于展示数据集合的强大组件,它允许用户以表格形式查看和操作数据。本示例关注的是在DataGrid中嵌套复选框(Checkbox),并实现全选功能。这在需要用户批量选择或操作数据的...
flex datagrid 实现合计功能控件包 带源码,demo fxp是flex4的工程文件,导入到工作空间即可 直接导入项目即可。 更多访问我的blog www.dplayer.net
Flex DataGrid是一个强大的数据展示组件,它在Adobe Flex框架中被广泛使用,用于显示和操作大量结构化的数据。这个组件提供了灵活的布局选项,可定制的列格式化,以及丰富的用户交互功能,如排序、筛选和编辑。...
在Flex开发中,DataGrid组件是一个非常常用的控件,它用于展示数据集合,并提供交互式操作,如排序、选择等。本示例聚焦于DataGrid中的CheckBox集成,特别是实现一个全选的功能。以下是对这个主题的详细解释: 一、...
在开发基于Adobe Flex的应用程序时,我们经常遇到需要将数据展示在用户友好的方式中,例如使用DataGrid组件。然而,有时用户希望将这些数据显示在更传统的格式中,如Microsoft Excel电子表格。本教程将详细介绍如何...
`DataGrid`组件是Flex提供的一种强大的工具,用于显示表格形式的数据。在这个实例中,我们将探讨如何利用Flex的`DataGrid`组件结合XML文件来实现动态数据列表。 首先,让我们深入了解`DataGrid`组件。`DataGrid`是...
Flex DataGrid 是 Adobe Flex 框架中的一个组件,它用于在应用程序中显示表格数据。在Flex中,实现数据网格的总计功能是一项常见的需求,它能够帮助用户快速地理解和分析大量数据。本项目提供了一个已经实现了总计...
在Flex开发中,数据网格(DataGrid)是一个非常重要的组件,它用于展示和管理大量结构化数据。在标题“flex datagrid doubleclick 实例”中,我们关注的是如何实现DataGrid组件对用户双击事件的响应。双击事件通常...
在Flex开发中,Datagrid组件是用于展示数据集的一个强大工具,它允许用户以表格的形式查看和操作数据。本文将深入探讨如何在Flex的Datagrid中实现数据的合计与平均值计算,帮助开发者更好地理解和应用这些功能。 ...
在Flex开发中,DataGrid控件是用于展示数据表格的常用组件,它可以高效地呈现大量结构化信息。在某些情况下,我们可能需要根据特定条件改变DataGrid中的某一行或几行的背景颜色,以突出显示或者区分不同的数据状态。...
Flex DataGrid 分页是Adobe Flex开发中一个关键的用户界面组件功能,用于处理大量数据时提高性能和用户体验。在Web应用程序中,一次性加载所有数据可能导致页面加载缓慢,消耗大量内存,而分页则能有效地解决这个...
在Flex开发中,DataGrid组件是用于展示结构化数据的强大工具。它允许用户以表格的形式查看和操作数据,常用于创建数据密集型的应用界面。在实际应用中,我们经常需要根据特定条件改变DataGrid中某一行的背景颜色,以...
Flex DataGrid是一款基于Adobe Flex技术的数据展示控件,它允许开发者在Web应用中展示大量数据并进行高效管理。在描述中提到的“flex datagrid pagination”是指DataGrid控件的一个重要特性——分页功能。在处理大...
在本文中,我们将深入探讨如何在Flex中设置`DataGrid`的数据提供者(`dataProvider`),以便动态地填充数据。`DataGrid`是Adobe Flex中一个非常重要的组件,它用于显示和操作网格形式的数据。理解如何有效地设置数据...