-1

I'm trying to create a DataGrid dynamically in Flash Builder AS3 code (not in the markup) but can't get any data to display. Here's how I'm created the grid:

for (var i:int = 0; i < numOfWinners; i++){
            if (list[i].Name[0])                        
                dataP.addItem({profilePic: list[i].ProfilePicUrl.toString(), name: list[i].Name.toString(), prizePic: list[i].ItemImageUrl.toString()});
            else
                dataP.addItem({profilePic: list[i].ProfilePicUrl.toString(), name: list[i].UserName.toString(), prizePic: list[i].ItemImageUrl.toString()});
        }

        // Create a new DataGridColumn object.
        var playerCol:GridColumn = new GridColumn("profilePic");
        playerCol.width = 110;

        var nameCol:GridColumn = new GridColumn("name");
        nameCol.width = 100;             
        nameCol.itemRendererFunction = MyCustomItemRendererFunction;            
        var prizePicCol:GridColumn = new GridColumn("prizePic");
        prizePicCol.width = 160;

        cols.addItem(nameCol);
        cols.addItem(playerCol);
        cols.addItem(prizePicCol);
        grid.columns = cols;            
        grid.dataProvider = dataP;



        private function MyCustomItemRendererFunction(item:Object, column:GridColumn):ClassFactory 
    {           
        // Create a Class Factory variable
        var myRendererObject:ClassFactory;

        // This is the "default" color is nothing else happens
        // I'm passing it a pointer to "MyItemRenderer"

        myRendererObject = new ClassFactory(ProfilePicRenderer);

        if (column.dataField == "profilePic")
        {
            myRendererObject.properties = { source: item.profilePic};
        }

        return myRendererObject;

    }

All the examples I seem to find are in the markup for a Flex 4 (MX + Spark) project.

What am I doing wrong here?

Thanks!

jbassking10
  • 833
  • 4
  • 15
  • 42
  • Could you please specify what does "can't get any data to display" mean ? Is the grid and the columns visible ? Do you see your itemrenderers but they are empty? If so, please post the code of your ProfilePicRenderer. What i already can tell is that you should just override the set data() method in your item renderer and get the profile pic there instead of setting it via properties - this is what set data is for :) – Philarmon May 11 '16 at 07:27
  • I don't see anything. Just a blank space. The itemRendererFunction never gets called. I have a break point but it doesn't get hit. – jbassking10 May 11 '16 at 12:27
  • Since I don't see it in your code I have to ask - you do add the datagrid to the stage with addChild(grid), right? And you don't set it to visible = false at some point or something? – Philarmon May 12 '16 at 07:25
  • correct. It seems this may not be supported in a pure AS project without Flex MX/Spark – jbassking10 May 12 '16 at 14:22
  • But generally there is a way to use datagrids without Flex. Have you seen this? http://help.adobe.com/en_US/ActionScript/3.0_UsingComponentsAS3/WS5b3ccc516d4fbf351e63e3d118a9c65b32-7fa8.html – Philarmon May 13 '16 at 09:28

1 Answers1

1

You can add ProfilePicRenderer as below:

var playerCol:GridColumn = new GridColumn("Profile Picture");
var profilePicItemRenderer:ClassFactory = new ClassFactory(ProfilePicRenderer);
playerCol.itemRenderer =  profilePicItemRenderer;
playerCol.width = 110;

where ProfilePicRenderer.mxml looks like below:

<?xml version="1.0"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    override public function set data(value:Object):void {
        if (super.data != value) {
            super.data = value;
        }
        image.source = "./images/" + data.profilePic;
    }
    ]]></fx:Script>
<s:Image id="image" width="50" height="50"/>

</s:GridItemRenderer>

Similarly add PrizePicRenderer as below:

var prizePicCol:GridColumn = new GridColumn("Prize Picture");
var prizePicRenderer:ClassFactory = new ClassFactory(PrizePicRenderer);
prizePicCol.itemRenderer =  prizePicRenderer;
prizePicCol.width = 160;

where PrizePicRenderer.mxml looks like below:

<?xml version="1.0"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    override public function set data(value:Object):void {
        if (super.data != value) {
            super.data = value;
        }
        image.source = "./images/" + data.prizePic;
    }
    ]]></fx:Script>
<s:Image id="image" width="50" height="50"/>
</s:GridItemRenderer>

Here is the sample application which adds the DataGrid dynamically with itemRenderer:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import mx.collections.ArrayList;

    import spark.components.DataGrid;
    import spark.components.gridClasses.GridColumn;

    private var dataP:ArrayCollection = new ArrayCollection([
        {"name": "Lebron James", "profilePic": "p1.jpeg", "prizePic": "t1.jpg"},
        {"name": "Stephen Curry", "profilePic": "p2.jpeg", "prizePic": "t2.jpg"},
        {"name": "Kevin Durant", "profilePic": "p3.jpeg", "prizePic": "t3.jpg"},]) ;

    private function addDataGrid():void
    {
        var grid:DataGrid = new DataGrid();

        var nameCol:GridColumn = new GridColumn("Name");
        nameCol.width = 100;
        nameCol.dataField = "name";

        var playerCol:GridColumn = new GridColumn("Profile Picture");
        var profilePicItemRenderer:ClassFactory = new ClassFactory(ProfilePicRenderer);
        playerCol.itemRenderer =  profilePicItemRenderer;
        playerCol.width = 110;

        var prizePicCol:GridColumn = new GridColumn("Prize Picture");
        var prizePicRenderer:ClassFactory = new ClassFactory(PrizePicRenderer);
        prizePicCol.itemRenderer =  prizePicRenderer;
        prizePicCol.width = 160;

        var cols:ArrayList = new ArrayList();
        cols.addItem(nameCol);
        cols.addItem(playerCol);
        cols.addItem(prizePicCol);
        grid.columns = cols;

        grid.dataProvider = dataP;
        vGroup.addElement(grid);
    }
    ]]></fx:Script>
<s:VGroup id="vGroup" paddingTop="10" paddingLeft="10"
                    paddingBottom="10" paddingRight="10">
    <s:Button label="Click to build DataGrid dynamically." click="addDataGrid()"/>
</s:VGroup>
</s:Application> 

I assume you have picture files in your project and dataProvider is well defined. Hope this is helpful.

Here is the ActionScript version of ProfilePicRenderer which is ProfilePicRendererAS.as if you don't want MXML.

package {
import spark.components.Image;
import spark.components.gridClasses.GridItemRenderer;

public class ProfilePicRendererAS extends GridItemRenderer {
private var image:Image;
public function ProfilePicRendererAS() {
    super();
    image = new Image();
    image.width = 50;
    image.height = 50;
    this.addElement(image);
}
override public function set data(value:Object):void {
    if (super.data != value) {
        super.data = value;
    }
    image.source = "./images/" + data.profilePic;
}
}
}

enter image description here

gbdcool
  • 974
  • 7
  • 18
  • Thanks! I tried it out and I'm getting a null reference from somewhere. It gets thrown vGroup.addElement. vGroup is not null. Here's the top of the stack: TypeError: Error #1009: Cannot access a property or method of a null object reference. at mx.styles::StyleManager$/getStyleManager()[E:\dev\4.y\frameworks\projects\framework\src\mx\styles\StyleManager.as:139] – jbassking10 May 11 '16 at 19:12
  • I should also mention that I'm doing the whole app in AS3. We're not using MXML. – jbassking10 May 11 '16 at 19:12
  • I've provided AS3 version of ProfilePicRenderer.mxml. Similarly you can write AS3 version of PrizePicRenderer.mxml. – gbdcool May 11 '16 at 20:31
  • Any idea what the exception would be from? Do I need to create a style manager or something? – jbassking10 May 11 '16 at 20:32
  • It's working fine for me without error. I'm using Flex SDK 4.6 version.Which version of SDK you are using? – gbdcool May 11 '16 at 20:41
  • You got the above error when you run my sample application or you tried to implement the same in your application? – gbdcool May 11 '16 at 20:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111676/discussion-between-gbdcool-and-jbassking10). – gbdcool May 11 '16 at 20:52