This tutorial is going to build on an earlier tutorial about the Flex DataGrid component. It's going to show how to do several things including how to use xml with a datagrid, how to use item renderers and label functions, and how to style and skin parts of the datagrid and scrollbar. Also at the end of the tutorial I supply a zip file with all the source code and assets needed to create the example in this tutorial.
We are going to start by looking at an example program that shows off what we are going to learn today. You will notice that the datagrid doesn't have any header labels or vertical grid lines. I have also skinned the scroll bar and changed some colors. All the data comes from two xml files that we will write here in a minute. The data shown in the datagrid is done in a couple different ways, which we will go over later.
Creating XML Data
The first item to take care of is getting our data together. This done using a couple of xml files. The first file has the people and their favorite game. The second xml file is a list of games that people can have for their favorite game and information about that game.
The first file is people.xml.
<?xml version="1.0"?>
<people>
<person>
<name>
<first>Charlie</first>
<last>Key</last>
</name>
<info>
<age>23</age>
<sex>Male</sex>
</info>
<favGameID>1</favGameID>
</person>
<person>
<name>
<first>Brandon</first>
<last>Cannaday</last>
</name>
<info>
<age>23</age>
<sex>Male</sex>
</info>
<title>CFO</title>
<favGameID>3</favGameID>
</person>
<person>
<name>
<first>Michael</first>
<last>Kuehl</last>
</name>
<info>
<age>23</age>
<sex>Male</sex>
</info>
<title>CTO</title>
<favGameID>5</favGameID>
</person>
<person>
<name>
<first>Kevin</first>
<last>Putvin</last>
</name>
<info>
<age>23</age>
<sex>Male</sex>
</info>
<title>Lazy Guy</title>
<favGameID>2</favGameID>
</person>
<person>
<name>
<first>Richard</first>
<last>Key</last>
</name>
<info>
<age>20</age>
<sex>Male</sex>
</info>
<title>Underling</title>
<favGameID>4</favGameID>
</person>
<person>
<name>
<first>Caroline</first>
<last>Andersen</last>
</name>
<info>
<age>23</age>
<sex>Female</sex>
</info>
<title>Supporter</title>
<favGameID>6</favGameID>
</person>
</people>
And the second is games.xml.
<?xml version="1.0"?>
<games>
<game id="1">
<name>World of Warcraft</name>
<publisher>Vivendi Universal</publisher>
<developer>Blizzard</developer>
<imageURL>wow_small.jpg</imageURL>
</game>
<game id="2">
<name>Halo 2</name>
<publisher>Microsoft Game Studios</publisher>
<developer>Bungie Studios</developer>
<imageURL>halo_small.jpg</imageURL>
</game>
<game id="3">
<name>Half Life 2</name>
<publisher>Valve Corporation</publisher>
<developer>Valve Corporation</developer>
<imageURL>hl_2_small.jpg</imageURL>
</game>
<game id="4">
<name>Gears of War</name>
<publisher>Microsoft Game Studios</publisher>
<developer>Epic Games</developer>
<imageURL>gears_of_war_small.jpg</imageURL>
</game>
<game id="5">
<name>The Elder Scrolls IV: Oblivion</name>
<publisher>2K Games</publisher>
<developer>Bethesda Game Studios</developer>
<imageURL>oblivion_small.jpg</imageURL>
</game>
<game id="6">
<name>The Legend of Zelda: Twilight Princess</name>
<publisher>Nintendo EAD</publisher>
<developer>Nintendo</developer>
<imageURL>zelda_small.jpg</imageURL>
</game>
</games>
Creating The Flex Interface
Creating the Flex interface is a simple task in which nothing really complex is done. I created a new application and added a panel to it. This panel holds all the other user interface components. We have a label at the top of the panel telling everyone that the information displayed is our favorite games. Below this is our datagrid which will hold the bulk of the data. After the datagrid there are a couple items to show details of a game including a large image, which is the image url from our xml data. The following code is our simple interface.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="530" height="610" viewSourceURL="/files/AdvancedDatagrid.mxml">
<mx:Panel width="530" height="610" layout="absolute" title="Advanced Datagrids">
<mx:Label horizontalCenter="0" y="1" text="Our Favorite Games"
fontSize="16" fontWeight="bold"/>
<mx:DataGrid id="dgPeeps" x="0" y="25" width="510" height="260" selectedIndex="0">
<mx:columns>
<mx:DataGridColumn headerText="Name" width="75"/>
<mx:DataGridColumn headerText="Info" width="80"/>
<mx:DataGridColumn headerText="Favorite Game"/>
</mx:columns>
</mx:DataGrid>
<mx:Image x="10" y="318" width="300" height="240" id="imgGameScreen"/>
<mx:Label x="10" y="293" fontWeight="bold" fontSize="13" id="lblGameName"/>
<mx:Label x="318" y="318" text="Developer:"/>
<mx:Label x="318" y="335" id="lblDeveloper"/>
<mx:Label x="318" y="361" text="Publisher:"/>
<mx:Label x="318" y="378" id="lblPublisher" />
</mx:Panel>
</mx:Application>
Well that takes care of the beginning of our application.
Binding the XML Data
Now we get to have little fun. Binding the xml data to the datagrid
isn't that bad. We take advantage of some components that make our life
much easier. The first step is creating an object from the xml source
file. Below is the code to accomplish this. It creates a <mx:Model>
(click here
for more info on this tag) object that represents the xml data from the
source file.
<mx:Model id="people" source="/data/people.xml"/>
Next we create another component of type <mx:XML>. This, as you can
probably figure out, makes a XML object. I don't use a data model this
time because we are going to use the games xml data a little
differently. We will take advantage of one of the xml tricks we can do
in Flex and we also do not need to bind the data to a DataGrid. Grabbing
our games xml data looks like:
<mx:XML id="games" source="/data/games.xml"/>
Both of the above tags will go right after the opening application tag.
Now we can simply bind the datagrid to our data model of the people from
our xml data. The only thing we need to do is make sure that we bind to
the correct property. Using <mx:Model> creates an object of the entire
xml data, therefore we need to go down one level to the person
property of our people model. The new datagrid looks like:
<mx:DataGrid id="dgPeeps" x="0" y="25" width="510" height="260"
selectedIndex="0" dataProvider="{people.person}">
This takes care of binding the data but none of it is going to show up yet because we haven't added any rendering code. The next thing we are going to do is display the data we now have.
Displaying DataGrid Data
This section is going to introduce a few new concepts. The first feature
we are going to look at and use are label functions. A lot of the time
we have complex data that we want to display in our datagrids and using
the dataField property just doesn't cut it. A label function will take
in the current item being processed and the data grid column and will
return a string. The returned string is what will be displayed in the
cell.
We are going to use a label function for the info column. To declare we
are going to use one, all that is needed is to set the labelFunction
property. This is set to displayPersonInfo which is an actionscript
function we will create in just a moment. The new datagrid column code
looks like the following.
<mx:DataGridColumn headerText="Info" width="80" labelFunction="displayPersonInfo"/>
Next we create the actionscript function in a new <mx:Script> tag
which goes right beneath the opening application tag. Inside our
function we are going to concatenate the two pieces of info (check the
xml structure to see what I mean) from our item. In the code below you
can see the two parameters that I brought in which were explained in the
earlier paragraph. We use the item parameter and grab item.info.age
and item.info.sex and return them as a combined string. That is the
basics of a label function. Also note that any label function needs to
take in both parameters (even if they're not used) and return a string.
<mx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridColumn;
private function displayPersonInfo(item:Object, col:DataGridColumn):String
{
return item.info.age + " " + item.info.sex;
}
]]>
</mx:Script>
At least we are displaying something now. Lets move on to the harder display components. For the other two columns we are going to use item renderers. An item renderer is a set of custom code that displays the data in a column in particular way using mxml components and/or your own custom components. There are four different kinds of item renderers: drop-in, inline, reusable, and component. In this tutorial we use two of them - inline and component. Look for a snippet tutorial in future going over all of them.
The first one we use is the inline item renderer. You might have noticed
when we created the interface that the first DataGridColumn had an
opening and closing tag. Well this was on purpose. We add the item
renderer inside the DataGridColumn tags, hence the name inline. After
the opening tag we add a <mx:itemRenderer> telling Flex that we are
going to define the itemRenderer property right here. Inside the
itemRenderer tag we build a <mx:Component>. Inside the
<mx:Component> tag we can use all the normal ui components we have at
our disposal. Now to refer to the current row data we use the data
object. In ours we simply have two <Label> components added to a
<VBox> and bind the text to the first and last names. Below you will
see our new complete DataGridColumn code.
<mx:DataGridColumn headerText="Name" width="75">
<mx:itemRenderer>
<mx:Component>
<mx:VBox verticalGap="2">
<mx:Label text="{data.name.first}" />
<mx:Label text="{data.name.last}" />
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
The other item renderer we use is going to be its own mxml component.
This means we are going to create an entire new file. This component is
going to use data from the row and also data from our games xml file to
display a small picture and name of the game. We create a new file which
we name "ImageRenderer.mxml" and I put it in a folder named "comp" -
this doesn't matter except when you reference it. In this file we are
going to create a simple custom component which is based off of a HBox
and has an <mx:Image> and a <mx:Label> in it. I also add the same
XML tag as we have in the main file because we are going to need this
data. The basic structure of the component looks like:
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" height="60" verticalAlign="top">
<mx:XML id="games" source="/data/games.xml" />
<mx:Image scaleX=".25" scaleY=".25"/>
<mx:Label width="250" />
</mx:HBox>
Everything looks alright except we aren't setting any data. We set the source of the image to something a little complex - earlier I mentioned we were going to use a cool XML object feature and here is the first place we use it. First off the image tag now looks like:
<mx:Image source="{games.game.(@id == data.favGameID).imageURL}"
scaleX=".25" scaleY=".25"/>
This looks a little crazy but we'll step through it. Ignoring
(@id == data.favGameID) and it looks like we are grabbing an imageURL
from the games XML object. Which is exactly what is going on, but we
need to tell it which game in the xml. To do this we use the id
attribute we defined for each of the games in the xml file. Also in our
people xml file we referred to each person's favorite game by the id so
we can use this to match up the correct game. Then all that
(@id == data.favGameID) is saying is that we are going use the id
attribute on game (because it comes right after that property) and get
the game with id equal to data.favGameID, this is the same data object
as before.
That takes care of the image, on to the label. We use the same technique as above but we just get the game name now. The new Label looks like the code below.
<mx:Label text="{games.game.(@id == data.favGameID).name}" width="250" />
This completes our new component. The whole file now looks like:
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" height="60" verticalAlign="top">
<mx:XML id="games" source="/data/games.xml" />
<mx:Image source="{games.game.(@id == data.favGameID).imageURL}"
scaleX=".25" scaleY=".25"/>
<mx:Label text="{games.game.(@id == data.favGameID).name}" width="250" />
</mx:HBox>
To hook this into our last datagrid column we simply set the
itemRenderer property equal to the name of this component. In my case
comp.ImageRenderer - remembering that I put it in the "comp" folder so
I have to put this at the beginning. The DataGridColumn code now looks
like the following.
<mx:DataGridColumn headerText="Favorite Game" itemRenderer="comp.ImageRenderer"/>
We now have a datagrid fully displaying all the information. Take a break and grab a drink...Ok we're back - on to the next part.
Display Selected Data
In this part we are going to display the large image and data about the currently selected game. We will use the xml feature we went over earlier extensively in this section. I am not going to go real far in depth about these components because it is all things that we have done before.
The larger image will have its source set to the image url of the
currently selected item's favorite game. The labels will have their
values set in the same fashion. To get to the selected item's favorite
game id we will use dgPeeps.selectedItem.favGameID. This grabs the
currently selected item from the datagrid and then gets the favGameID
from the selected item. So the whole new lower ui section now looks
like.
<mx:Image x="10" y="318" width="300" height="240" id="imgGameScreen"
source="{games.game.(@id == dgPeeps.selectedItem.favGameID).imageURL}"/>
<mx:Label x="10" y="293" fontWeight="bold" fontSize="13" id="lblGameName"
text="{games.game.(@id == dgPeeps.selectedItem.favGameID).name}"/>
<mx:Label x="318" y="318" text="Developer:"/>
<mx:Label x="318" y="335" id="lblDeveloper"
text="{games.game.(@id == dgPeeps.selectedItem.favGameID).developer}"/>
<mx:Label x="318" y="361" text="Publisher:"/>
<mx:Label x="318" y="378" id="lblPublisher"
text="{games.game.(@id == dgPeeps.selectedItem.favGameID).publisher}"/>
Sorry about the formatting atrocity (we have a limited page width to work with). Next we get to do some styling and skinning.
Styling the Application
Adobe has given Flex some great features when it comes to styling an
application, I will touch on just a small number of them in this
tutorial. The first change we are going to make is simply setting a
property of the datagrid. We set the showHeaders property to false to
remove the header row. The new DataGrid component has the following
code.
<mx:DataGrid id="dgPeeps" x="0" y="25" width="510" height="260"
selectedIndex="0" dataProvider="{people.person}" showHeaders="false">
The rest of our changes are going to be done in a new file which is going to be a css file. Flex has the ability to use css syntax to define the style of components. You can define styles for components or create css classes and use them in your mxml to give a component a certain style just like in html. My css file is in a folder called "assets" and is named "app.css".
In this file we are going to start by actually embedding a new font from a .swf file - which is included in the zip file. In "fonts.swf" the font face "Myriad Web" is defined. To use this we need to define the different font faces we are going to use. I set up three different font faces one for normal, one for bold, and one for italics. To learn more about fonts in Flex check out the Adobe livedocs here. The following code goes in our css file and will use the "fonts.swf" to create the new font faces.
/** Font Info **/
@font-face
{
src:url("/assets/fonts.swf");
fontFamily: "Myriad Web";
}
@font-face
{
src:url("/assets/fonts.swf");
fontFamily: "Myriad Web";
fontWeight: bold;
}
@font-face
{
src:url("/assets/fonts.swf");
fontFamily: "Myriad Web";
fontStyle: italic;
}
To use these we will set some styles for the Application, we set the
font-family and font-size. To use the new font family I refer to it
using the fontFamily name in the @font-face code. The Application
style is added to the css file and looks like:
Application
{
font-family: "Myriad Web";
font-size: 12;
}
To use these styles we add a new tag to our main mxml file. The tag is
<mx:Style> and we simply set the source property to our css file.
The new tag will go right underneath the opening application tag and
will enable the example to use the new Application style.
<mx:Style source="/assets/app.css"/>
Styling the datagrid is going to be done in the same fashion. Now we will setup a "DataGrid" style in which we remove the vertical grid lines and border. Along with these we also set some colors of the selected row, the rolled over row, and the alternating row colors. This is all pretty self explanatory so I won't go into any more detail. The following code goes into our css file.
DataGrid
{
vertical-grid-lines: false;
border-style: none;
selection-color: #C1FFC5;
roll-over-color: #7CFF77;
alternating-item-colors: #F4FBFF, #FFFFFF;
}
This takes care of updating the style of our datagrid. The last item we
are going to play with is the vertical scroll bar. We can style
VScrollBar to modify all the application's vertical scroll bars. Using
some images I created for the up (upArrow.png) and down (downArrow.png)
arrows and the scroll bar (thumb.png) and scroll background
(scrolltrack.png) I can skin what the scroll bar looks like. All these
images are included in the "assets" folder in the zip file.
We set each of the needed skin styles to our new images. Each piece
except the scroll background has an up, over, and down skin but I set
all three to the same image for simplicity sake. For the thumb image I
used scale-9 to define edge scales, more info on this can be found
here. That
isn't all that's important though. Remember to use the Embed tag to
embed the image into our application. The style ends up looking like the
following.
VScrollBar {
downArrowUpSkin:
Embed(source="/assets/downArrow.png");
downArrowOverSkin:
Embed(source="/assets/downArrow.png");
downArrowDownSkin:
Embed(source="/assets/downArrow.png");
upArrowUpSkin:
Embed(source="/assets/upArrow.png");
upArrowOverSkin:
Embed(source="/assets/upArrow.png");
upArrowDownSkin:
Embed(source="/assets/upArrow.png");
thumbDownSkin:
Embed(source="/assets/thumb.png", scaleGridLeft="7", scaleGridTop="5",
scaleGridRight="8", scaleGridBottom="7");
thumbUpSkin:
Embed(source="/assets/thumb.png", scaleGridLeft="7", scaleGridTop="5",
scaleGridRight="8", scaleGridBottom="7");
thumbOverSkin:
Embed(source="/assets/thumb.png", scaleGridLeft="7", scaleGridTop="5",
scaleGridRight="8", scaleGridBottom="7");
trackSkin:
Embed(source="/assets/scrolltrack.png", scaleGridLeft="7",
scaleGridTop="4", scaleGridRight="8", scaleGridBottom="6");
}
I know that was a real quick intro into styling and skinning in Flex using css. It should be enough to get you started when customizing the look and feel of your own applications. You can find more information about skinning here.
Final Source Code
Well all that is left is to show you all the final code we end up with.
CSS Code:
/* CSS file */
Application
{
font-family: "Myriad Web";
font-size: 12;
}
DataGrid
{
vertical-grid-lines: false;
border-style: none;
selection-color: #C1FFC5;
roll-over-color: #7CFF77;
alternating-item-colors: #F4FBFF, #FFFFFF;
}
VScrollBar {
downArrowUpSkin:
Embed(source="/assets/downArrow.png");
downArrowOverSkin:
Embed(source="/assets/downArrow.png");
downArrowDownSkin:
Embed(source="/assets/downArrow.png");
upArrowUpSkin:
Embed(source="/assets/upArrow.png");
upArrowOverSkin:
Embed(source="/assets/upArrow.png");
upArrowDownSkin:
Embed(source="/assets/upArrow.png");
thumbDownSkin:
Embed(source="/assets/thumb.png", scaleGridLeft="7", scaleGridTop="5",
scaleGridRight="8", scaleGridBottom="7");
thumbUpSkin:
Embed(source="/assets/thumb.png", scaleGridLeft="7", scaleGridTop="5",
scaleGridRight="8", scaleGridBottom="7");
thumbOverSkin:
Embed(source="/assets/thumb.png", scaleGridLeft="7", scaleGridTop="5",
scaleGridRight="8", scaleGridBottom="7");
trackSkin:
Embed(source="/assets/scrolltrack.png", scaleGridLeft="7",
scaleGridTop="4", scaleGridRight="8", scaleGridBottom="6");
}
/** Font Info **/
@font-face
{
src:url("/assets/fonts.swf");
fontFamily: "Myriad Web";
}
@font-face
{
src:url("/assets/fonts.swf");
fontFamily: "Myriad Web";
fontWeight: bold;
}
@font-face
{
src:url("/assets/fonts.swf");
fontFamily: "Myriad Web";
fontStyle: italic;
}
The main mxml file.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
width="530" height="610" viewSourceURL="/files/AdvancedDatagrid.mxml">
<mx:Style source="/assets/app.css"/>
<mx:Script>
<![CDATA[
import mx.controls.dataGridClasses.DataGridColumn;
private function displayPersonInfo(item:Object, col:DataGridColumn):String
{
return item.info.age + " " + item.info.sex;
}
]]>
</mx:Script>
<mx:Model id="people" source="/data/people.xml"/>
<mx:XML id="games" source="/data/games.xml"/>
<mx:Panel width="530" height="610" layout="absolute" title="Advanced Datagrids">
<mx:Label horizontalCenter="0" y="1" text="Our Favorite Games"
fontSize="16" fontWeight="bold"/>
<mx:DataGrid id="dgPeeps" x="0" y="25" width="510" height="260"
selectedIndex="0" dataProvider="{people.person}" showHeaders="false">
<mx:columns>
<mx:DataGridColumn headerText="Name" width="75">
<mx:itemRenderer>
<mx:Component>
<mx:VBox verticalGap="2">
<mx:Label text="{data.name.first}" />
<mx:Label text="{data.name.last}" />
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Info" width="80"
labelFunction="displayPersonInfo"/>
<mx:DataGridColumn headerText="Favorite Game"
itemRenderer="comp.ImageRenderer"/>
</mx:columns>
</mx:DataGrid>
<mx:Image x="10" y="318" width="300" height="240" id="imgGameScreen"
source="{games.game.(@id == dgPeeps.selectedItem.favGameID).imageURL}"/>
<mx:Label x="10" y="293" fontWeight="bold" fontSize="13" id="lblGameName"
text="{games.game.(@id == dgPeeps.selectedItem.favGameID).name}"/>
<mx:Label x="318" y="318" text="Developer:"/>
<mx:Label x="318" y="335" id="lblDeveloper"
text="{games.game.(@id == dgPeeps.selectedItem.favGameID).developer}"/>
<mx:Label x="318" y="361" text="Publisher:"/>
<mx:Label x="318" y="378" id="lblPublisher"
text="{games.game.(@id == dgPeeps.selectedItem.favGameID).publisher}"/>
</mx:Panel>
</mx:Application>
Well I hope that this tutorial is useful for many different things.
Source Files:
thanks for sharing the source code, i really need it ;)
Hi,
What if I want to load the datagrid from two or more providers. say the first two columns from one ArrayCollection and the rest from another ArrayCollection.
Is this possible, if yes can you send me an example.
This isn't strictly possible without extending the datagrid class. What I would say though is that you could build an ArrayCollection made up of objects from both sources. Then you would have to have custom item renderer or label function to display the correct output. This way you wouldn't have to come up with some crazy method of putting two data providers on a datagrid.
This was an awesome example. Helped out a lot on my current project. Thanks
Nice tut! Helped me out with various projects!
I think this tutorial is very helpful. It is very clear and simple. I would just want to know how do you send information from flex to the database?
Thank you!
Check out the other tutorial on flex, php, and mysql I wrote up. If you are looking for something different than that let me know.
I am using custom item renderers. What is the correct way to have the DataGrid CSS directives (such as selection-color) apply to the custom item renderers ? Thanks!
Hi,
Nice example, thanks. I tried modifying your code.
I wanted the ability to update the XML (Developer's name let's say) so I changed 'lblDeveloper' to be a text input.
I also created a button, when clicked, it calls saveData() which is Given Below
Now this is working fine in my flex builder, but when I try to export it, it's not reading data from the XML file, but using the data I used initially to build it.
I am sure that I am doing something wrong, but what is it? Can you point me to the right direction.
Thanks
When you load xml as mx:Model or mx:XML, it compiles that XML file into your SWF. You can make it load dynamically at run time by pulling from an HTTPRequest. The return type e4x will return the same type of object in mx:XML, and a return type of array will mimic mx:Model
How come when I export this application, the xml files games.xml, and people.xml don't get exported?
Thanks in Advance
Please answer my Question.
Thanks
So are you exporting a release version? Also the files won't be embedded into the application.
Charlie
I am making a calendar display and want to show all events for a given day in a datagrid cell. I have included a sample of my XML and my data grid code. Any idea how I can show multiple records in one data grid cell (for example I have a boys event and a girls event scheduled for Dec 12)?
December 9 2007-12-09
10 2007-12-10
11 2007-12-11
12 2007-12-12 18:45:00 6:45 Boys Program
12 2007-12-12
6:45 Girls Program
Tedd,
You could do this by writing your own item renderer and then in the objects in your data provider you could have one variable that could be an array of events. Then in the item renderer you could then loop through the events for the given day. So basically the objects in the dataprovider represent each day and each day has events. Does this make any sense?
It makes perfect sense, I have actually started to do that. I will let you know how it comes out...
Is there any particular reason that this application doesn't need to override the 'set data' method in the renderer??
BMW, it doesn't need to override it because there is no reason to in this case. The data gets set by the datagrid on the renderer and we don't need to modify it or anything special so we can just use it as it comes in. Now if you wanted to change something like the background color of the item renderer depending on the data you would then need to override the set data method. But if you are using the data as is there is no reason to.
Thanks for the explanation fattest
thanks a lot for this valuable example!
I just face one problem that clicking the scroll bar area for page down/up doesn't work. Is there a solution for that?
Regards, Gennady
This code helped me a lot in understanding the xml binding with data grid. thanks for sharing the code thanks a lot
Thanks a lot. This is really nice example.
Very nice example. Do you know how can I put a image as background for the list ? Do I have to do implement a special render for the datagrid background ? I intent to build something like itune Artwork in flex do to a slide show - any hint are appreciated!
Q
In {data.name.first}, where is data defined or what does it refer to?
Thanks
This refers to the data in the item renderer that is always set by the DataGrid, check out this tutorial on item renderers to help explain in a little more detail how this works. But basically data is the object passed to the item renderer and then we can pull out the first name by digging down though the object.
NO .FLA IN THE ZIP????
Flex does not use .fla files. This is not flash. The files compile with the Flex SDK to .swf file. If you're talking about the fonts I got the swf from Adobe to use.
Wow, thanks for sharing!!!
Thanks for a short and simple tute , can we customize a complete column in a datagrid , i mean all the rows under a single column, i m trying to do that but unable to find any solution wud some one pls help me!! I ll appreciate ur Help regards, Lisa
Lisa, Yeah sure we can, what are trying to customize about the column? Maybe I can help with a quick solution.
hey thanks fat am actually trying to make the first column a customised row where in just like in MS office excel i was able to customize the column header but not the first column or rather the vertical row can u help me on this pls if u have a solution can u mail me to lisamargret@yahoo.com regards, Lisa
Lisa, have you tried a custom item renderer in the column, ignoring the data that is coming in? If you are looking for something like excel I think this would handle it nicely. Let me know if you want an example I will write something up real quick.
if you just want to change the header for a column (like make it bold or include an image), in the DataGridColumn tag, use the property for headerRenderer or headerStyleName
Hi,
Can any buddy help me in drag n drop. I want to drag cell's info from 1st datagrid to marge/replace in a row in another datagrid cell.
Really helpful. Thank you very much!!
Hi,
I have a requirement which is as follows:
"Initially bind first 10 records from 5000 records to the datagrid. On scrolling the scrollbar i need to bind another set of records and so on. Scrolling should be done on both upwards and downwards. i.e. when we go upwards we need to get previous set of records and downwards we need to get next set of records. Another requirement is I need to implement Server-Side Sorting on datagrid".
Can anyone help me in doing so?.
Thanks in advance...
I waant to disply the data in datagrid as a link.How can i do that
Rasmi, I assume you figured out how to do this but I will have this in a tutorial also very soon.
Hi,
How can i change the header color of the sorted column?
Thanks in advance
Thanks for the detailed example
Thanks for the example.
I'm eating this up! Can anyone give me this example with a mx:httpservice url=data.xml example? I have a need to dynamicly update the xml files.
If I understand things correctly, only the httpservice allows you to do that. Either that or you have to recompile every time you get an update.
Such an awesome tutorial!
Joe you are correct you need to use a httpservice to do this. I will have this in a tutorial here soon
i am trying to display the numbers form 1 to 75 with a color and 76 to 100 with another color. could any one of u tell me how to do it in flex.
Srinu, do mean the rows with that data or the single cell with that data?
Really helpful!! The way you've explained the entire concept is amazing. Wonderful effort!!
Ok here is a new tutorial with the some new datagrid features.
Hello guys... do anyone have any idea of how can i populate and check the text when I add to ADG or DG? I mean i want to add data to Datagrid in the mean time, i also want to check the input text.
Example...if the data is a specific text like "Online", I will display an icon/image next to it to indicate this person is online. if "offline", i will display another image. so, my problem is can i check like that? If yes, can u guys guide me, please....
I need ur hlep.
Thanks
scsfdev, yeah I don't think this would be hard, just create an item renderer that checks the data when set and show the icon with the data is appropriate. let me know if you need more info.
I just got it and thanks 'The Fattest' :)
Like the way you said, I used Render and check the status and indicate the current status.
thanks :)
I have searched for a very long time on a way to set a working rotation on the text in a datagrid header at an angle other than +90. I often work with data with large numbers of columns, a descriptive header of some sort, with each cell below containing single character data. Doing this with flex has been far less straightforward to a beginner like me than I had thought it would be.
Originally I had thought it would be really cool to create column headers slanted at 45 deg like you can do with ms excel, overlapping their narrow columns below. Now I'd be happy with a column header that could render the text (embedded is what I've been trying) at -90deg, writing up from the bottom of the header cell. 90deg is the only angle which sort of works though it writes down from the top of cell to bottom. This solution uses a VBox holding a label with rotation="90" as the headerRenderer.
Maybe this is obvious to experienced folks but I've seen a lot of people trying to find how to do this and having trouble finding working solutions.
We have been tring to print a Flex3 datagrid landscape for several days (\$3,000) and gave up... the product does not seem to lend itself to printing datagrids rotated without and MASSIVE amount of programming that is NOT worth the effort. We ended up pushing the grid results to PHP and printing. GOOD LUCK!
nice tutorial,easy and simple,but i want to know how to get values dynamically from database
Hi, this is very nice tutorial but how can i add more item at run time dynamically.
this examle is not working…
error: Definition comp.ImageRenderer could not be found
Please - how to fix this?
Sounds like your missing the mxml file for that. If you right click the example above and view source you will see a folder called comp with a mxml file called ImageRenderer. This file is also in the zip file.
Has anyone else had difficulty in sorting a "date" column in a datagrid? I'm pulling in data from a Lotus Notes view (in XML format) using an HTTPService and "e4x". I'm omitting the 'sortable' parameter in the datagridcolumn object, since it's "true" by default. I click up and down on the column header, but the darn thing won't sort correctly. I can't imagine I'm the only person in the Flex universe who wants to sort a datagridcolumn consisting of dates. If someone else has encountered and conquered this, please raise a hand.
Thanks, Dan Toma
I would guess your going to have to provide a sorting function for the column. You can do this by setting the sortCompareFunction on the DataGridColumn.
Please help. I've managed to select a record from a DataGrid which then sends a primary key to mySQL via Php. The problem I'm having is the DataGrid kind of refreshes losing the currently selected row/record. I'm new to Flex so I guess I could be doing something wrong :O)) Seriously, any help would be appreciated.
So you're going to need to keep track of of the selected item on the datagrid. This is a quick example for storing the item.
Then when you're receiving the result you need to reset the selected item.
Please help, I am working with flex and php. In php side, I retrieve the data from DB and convert the result as xml. Then I recieve the data in Flex(with 'e4x' result format). Everything is OK and fine. but To load data in grid, it takes too long time. How can I reduce the loading time in Flex. Please give me suggestion from server side to client side...!!
Well, if the bulk of the time is spent in actually displaying the data you could load the first 25 results and tack on the rest of the data after loading that. Are you sure the majority of the time is spent displaying the data in the datagrid?
public function getResult(e:ResultEvent):void{ var xmlResult:XMLList=new XMLList(e.result.Catagory); ......... ....... ....... ......... } Now,In xmlResult, it has 2000 rows/tags, which information I will load in DataGrid. but how I will load first 25, then tack the rest of the result in the grid?
Another thing, I am using HTTPService class of flex to get data from server side. Is it a time consuming? is there have another good mechanism(like AMFPHP)that will give me a good result?
Or, how I can appropriately measure, in which point, it spent majority of the time?
hi guyz! The Fattest ! i am a big fan of u.
plz help me , i am trying to make a datagrid that can provide a blank cell structure in editable mode like we have in MS Excel sheet.
any input from ur side would be helpful
Hi TheFattest,
There are no problems with you compile it with my code but I get an error of -1 when exporting my release build with your code. Would you happen to know why? I get two -1 errors for the AdvancedDatagrid.mxml and index.mxml
Your thoughts are much appreciated in anticipation.
Kind regards, eagerlearner
Hi TheFattest,
I fixed the previous problem, but now I am getting mx.core.IUIComponent error from the import model and xml files.
Your help would be appreciated.
Thanks, eagerlearner
I've resolved this now...The binding files cannot be in a panel as this is against the rules of flex
thanks, man!
Hi, I am trying to use an inline itemrenderer to display an image and a label in a datagrid column. I use a dynamic XML to load the datagrid. But somehow it doesnt seem to work for me. Please let me know where i am commiting a mistake.
My Datagrid:
The errors that i get are:
1180: Call to a possibly undefined method setIcons. 1120: Access of undefined property errorList. (errorList is the dataprovider for my datagrid)
Hi - I'm trying to do the same thing - load a different image based on some criteria). Did you ever find a solution? I would be very grateful if you could respond.
Thanks
Hi, Actually i have changed image source and label text as:
Now the errors are gone. But the datagrid column looks shows only the label but not the image.
Try
Hi!
Great tutorial, but i just have an problem. I cant display any pics! Even when i use your files and just run them, the pics still wont display and i dont know what im doing wron, any ideas?
Regards
I have the same problem ? Smth wrong with the code it seems !
Did you download all the images? I'm pretty sure the code works.
The path to the images is probably coming back as "null" - this is defined in two spots: AdvancedDatagrid.mxml and ImageRenderer.mxml
You could try putting all the images in the bin (or bin-debug) folder, and then adding the line...
[language]if (imgPath == null) { imgPath = ""; }[/language]
... immediately after each line where imgPath is originally defined.
A few paragraphs before "Display Selected Data", we are doing -> @id == data.favGameID . I understand that @id is coming from games.xml but how does Flex know that "data" (in data.favGameID) is coming from people.xml ?
When you create an item rendered the data property is set to the current item being rendered. So this mean it will be equal to the current row in the data source. This is just how it is built in. You can take a look at IDataRenderer (i think) for more information.
I am working on the AdvancedDataGrid with a hierarchical data. I don't want to show the horizontal grid line, but at the border of each group, I would like to show the horizontal grid line. Does anyone know how to do that?
The second question is that when the tree node is closed, the tree node level shows the summary (or total) of the group and when the node is open, it shows nothing in that level. Do you have any suggestion?
Thanks for the tutorial.
Hi Fat,..i downloaded the code and imported the source.zip file ,but on executing the code it searches for the {projectname}.html in the bin-debug folder ,,can u please help me to resolve it?
thanks in advance:-)
@Pat - we have a product that might meet your needs with styling the lines. Check out Flexicious Ultimate!
http://www.flexicious.com/Home/IndexUltimate
Hai any one help me to do chat application in flex that has to receive data while in online and also have to get the data when we are in offline.
hi can anybody plz tell me hw to sort the enable linkbuttons in the datagrid coloumn. all the enabled icon in the link button shud be sorted on the click of header. it would of great help if anybody comes up with a solution for this. Thank u
Hi Fatt, Can u pls tell me how to write the code for "Next" & "Previous" buttons to select the image
Hi Friends, Anyone can help me for how to write the code for "Next" & "Previous" buttons to select the images in Horizontal list..?
Excellent example this exactly what I wanted the only thing is I have the 2 XML sources coming from httpservice calls dynamically. The "data" part is fine but how can I change the below line if games.xml is coming from httpservice call?
text="{games.game.(@id == data.favGameID).name}"
Excellent example this exactly what I wanted the only thing is I have the 2 XML sources coming from httpservice calls dynamically. The "data" part is fine but how can I change the below line if games.xml is coming from httpservice call?
text="{games.game.(@id == data.favGameID).name}"
If anyone knows the solution please email to jaya_jw@hotmail.com.
Hi Fatt,
Is It Possible for Use Two DataProviders in One DataGrid i,e Two fields from one Grid and One contain from another gird If not possible then Why?
If anyone knows please email to noorul1986@gmail.com
Thanks
what i want to do is to click on any perticular cell of calenderand a window should be displayed according to the sclected date. how can i do that which control should i use.?
Hi all and Fattest
Im havin a bit of a stumble block.
Im using flex 4 and the not supported anymore. I tried using instead as I should but get an error that I dont know how to resolve.
Description Resource Path Location Type In initializer for 'mxmlContent', type mx.core.IFactory is not assignable to target type Array or target element type mx.core.IVisualElement. datGrid.mxml /datGrid/src line 42 Flex Problem
Please any help would be greatly appreciated
see now code was excluded in post.
mx component not working so using fx component
ok go fx component to stop giving me an error but now the datagrid is not showing when I run the application.
Please anybody help :(
sorry can also contact me on creativelyinteractive@gmail.com
thank you all ;-)
Hi...How to add a label above the datagrid? I have developed a CustomDataGrid which should have a label above the grid. Eg. should display the grid with a label above it.
Thanks in advance!!
How to highlight a added row in advanced datagrid programmatically....any suggestions is appreciated (Consider the scenario that there is a add button. When the user will click the button a new row will be added and will display as highlighted)