3 Replies Latest reply on Jul 24, 2009 5:39 PM by Mike6679

    DataGrid - Create at runtime - How to Add Items??

    Mike6679 Level 1

      I create a DataGrid at runtime and then and columns to it as need be. How Can I add items with the correct dataField if I don't if I don;t know this till runtime? In other word I'm having trouble constructing the Object to send to AddItem becase the dataField Name needs to be hard coded...

      Below does not work for me because if I have more than one column then I can seem to figure out out to create  ItemObjFinal dynnamically.

       

      var ItemObjFinal:Object = {ThisNameNeedsToBeDynamic: "text", ThisNameNeedsToBeDynamic: "value" };

       

       

      I also tried creating an array of Objects like this:

       

      var ItemObjFinal:Object = new Object;
      var obj:Object= dgc.dataField;
      ItemObjFinal [0] = {(obj.valueOf()):  dgc.headerText };

      ItemObjFinal [1] = {(obj.valueOf()):  dgc.headerText };

       

       

       

       

       

      =========================================================================================

       

                    ac.addItemAt(dgc, int(ac.length));
                    dataGrid_preview.columns = ac.toArray();
                   
                   
                   
                    var obj:Object= dgc.dataField;
                    var ItemObjFinal:Object = {(obj.valueOf()):  dgc.headerText };
                   
                    var obj2:Object= dgc.dataField;
                    var ItemObjFinal2:Object = {(obj2.valueOf()): dgc.headerText};
                   
                   
                   ////////////////////////////

       

                    //K Now add it!
                    //IList(dataGrid_preview.dataProvider).removeAll();

                    IList(dataGrid_preview.dataProvider).addItemAt(ItemObjFinal,0);
                   //IList(dataGrid_preview.dataProvider).addItemAt(ItemObjFinal2,1);

        • 1. Re: DataGrid - Create at runtime - How to Add Items??
          Flex harUI Adobe Employee

          Before I show you the answer, please re-think whether you'll need this code in the end.  It is very rare to need to do this because either you know the names of the columns already or they will be determined by data fetched from a server and you won't need to make up objects since they'll also come from the server.

           

          Usually you know you need fields like firstName and lastName and set up the DataGridColumns dataField="firstName" and dataField="lastName" and mock up objects like

           

          Other times, you'll get a chunk of data objects.  You can use ObjectUtil.getClassInfo to get the list of fields, but then you'll probably not need to mock up objects since you already have them in that chunk.

           

          However, if this is what you really need to do, the code should look like this:

           

          var ItemObjFinal:Object = new Object;

          ItemObjFinal[dgc.dataField] = dgc.headerText;

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: DataGrid - Create at runtime - How to Add Items??
            Mike6679 Level 1

            Thanks for the quick reply. But I don't think this solves my problem . Your code seems to do do what I was already doing but in a different way. I need to construct  ItemObjFinal to contain ALL the columns data for a given row. For example:

             

            var ItemObjFinal:Object = {ThisNameNeedsToBeDynamic: "text", ThisNameNeedsToBeDynamic: "value" };

             

             

            This code above adds data for one row *2* columns across. How can I do that?

             

             

             

            FYI: This is actually for a Demo that allows users to create, modify , delete  fields dynamically for a DataGrid and its not tied into any back end database so I need to do it this way for now....

            • 3. Re: DataGrid - Create at runtime - How to Add Items??
              Mike6679 Level 1

              Ahh answered my own question:

               

               

                           ac.addItemAt(dgc, int(ac.length));
                            dataGrid_preview.columns = ac.toArray();
                           
                        
                            var ItemObjFinal:Object = new Object;
                            var ItemObjFinal2:Object = new Object;
                           
                            for each(var col:DataGridColumn in ac)
                            {
                              ItemObjFinal[col.dataField] = col.headerText;
                              ItemObjFinal2[col.dataField] = col.headerText;
                            }
                           
                            ItemObjFinal[dgc.dataField] = dgc.headerText;
                            ItemObjFinal2[dgc.dataField] = dgc.headerText;
                     
                         
                            //K Now add it!
                            if(IList(dataGrid_preview.dataProvider).length > 1)
                            {
                                IList(dataGrid_preview.dataProvider).removeItemAt(0);
                                IList(dataGrid_preview.dataProvider).removeItemAt(1);
                            }
                           
                            IList(dataGrid_preview.dataProvider).addItemAt(ItemObjFinal,0);
                            IList(dataGrid_preview.dataProvider).addItemAt(ItemObjFinal2,1);

               

               

              This code may still need some tweaking as I get an RTE at  "IList(dataGrid_preview.dataProvider).removeItemAt(1);"   but at least I'm able to solve my original question. Thanks Alex!