4 Replies Latest reply on Jul 3, 2008 2:09 PM by dimival

    Adding a row to DataGrid using ArrayCollection

    Joey-ATL Level 1
      One approach works, when I hard code it; doing it programatically doesn't:

      //************* Approach 1 - Hard-Coding ***********************************
      var acData:ArrayCollection = new ArrayCollection();
      ...

      //Each DataGridColum has a dataField like "col1", "col2", "col3", etc ...
      //Therefore, adding items like this works
      acData.addItem({col0:"testa",col1:"testb",col2:"testc",col3:"testd",col4:"teste"});


      //************* Approach 2 - Programatically ***********************************
      //But adding items like this doesn't
      for(var j=0; j<dataRows.length(); j++){
      var t:String = dataRows[j].toString();//dataRows is XMLList type, data is formatted just like the above format
      //I did a breakpoint to verify and had someone else look at the string and it matches the format that works.
      acData.addItem(t);
      }

      When I try the second approach I get the entire contents of the string in each grid cell, where in the first approach I get "testa" in the first column, "testb" in the second column (without the quotes of course), etc ....

      Does anyone know why there is a difference or what I am doing wrong?

      Thanks in advance.


        • 1. Re: Adding a row to DataGrid using ArrayCollection
          dimival Level 1
          I guess that is because in the second approach you are adding a string to your ArrayCollection instead of an object like the in the first approach:

          acData.addItem({col0:"testa",col1:"testb",col2:"testc",col3:"testd",col4:"teste"}); // This is an object with properties which you are adding to the ArrayCollection

          var t:String = dataRows[j].toString(); //this is a string, not an object
          acData.addItem(t); // you are adding a string to the ArrayCollection

          You need to take the string from you XML and build a new object like in your first approach, using the { }


          • 2. Adding a row to DataGrid using ArrayCollection
            Joey-ATL Level 1
            Yes, I agree!

            I have the string perfectly formatted so how do I convert it to an Object? I know in JavaScript I could use the eval function, but I am using ActionScript3 which I see has removed that function much like JavaScript has.

            Is there another way? I am seeing a couple of areas talking about square brackets as a replacement for eval, I don't see a good example of how it works.

            Maybe I should be asking, how do you create a Object programmatically instead?

            ****** This works too: *********
            var o:Object = new Object();
            o.col0 = "JJ";
            o.col1 = "JJ";
            o.col2 = "JJ";
            o.col3 = "JJ";
            o.col4 = "JJ";
            acData.addItem(o);


            But I don't know how many columns there will be, it will change every time so I can't create them ahead of time.
            • 3. Re: Adding a row to DataGrid using ArrayCollection
              Joey-ATL Level 1
              I got it!

              I used this and it worked.

              var o:Object = new Object();
              o["col" + 0] = "testa";
              o["col" + 1] = "testb";
              o["col" + 2] = "testc";
              o["col" + 3] = "testd";
              o["col" + 4] = "teste";
              acData.addItem(o);

              so then I put this in a For Loop like so:

              var o:Object = new Object();
              for(var k=0; k<arrData.length; k++){
              o["col" + k] = arrData[k];
              }
              acData.addItem(o);

              Thanks for the help!
              • 4. Re: Adding a row to DataGrid using ArrayCollection
                dimival Level 1
                Glad i could help out, in the end you found the solution on your own which is great.